s = socket.socket(socket.AF_INET,
socket.SOCK_RAW,
socket.IPPROTO_RAW)
# IP Header
ipobj = IP(src_host, dst_host)
iph = ipobj.pack()
# TCP Header
tcpobj = TCP(1234, 80)
tcpobj.data_length = len(data) # Used in pseudo header
tcph = tcpobj.pack(ipobj.source,
ipobj.destination)
# Injection
packet = iph + tcph + data #构造好的packet
s.sendto(packet,(dst_host,80))
#s.connect((dst_host,80))不管是用send()还是sendto()都能发出去
response=s.recvfrom(1024)[0][0:] #但是就是卡在这儿了,一直没有收到信息,不管是用recv()还是recvfrom()
#response=s.recv(1024)
if response:
ip = ipobj.unpack(response)
response = response[ip.ihl:]
tcp = tcpobj.unpack(response)
print "IP Header:", ip.list
print "TCP Header:", tcp.list
问题就是:构造的packet能够发出去,但是就是recv()没有收到任何信息,就一直卡在那里,加个settimeout的话,全部都是超时。我是想写个tcp syn扫描的。
参考代码的网址是:http://www.freebuf.com/articles/network/41127.html
https://github.com/OffensivePython/Pinject/blob/master/pinject.py