-
Notifications
You must be signed in to change notification settings - Fork 1
/
handshake.py
68 lines (58 loc) · 1.74 KB
/
handshake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Research:
# https://stackoverflow.com/questions/26480854/3-way-handshake-in-scapy
# https://wiki.python.org/moin/TcpCommunication
# https://null-byte.wonderhowto.com/how-to/build-stealth-port-scanner-with-scapy-and-python-0164779/
# https://www.hackingarticles.in/nmap-scans-using-hex-value-flags/
# https://www.keycdn.com/support/tcp-flags
import sys
from scapy.layers.inet import *
from scapy.sendrecv import send
import argparse
from datetime import datetime
parser = argparse.ArgumentParser(description="Send a three-way TCP handshake.")
parser.add_argument(
"host",
nargs="?",
help="Target host ip address."
)
parser.add_argument(
"-p",
"--port",
default=80,
help="Port of host. probably 80."
)
arguments = parser.parse_args()
NULL = 0x00
FIN = 0x01
SYN = 0x02
RST = 0x04
PSH = 0x08
ACK = 0x10
SYNACK = 0x12
RSTACK = 0x14
URG = 0x20
ECE = 0x40
CWR = 0x80
NS = 0x100
if len(sys.argv) <= 1:
parser.print_help()
sys.exit()
startClock = datetime.now()
sourcePort = 80 # source port is static 80
host = arguments.host
hostPort = int(arguments.port)
conf.verb = 0 # Scapy verbosity level [1-3]
SYNACK_packet = sr1(IP(dst=host) / TCP(sport=sourcePort, dport=hostPort, flags="S")) # Send SYN packet
packetFlag = SYNACK_packet.getlayer(TCP).flags
if packetFlag == SYNACK:
print("Server responed with SYN, ACK")
ACK_packet = IP(dst=host) / TCP(sport=sourcePort, dport=hostPort, flags="A") # send ACK packet
response = send(ACK_packet)
# print(response.getlayer(TCP).flags)
elif packetFlag == RSTACK:
print("Server responed with RST, ACK")
elif packetFlag == RST:
print("Server responed with RST")
else:
print("connection timed out".format(hostPort))
print("Time elapsed {}".format(datetime.now() - startClock))