Scapy Example
#!/usr/bin/python3
from scapy.all import *
TARGET = '8.8.8.8'
MAX_TTL = 14
TIMEOUT = 1.5
ip = IP()
icmp = ICMP()
ip.dst = TARGET
ip.ttl = (1,MAX_TTL)
p = ip/icmp
ans, uans= sr(p, timeout=TIMEOUT)
for snd, rcv in ans:
result = str(snd.ttl) + '\t' + rcv.src
if rcv.src == TARGET:
result += ' \t <== TARGET!'
print(result)
break
print(result)
sniff and spoof
#!/usr/bin/python3
from scapy.all import *
TARGET = '192.168.41.140'
spoof_ip = '8.8.8.8'
def spoof(pkt):
pkt.show()
ip = IP()
ip.src = spoof_ip
ip.dst = TARGET
icmp = ICMP()
icmp.type = 'echo-reply'
icmp.id = pkt[2].id
icmp.seq = pkt[2].seq
p = ip/icmp/pkt[3]
send(p)
pkt = sniff(filter='icmp[icmptype] == icmp-echo', prn=spoof)