Skip to content

Instantly share code, notes, and snippets.

@thepacketgeek
Last active December 26, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thepacketgeek/7109130 to your computer and use it in GitHub Desktop.
Save thepacketgeek/7109130 to your computer and use it in GitHub Desktop.
TCP port scanner, 1 host for an array of specified ports
from scapy.all import *
import random
# Define end host and TCP port range
host = "www.facebook.com"
portRange = [22,23,80,443,3389]
# Send SYN with random Src Port for each Dst port
for dstPort in portRange:
srcPort = random.randint(1025,65534)
resp = sr1(IP(dst=host)/TCP(sport=srcPort,dport=dstPort,flags="S"),timeout=1,verbose=0)
if (str(type(resp)) == "<type 'NoneType'>"):
print host + ":" + str(dstPort) + " is filtered (silently dropped)."
elif(resp.haslayer(TCP)):
if(resp.getlayer(TCP).flags == 0x12):
send_rst = sr(IP(dst=host)/TCP(sport=srcPort,dport=dstPort,flags="R"),timeout=1,verbose=0)
print host + ":" + str(dstPort) + " is open."
elif (resp.getlayer(TCP).flags == 0x14):
print host + ":" + str(dstPort) + " is closed."
elif(resp.haslayer(ICMP)):
if(int(resp.getlayer(ICMP).type)==3 and int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
print host + ":" + str(dstPort) + " is filtered (silently dropped)."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment