id: 512 seq: 7936
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python26\lib\threading.py", line 522, in __bootstrap_inner
self.run()
File "pcapytest.py", line 54, in run
self.pcap.loop(0, self.packetHandler)
File "pcapytest.py", line 68, in packetHandler
self.createAnswer(packageip)
File "pcapytest.py", line 117, in createAnswer
s.sendto(ip.get_packet(), (dst, 0))
error: [Errno 10004] blocking was cancelled through WSACancelBlockingCall #free translation from german
how can i rewrite that part so that it works?
import sys
import string
from threading import Thread
import pcapy
from pcapy import findalldevs, open_live
import impacket
from impacket.ImpactDecoder import EthDecoder, LinuxSLLDecoder
from impacket import ImpactDecoder, ImpactPacket
import socket
class DecoderThread(Thread):
def __init__(self, pcapObj):
# Query the type of the link and instantiate a decoder accordingly.
datalink = pcapObj.datalink()
if pcapy.DLT_EN10MB == datalink:
self.decoder = EthDecoder()
elif pcapy.DLT_LINUX_SLL == datalink:
self.decoder = LinuxSLLDecoder()
else:
raise Exception("Datalink type not supported: " % datalink)
self.pcap = pcapObj
Thread.__init__(self)
def run(self):
# Sniff ad infinitum.
# PacketHandler shall be invoked by pcap for every packet.
self.pcap.loop(0, self.packetHandler)
def packetHandler(self, hdr, data):
# Use the ImpactDecoder to turn the rawpacket into a hierarchy
# of ImpactPacket instances.
# Display the packet in human-readable form.
package = self.decoder.decode(data)
#package = ethernet
#package.child() = IP
packageip = package.child()
print packageip.get_ip_dst()
target = '195.28.191.68'
if packageip.get_ip_dst() == target:
self.createAnswer(packageip)
def createAnswer(self, rip):
print 'creating the answer package'
# Extract the ICMP packet from its container (the IP packet).
ricmp = rip.child()
#get the id
icmpid = ricmp.get_icmp_id()
#get the seq
icmpseq = ricmp.get_icmp_seq()
#get the child from icmp
newchild = ricmp.child()
#get the data from the child
answerdata = newchild.get_packet()
dst = rip.get_ip_src()
src = ""
print 'source ip:', src, 'destination ip:', dst
print 'answerdata', repr(answerdata)
print 'id:', icmpid, 'seq:', icmpseq
#dst = '192.168.11.170'
#src = ''
#src = ''
ip = ImpactPacket.IP()
ip.set_ip_src(src) #fuer klaus seinen rechner: die bf2 ip
ip.set_ip_dst(dst) #141.41.92.115 ?
icmp = ImpactPacket.ICMP()
icmp.set_icmp_type(icmp.ICMP_ECHOREPLY)
icmp.set_icmp_code(icmp.ICMP_ECHO)
icmp.set_icmp_id(icmpid)
icmp.set_icmp_seq(icmpseq)
icmp.contains(ImpactPacket.Data(answerdata))
# Have the IP packet contain the ICMP packet (along with its payload).
ip.contains(icmp)
# Calculate its checksum.
icmp.set_icmp_cksum(0)
icmp.auto_checksum = 1
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# Send it to the target host.
s.sendto(ip.get_packet(), (dst, 0))
s.close()
from pcapy import findalldevs, open_live
from impacket import ImpactDecoder, ImpactPacket
def get_interface():
# Get the list of interfaces we can listen on
ifs = findalldevs()
# No interfaces found
if len(ifs) == 0:
raise RuntimeError, "Error: no available network interfaces, or you don't have enough permissions on this system."
# A single interface was found
if len(ifs) == 1:
interface = ifs[0]
# Multiple interfaces found
else:
print "Available network interfaces:"
for i in xrange(len(ifs)):
print '\t%i - %s' % (i + 1, ifs[i])
print
while 1:
choice = raw_input("Choose an interface [0 to quit]: ")
try:
i = int(choice)
if i == 0:
interface = None
break
interface = ifs[i-1]
break
except Exception:
pass
# Return the selected interface
return interface
def main(filter):
dev = get_interface()
#dev = 'eth0'
# Open interface for catpuring.
p = open_live(dev, 1500, 0, 100)
# Set the BPF filter. See tcpdump(3).
p.setfilter(filter)
print "Listening on %s: net=%s, mask=%s, linktype=%d" % (dev, p.getnet(), p.getmask(), p.datalink())
# Start sniffing thread and finish main thread.
DecoderThread(p).start()
# Process command-line arguments. Take everything as a BPF filter to pass
# onto pcap. Default to the empty filter (match all).
main('icmp')