$ sudo python scapy.py
Welcome to Scapy (1.0.4.1beta)
>>>
>>> p = sniff(count=5)
>>> p.show()
0000 Ether / IP / TCP 10.233.200.32:53294 > 10.233.11.43:ssh A
0001 Ether / ARP who has 10.233.11.189 says 10.233.10.209 / Padding
0002 Ether / ARP who has 10.233.11.235 says 10.233.0.3 / Padding
0003 Ether / IP / TCP 10.233.0.24:ideafarm-chat > 10.233.11.43:57462 PA / Raw
0004 Ether / IP / TCP 10.233.11.43:57462 > 10.233.0.24:ideafarm-chat Ap = IP(dst='10.0.0.1')/TCP(dport=25)print p
<IP frag=0 proto=TCP dst=10.0.0.1 |<TCP dport=smtp |>>
>>> str(p)
'E\x00\x00(\x00\x01\x00\x00@\x06Z\xbb\n\xe9\x0b+\n\x00\x00\x01\x00\x14' \
'\x00\x19\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00o\xa1\x00\x00'>>> IP(str(p))
<IP version=4L ihl=5L tos=0x0 len=40 id=1 flags= frag=0L ttl=64
proto=TCP chksum=0x5abb src=10.233.11.43 dst=10.0.0.1 options=''
|<TCP sport=ftp-data dport=smtp seq=0L ack=0L dataofs=5L
reserved=0L flags=S window=8192 chksum=0x6fa1 urgptr=0 |>>
>>> p.show()
###[ IP ]###
version= 4L
ihl= 5L
tos= 0x0
len= 40
id= 1
flags=
frag= 0L
ttl= 64
proto= TCP
chksum= 0x5abb
src= 10.233.11.43
dst= 10.0.0.1
options= ''
###[ TCP ]###
sport= ftp-data
dport= smtp
seq= 0L
ack= 0L
dataofs= 5L
reserved= 0L
flags= S
window= 8192
chksum= 0x6fa1
urgptr= 0
options= {}
p = Ether()/IP()/UDP()/DNS()/DNSQR().payload attribute.print p.payload.payload
<UDP sport=domain |<DNS |<DNSQR |>>>p.dport == 53p.payload.dport = 10053sendp(packet) layer 2 send.send(packet) layer 3 send.ans, unans = srp(packet) layer 2 send and receive.ans, unans = sr(packet) layer 3 send and receive.ans = srp1(packet) layer 2 send and receive, returns first reply.ans = sr1(packet) layer 3 send and receive, returns first reply.arpcachepoison('10.0.0.1', '10.0.0.2')def arpcachepoison(target, victim):
tmac = getmacbyip(target)
p = Ether(dst=tmac) /ARP(op="who-has", psrc=victim, pdst=target)
sendp(p, iface_hint=target)mac = '0003e48d0c71'.decode("hex"); eff = '\xff'sendp(Ether(dst='ff:ff:ff:ff:ff:ff') /IP(dst='255.255.255.255') /UDP(dport=7) /Raw(eff*6 + mac*16))send(IP(dst='255.255.255.255') /UDP(dport=7) /Raw(eff*6 + mac*16))packets = sniff(count=100, filter='tcp port 80')packets = sniff(offline='capture.pcap')for p in packets:
if 'Set-Cookie' in getattr(p, 'load', None):
p.show()
>>> packets = sniff(count=100, filter='tcp port 80')
>>> for p in packets:
... if 'Set-Cookie' in getattr(p, 'load', None):
... p.show()
from pprint import pprint
packets = sniff(count=100, filter='tcp')
def keyfunc(p):
return tuple(sorted(((p.payload.src, p.sport), (p.payload.dst, p.dport))))
d = dict.fromkeys((keyfunc(p) for p in packets), 0)
for p in packets:
d[keyfunc(p)] += len(p)
pprint(d)
{(('203.144.10.9', 80), ('203.206.56.31', 32847)): 148,
(('203.144.10.9', 80), ('203.206.56.31', 54117)): 148,
...
(('203.144.10.9', 80), ('203.206.56.31', 59450)): 22667}packets = sniff(count=1000, filter='tcp port 139 or port 80')
netbios_hosts = set(p.payload.src for p in packets if p.dport == 139)
web_hosts = set(p.payload.src for p in packets if p.dport == 80)
print netbios_hosts.intersection(web_hosts)ans, unans = sr(IP(dst='10.0.0.0/24')/TCP(dport=80), timeout=5)
for snd, rcv in ans:
print snd.dst, 'port', snd.dport, 'open'target = 'av.com'
ans, unans = sr(IP(dst=target, ttl=(0,25),id=RandShort())/TCP(flags=0x2, dport=443))
for snd, rcv in ans:
print snd.ttl, rcv.src, isinstance(rcv.payload, TCP)0 10.233.255.254 False
1 10.233.255.254 False
2 203.55.228.88 False
...
9 216.115.106.207 False
10 66.218.82.223 False
11 66.94.234.13 True
12 66.94.234.13 True
...ans, unans = scapy.sr(
scapy.IP(dst='netboxblue.com')
/scapy.TCP(dport=9, sport=(40000, 40000+1000)),
timeout=30)
if len(ans) < 1000:
print 'Connection tracking problem'
# Send a packet with a sourceport from 1024 to 65355
ans, unans = scapy.sr(
scapy.IP(dst='netboxblue.com')
/scapy.TCP(dport=80, sport=(1024, 65000)),
timeout=30)
# look through all the packets we didn't get replies for:
for x in unans:
print x.sportapt-get install python-scapy