Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
paaguti committed Jun 2, 2017
2 parents fff838c + 6db9cf9 commit ad411ca
Show file tree
Hide file tree
Showing 107 changed files with 4,669 additions and 2,571 deletions.
3 changes: 2 additions & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ build: off

install:
# Install the npcap, windump and wireshark suites
- choco install -y npcap wireshark
- ps: .\.appveyor\InstallNpcap.ps1
- ps: .\.appveyor\InstallWindump.ps1
- choco install -y wireshark
# Install Python modules
- "%PYTHON%\\python -m pip install cryptography coverage mock pyreadline"
- set PATH="%PYTHON%\\Scripts\\;%PATH%"
Expand Down
19 changes: 19 additions & 0 deletions .appveyor/InstallNpcap.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Config
$urlPath = "https://nmap.org/npcap/dist/npcap-0.90.exe"
$checksum = "0477a42a9c54f31a7799fb3ee0537826041730f462abfc066fe36d81c50721a7"

############
############
# Download the file
wget $urlPath -UseBasicParsing -OutFile $PSScriptRoot"\npcap.exe"
# Now let's check its checksum
$_chksum = $(CertUtil -hashfile $PSScriptRoot"\npcap.exe" SHA256)[1] -replace " ",""
if ($_chksum -ne $checksum){
echo "Checksums does NOT match !"
exit
} else {
echo "Checksums matches !"
}
# Run installer
Start-Process $PSScriptRoot"\npcap.exe" -ArgumentList "/loopback_support=yes /S" -wait
echo "Npcap installation completed"
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
scapy/__init__.py export-subst
* text=auto
*.bat text eol=crlf
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ parsed from a string (during a network capture or a PCAP file
read). Adding inefficient code here will have a disastrous effect on
Scapy's performances.

### Python 2 and 3 compatibility

The project aims to provide code that works both on Python 2 and Python 3. Therefore, some rules need to be apply to achieve compatibility:
- byte-string must be defined as `b"\x00\x01\x02"`
- exceptions must comply with the new Python 3 format: `except SomeError as e:`
- lambdas must be written using a single argument when using tuples: use `lambda x_y: x_y[0] + f(x_y[1])` instead of `lambda (x, y): x + f(y)`.

### Code review

Maintainers tend to be picky, and you might feel frustrated that your
Expand Down
8 changes: 4 additions & 4 deletions bin/UTscapy.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
REM Use Python to run the UTscapy script from the current directory, passing all parameters
title UTscapy
python "%~dp0\UTscapy" %*
@echo off
REM Use Python to run the UTscapy script from the current directory, passing all parameters
title UTscapy
python "%~dp0\UTscapy" %*
8 changes: 4 additions & 4 deletions bin/scapy.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
REM Use Python to run the Scapy script from the current directory, passing all parameters
title scapy
python "%~dp0\scapy" %*
@echo off
REM Use Python to run the Scapy script from the current directory, passing all parameters
title scapy
python "%~dp0\scapy" %*
4 changes: 2 additions & 2 deletions dev/scripts/autoFixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def main():
opts, args = getopt.getopt(sys.argv[1:], "t:")
if not args:
raise getopt.error, "At least one file argument required"
except getopt.error, msg:
except getopt.error as msg:
print msg
print "usage:", sys.argv[0], "files ..."
return
Expand Down Expand Up @@ -43,7 +43,7 @@ def process(filename, tabsize):
f = open(filename)
text = f.read()
f.close()
except IOError, msg:
except IOError as msg:
print "%r: I/O error: %s" % (filename, msg)
return
# Remove tabs
Expand Down
2 changes: 1 addition & 1 deletion doc/scapy/troubleshooting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ I can't ping 127.0.0.1. Scapy does not work with 127.0.0.1 or on the loopback in

