Skip to content

Instantly share code, notes, and snippets.

@Exchizz
Created September 26, 2018 11:22
Show Gist options
  • Save Exchizz/d471d8eb968996c772567898ecd7e46d to your computer and use it in GitHub Desktop.
Save Exchizz/d471d8eb968996c772567898ecd7e46d to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python
# Example of TCP's threeway handshake using python and scapy
# source: https://www.fir3net.com/Programming/Python/how-to-build-a-tcp-connection-in-scapy.html
# Requires:
# iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP
# Otherwise the kernel will send a TCP RESET as the kernel is not aware of TCP connection going on.
from scapy.all import *
# VARIABLES
src = "172.17.0.2"
dst = "rpii01"
sport = random.randint(1024,65535)
dport = 80
print "sport: " + str(sport)
# SYN
ip=IP(src=src,dst=dst)
SYN=TCP(sport=sport,dport=dport,flags='S',seq=1000)
SYNACK=sr1(ip/SYN)
# ACK
ACK=TCP(sport=sport, dport=dport, flags='A', seq=SYNACK.ack, ack=SYNACK.seq + 1)
send(ip/ACK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment