2

I try to implement 3-way-hadnshake with a raw socket in Python and using Scapy.

The code is:

s=socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.IPPROTO_TCP)
ss=StreamSocket(s)
iph=IPheader()
syn = TCP(sport=TCP_SOURCE_PORT,dport=TCP_DESTINATION_PORT, flags="S")
synack = ss.sr1(iph/syn)
myack = iph/TCP(dport=synack[TCP].sport, sport=synack[TCP].dport, seq=synack[TCP].ack, ack=synack[TCP].seq+1, flags="A")
ss.send(myack)

IPheader() method return a scapy IP header.

When running the script i get this error:

ERROR: --- Error in child 3057
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 89, in sndrcv
pks.send(p)
File "/usr/lib/python2.7/dist-packages/scapy/supersocket.py", line 34, in send
return self.outs.send(sx)
error: [Errno 6] No such device or address

2 Answers 2

2

I see a couple of possible problems with your code:

  • before invoking StreamSocket() you need to establish a connection with a regular socket. So you need to make a connection, something like s.connect(("10.1.1.1",9000)) before the line ss=StreamSocket(s). Further information can be found here

  • You may need to correct base socket type. I would suggest something like s=socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP). For further information check this

2
  • Made the changes and works. Now i try to send the SYN packet through that StreamSocket but i see it malformed on Wireshark. iph=IPheader() syn = TCP(sport=TCP_SOURCE_PORT, dport=TCP_DESTINATION_PORT, seq=100, ack=400, flags="S") pkt=iph/syn ss.send(pkt) Commented Apr 16, 2014 at 8:16
  • I solved the above problem, by sending through socket only TCP header without IP header. Next problem, my device is not responding to my SYN packet. I can't set the sequence number i want, and the SYN packet is sent with Seq=0, but i don't think this is the problem Commented Apr 16, 2014 at 9:01
0

The device is not responding to your SYN packet because a RAW socket does not do that. You have to send the SYN-ACK manually. Also, Wireshark and TCP show sequence numbers as RELATIVE. In order to show the ACTUAL number you must turn this option off. Thirdly, you can set the sequence number manually or randomize it using

TCP(sport = port1, dport = port2, flags="A", seq = random.getrandbits(32), ack = MY_ACK)

or

TCP(sport = port1, dport = port2, flags="A", seq = 01395, ack = MY_ACK)

Not the answer you're looking for? Browse other questions tagged or ask your own question.