The loopback interface is a very special interface. Packets going through it are not really assembled and disassembled. The kernel routes the packet to its destination while it is still stored an internal structure. What you see with tcpdump -i lo is only a fake to make you think everything is normal. The kernel is not aware of what Scapy is doing behind his back, so what you see on the loopback interface is also a fake. Except this one did not come from a local structure. Thus the kernel will never receive it.

In order to speak to local applications, you need to build your packets one layer upper, using a PF_INET/SOCK_RAW socket instead of a PF_PACKET/SOCK_RAW (or its equivalent on other systems that Linux)::
In order to speak to local applications, you need to build your packets one layer upper, using a PF_INET/SOCK_RAW socket instead of a PF_PACKET/SOCK_RAW (or its equivalent on other systems than Linux)::

>>> conf.L3socket
<class __main__.L3PacketSocket at 0xb7bdf5fc>
Expand Down
12 changes: 6 additions & 6 deletions run_scapy.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off
set PYTHONPATH=%cd%
python -m scapy.__init__
if errorlevel 1 (
PAUSE
)
@echo off
set PYTHONPATH=%cd%
python -m scapy.__init__
if errorlevel 1 (
PAUSE
)
14 changes: 7 additions & 7 deletions scapy/arch/bpf/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_if_raw_addr(ifname):
# Get ifconfig output
try:
fd = os.popen("%s %s" % (conf.prog.ifconfig, ifname))
except OSError, msg:
except OSError as msg:
warning("Failed to execute ifconfig: (%s)" % msg)
return b"\0\0\0\0"

Expand All @@ -78,7 +78,7 @@ def get_if_raw_hwaddr(ifname):
# Get ifconfig output
try:
fd = os.popen("%s %s" % (conf.prog.ifconfig, ifname))
except OSError, msg:
except OSError as msg:
raise Scapy_Exception("Failed to execute ifconfig: (%s)" % msg)

# Get MAC addresses
Expand All @@ -104,7 +104,7 @@ def get_dev_bpf():
try:
fd = os.open("/dev/bpf%i" % bpf, os.O_RDWR)
return (fd, bpf)
except OSError, err:
except OSError as err:
continue

raise Scapy_Exception("No /dev/bpf handle is available !")
Expand All @@ -117,7 +117,7 @@ def attach_filter(fd, iface, bpf_filter_string):
command = "%s -i %s -ddd -s 1600 '%s'" % (conf.prog.tcpdump, iface, bpf_filter_string)
try:
f = os.popen(command)
except OSError, msg:
except OSError as msg:
raise Scapy_Exception("Failed to execute tcpdump: (%s)" % msg)

# Convert the byte code to a BPF program structure
Expand Down Expand Up @@ -154,7 +154,7 @@ def get_if_list():
# Get ifconfig output
try:
fd = os.popen("%s -a" % conf.prog.ifconfig)
except OSError, msg:
except OSError as msg:
raise Scapy_Exception("Failed to execute ifconfig: (%s)" % msg)

# Get interfaces
Expand Down Expand Up @@ -184,7 +184,7 @@ def get_working_ifaces():
# Get interface flags
try:
result = get_if(ifname, SIOCGIFFLAGS)
except IOError, msg:
except IOError as msg:
warning("ioctl(SIOCGIFFLAGS) failed on %s !" % ifname)
continue

Expand All @@ -201,7 +201,7 @@ def get_working_ifaces():
try:
fcntl.ioctl(fd, BIOCSETIF, struct.pack("16s16x", ifname))
interfaces.append((ifname, int(ifname[-1])))
except IOError, err:
except IOError as err:
pass

