Python - How To Create A Port Scanner TCP SYN Using The Method (TCP SYN) - Stack Overflow
Python - How To Create A Port Scanner TCP SYN Using The Method (TCP SYN) - Stack Overflow
How to Create a port scanner TCP SYN using the method (TCP SYN
)?
Asked
4 years, 9 months ago Active
4 years, 1 month ago Viewed
9k times
#####################################
# Portscan TCP #
-1 # #
#####################################
#!/usr/bin/python3
import socket
ports = []
count = 0
count += 1
client.settimeout(0.05)
if code == 0: #0 = Success
else:
The python script above is a TCP Scanning. How can I change it into a TCP SYN scanning ?
How to Create a port scanner TCP SYN using the method (TCP SYN ) ?
python python-3.x
2 Um, that's a bit different than just trying to connect. You will have to come with an approach of your
own and ask a precise question!
– Marcus Müller
Aug 18 '16 at 18:09
As @Upsampled mentioned you might use raw sockets (https://en wikipedia org/) as you only
As @Upsampled mentioned, you might use raw sockets (https://en.wikipedia.org/) as you only
need a subset of TCP protocol (send SYN and recieve RST-ACK or SYN-ACK
).
Here's the code sample that already implements a simple port scanner
http://pastebin.com/YCR3vp9B and a detailed article on what it does:
http://null-
byte.wonderhowto.com/how-to/build-stealth-port-scanner-with-scapy-and-python-0164779/
The code is a little bit ugly but it works — I've checked it from my local Ubuntu PC against my
VPS. Here's the most important code snippet (slightly adjusted to conform to PEP8):
srcport = RandShort()
SYNACKpkt = sr1(IP(dst=target) /
pktflags = SYNACKpkt.getlayer(TCP).flags
if pktflags == SYNACK:
# port is open
pass
else:
# ...
pass
Share Improve this answer Follow edited Aug 23 '16 at 6:59 answered Aug 23 '16 at 6:53
ffeast
8,830 23 34
First, you will have to generate your own SYN packets using RAW sockets. You can find an
example here
5
Second, you will need to listen for SYN-ACKs from the scanned host in order to determine
which ports actually try to start the TCP Handshake (SYN,SYN-ACK,ACK). You should be able
+25 to detect and parse the TCP header from the applications that respond. From that header you
can determine the origin port and thus figure out a listening application was there.
Also if you implement this, you also basically made a SYN DDOS utility because you will be
creating a ton of half-opened tcp connections.