0

Am newbie to python and stuck at a point. I want to create port scanner with using only python 3 inbuilt libraries (means avoiding scapy etc) I have following code :

import socket
for i in range(1,26): 
 s = socket.socket() 
 s.settimeout(0.5)
 ip = "74.207.244.221" #scanme.nmap.org
 response = s.connect_ex((ip, i)) 
 if response:
 print ("%d\tclose" %i)
 else:
 print ("%d\topen" %i)
 s.close()

Now I want to add 2 functionalities to this : that is

  1. Distinguish between close and filtered ports . In both cases am receiving same errno in return so how can I check if I have received back a rst packet or nothing ? As far as I have tried s.recv() isn't working for this.
  2. I want to control the number of tries (attempts), i.e I want to send only one or two syn packets. I don't want this program to send more than 2 syn packets for probes. How can this thing be achieved ?
asked Feb 20, 2013 at 4:36
1
  • I would expect that every existing port scanner will work much better than code written by a newbie with a reputation of 1. Commented Feb 20, 2013 at 5:24

2 Answers 2

1

Distinguish between close and filtered ports . In both cases am receiving same errno in return so how can I check if I have received back a rst packet or nothing

You've probably only checked with servers that send back a RST. Here's what I tried:

  • First case, normal config:

    >>> os.strerror(s.connect_ex((ip, 81)))
    'Connection refused'
    
  • Second, with manual iptables:

    iptables -A OUTPUT -p tcp --dport 81 -j DROP
    >>> os.strerror(s.connect_ex((ip, 81)))
    'Resource temporarily unavailable'
    

I want to control the number of tries (attempts), i.e I want to send only one or two syn packets.

I don't think there's a setsockopt TCP option exposed, but on linux there's:

net.ipv4.tcp_syn_retries

However, since you limited the timeout for the socket, all operations that don't finish within 0.5 seconds will time out. So it's likely only 1 or 2 SYNs will leave the station.

answered Feb 20, 2013 at 4:53
Sign up to request clarification or add additional context in comments.

Comments

0
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socekt.SOCK_STREAM)
host = 74.207.244.221
def portscan(port):
 try:
 s.connect((host,port))
 return True
 else:
 return False
for x in range(1,255):
 if portscan(x):
 print('Port',x,'Is Open')
answered Oct 27, 2017 at 18:11

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.