# Close the file descriptor
Expand Down
22 changes: 11 additions & 11 deletions scapy/arch/bpf/supersocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def __init__(self, iface=None, type=ETH_P_ALL, promisc=None, filter=None, nofilt
# Set the BPF buffer length
try:
fcntl.ioctl(self.ins, BIOCSBLEN, struct.pack('I', BPF_BUFFER_LENGTH))
except IOError, err:
except IOError as err:
msg = "BIOCSBLEN failed on /dev/bpf%i" % self.dev_bpf
raise Scapy_Exception(msg)

# Assign the network interface to the BPF handle
try:
fcntl.ioctl(self.ins, BIOCSETIF, struct.pack("16s16x", self.iface))
except IOError, err:
except IOError as err:
msg = "BIOCSETIF failed on %s" % self.iface
raise Scapy_Exception(msg)
self.assigned_interface = self.iface
Expand All @@ -75,15 +75,15 @@ def __init__(self, iface=None, type=ETH_P_ALL, promisc=None, filter=None, nofilt
# Don't block on read
try:
fcntl.ioctl(self.ins, BIOCIMMEDIATE, struct.pack('I', 1))
except IOError, err:
except IOError as err:
msg = "BIOCIMMEDIATE failed on /dev/bpf%i" % self.dev_bpf
raise Scapy_Exception(msg)

# Scapy will provide the link layer source address
# Otherwise, it is written by the kernel
try:
fcntl.ioctl(self.ins, BIOCSHDRCMPLT, struct.pack('i', 1))
except IOError, err:
except IOError as err:
msg = "BIOCSHDRCMPLT failed on /dev/bpf%i" % self.dev_bpf
raise Scapy_Exception(msg)

Expand All @@ -105,7 +105,7 @@ def set_promisc(self, value):

try:
fcntl.ioctl(self.ins, BIOCPROMISC, struct.pack('i', value))
except IOError, err:
except IOError as err:
msg = "Can't put your interface (%s) into promiscuous mode !" % self.iface
raise Scapy_Exception(msg)

Expand All @@ -120,7 +120,7 @@ def guess_cls(self):
try:
ret = fcntl.ioctl(self.ins, BIOCGDLT, struct.pack('I', 0))
ret = struct.unpack('I', ret)[0]
except IOError, err:
except IOError as err:
warning("BIOCGDLT failed: unable to guess type. Using Ethernet !")
return Ether

Expand All @@ -143,7 +143,7 @@ def set_nonblock(self, set_flag=True):
if self.fd_flags is None:
try:
self.fd_flags = fcntl.fcntl(self.ins, fcntl.F_GETFL)
except IOError, err:
except IOError as err:
warning("Can't get flags on this file descriptor !")
return

Expand All @@ -165,7 +165,7 @@ def get_stats(self):
try:
ret = fcntl.ioctl(self.ins, BIOCGSTATS, struct.pack("2I", 0, 0))
return struct.unpack("2I", ret)
except IOError, err:
except IOError as err:
warning("Unable to get stats from BPF !")
return (None, None)

Expand All @@ -175,7 +175,7 @@ def get_blen(self):
try:
ret = fcntl.ioctl(self.ins, BIOCGBLEN, struct.pack("I", 0))
return struct.unpack("I", ret)[0]
except IOError, err:
except IOError as err:
warning("Unable to get the BPF buffer length")
return

Expand Down Expand Up @@ -282,7 +282,7 @@ def recv(self, x=BPF_BUFFER_LENGTH):
# Get data from BPF
try:
bpf_buffer = os.read(self.ins, x)
except EnvironmentError, e:
except EnvironmentError as e:
if e.errno == errno.EAGAIN:
return
else:
Expand Down Expand Up @@ -333,7 +333,7 @@ def send(self, pkt):
if self.assigned_interface != iff:
try:
fcntl.ioctl(self.outs, BIOCSETIF, struct.pack("16s16x", iff))
except IOError, err:
except IOError as err:
msg = "BIOCSETIF failed on %s" % iff
raise Scapy_Exception(msg)
self.assigned_interface = iff
Expand Down
16 changes: 8 additions & 8 deletions scapy/arch/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def attach_filter(s, bpf_filter, iface):
conf.iface if iface is None else iface,
bpf_filter,
))
except OSError,msg:
except OSError as msg:
log_interactive.warning("Failed to execute tcpdump: (%s)")
return
lines = f.readlines()
Expand All @@ -146,7 +146,7 @@ def attach_filter(s, bpf_filter, iface):
nb = int(lines[0])
bpf = ""
for l in lines[1:]:
bpf += struct.pack("HBBI",*map(long,l.split()))
bpf += struct.pack("HBBI", *(int(e) for e in l.split()))

# XXX. Argl! We need to give the kernel a pointer on the BPF,
# python object header seems to be 20 bytes. 36 bytes for x86 64bits arch.
Expand Down Expand Up @@ -282,7 +282,7 @@ def in6_getifaddr():
ret = []
try:
f = open("/proc/net/if_inet6","r")
except IOError, err:
except IOError as err:
return ret
l = f.readlines()
for i in l:
Expand All @@ -296,7 +296,7 @@ def in6_getifaddr():
def read_routes6():
try:
f = open("/proc/net/ipv6_route","r")
except IOError, err:
except IOError as err:
return []
# 1. destination network
# 2. destination prefix length
Expand Down Expand Up @@ -359,9 +359,9 @@ def get_last_packet_timestamp(sock):


def _flush_fd(fd):
if type(fd) is not int:
if hasattr(fd, 'fileno'):
fd = fd.fileno()
while 1:
while True:
r,w,e = select([fd],[],[],0)
if r:
os.read(fd,MTU)
Expand Down Expand Up @@ -457,7 +457,7 @@ def send(self, x):
x.sent_time = time.time()
try:
self.outs.sendto(sx, sdto)
except socket.error, msg:
except socket.error as msg:
if msg[0] == 22 and len(sx) < conf.min_pkt_size:
self.outs.send(sx + b"\x00" * (conf.min_pkt_size - len(sx)))
elif conf.auto_fragment and msg[0] == 90:
Expand Down Expand Up @@ -522,7 +522,7 @@ def recv(self, x=MTU):
def send(self, x):
try:
return SuperSocket.send(self, x)
except socket.error, msg:
except socket.error as msg:
if msg[0] == 22 and len(x) < conf.min_pkt_size:
padding = b"\x00" * (conf.min_pkt_size - len(x))
if isinstance(x, Packet):
Expand Down
12 changes: 5 additions & 7 deletions scapy/arch/pcapdnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def get_if_raw_addr(iff):
if a.contents.addr.contents.sa_family == socket.AF_INET:
ap = a.contents.addr
val = cast(ap, POINTER(sockaddr_in))
#ret = bytes(val.contents.sin_addr[:4])
ret = "".join([chr(x) for x in val.contents.sin_addr[:4]])
ret = "".join(chr(x) for x in val.contents.sin_addr[:4])
a = a.contents.next
break
p = p.contents.next
Expand Down Expand Up @@ -151,8 +150,7 @@ def next(self):
if not c > 0:
return
ts = self.header.contents.ts.tv_sec
pkt = "".join([ chr(i) for i in self.pkt_data[:self.header.contents.len] ])
#pkt = bytes(self.pkt_data[:self.header.contents.len])
pkt = "".join(chr(i) for i in self.pkt_data[:self.header.contents.len])
return ts, pkt
def datalink(self):
return pcap_datalink(self.pcap)
Expand Down Expand Up @@ -337,10 +335,10 @@ def send(self, x):
if conf.use_pcap:
try:
import pcap
except ImportError,e:
except ImportError as e:
try:
import pcapy as pcap
except ImportError,e2:
except ImportError as e2:
if conf.interactive:
log_loading.error("Unable to import pcap module: %s/%s" % (e,e2))
conf.use_pcap = False
Expand Down Expand Up @@ -485,7 +483,7 @@ def send(self, x):
except ImportError:
# Then, try to import dumbnet as dnet
import dumbnet as dnet
except ImportError,e:
except ImportError as e:
if conf.interactive:
log_loading.error("Unable to import dnet module: %s" % e)
conf.use_dnet = False
Expand Down
Loading

0 comments on commit ad411ca

Please sign in to comment.