0

So, I wrote a portscanner which worked wonderfully. It outputted everything I wanted. But at some point, I've broken it and I can't identify how I broke it.

The error I'm getting is:

line 12, in portscan
    if(tcp_connect.getlayer(TCP).flags == SYNACK):
AttributeError: 'NoneType' object has no attribute 'getlayer'
[Finished in 4.4s with exit code 1]

Here is the script:

#!/usr/bin/env python3
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *

def portscan(host,dst_port):
    src_port = RandShort()
    SYNACK = 0x12
    RSTACK = 0x14
    tcp_connect = sr1(IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags="S"),verbose=0,timeout=2)

    if(tcp_connect.getlayer(TCP).flags == SYNACK):
        send_rst = sr(IP(dst=host)/TCP(sport=src_port,dport=dst_port,flags="AR"),verbose=0,timeout=2)
        print (dst_port,"is open")

    elif (tcp_connect.getlayer(TCP).flags == RSTACK):
        print (dst_port,"is closed")

if __name__ == '__main__':
    host = '192.168.0.40'
    port = 80
    portscan(host,port)

I'm not sure what I've changed in order for me to break it. Any ideas would be appreciated!

7
  • Apparently tcp_connect is None. Have you done any debugging? Commented Jun 29, 2018 at 16:31
  • I'm trying right now, but I can't see what I've done wrong lol Commented Jun 29, 2018 at 16:37
  • The first step would be to find under what circumstance sr1 returns None. IS that one of your functions? Commented Jun 29, 2018 at 16:38
  • 1
    "If there is, no response a None value will be assigned instead when the timeout is reached.". None being returned means there was no response. You'll need to check for that. Side Note: Scapy has to have some of the worst docs I've seen. I can't even find a function-by-function breakdown. Commented Jun 29, 2018 at 16:42
  • Would that be best done with a try catch? if(str(type(tcp_connect_scan_resp))=="<type 'NoneType'>"): print ('dead') This is a method I've seen online but it doesn't seem to work for me yet Commented Jun 29, 2018 at 16:44

1 Answer 1

1

Carcigenicate pointed out:

If there is no response a None value will be assigned instead when the timeout is reached. None being returned means there was no response. You'll need to check for that . . .

The solution:

if tcp_connect == None: 
     (Handle Failure)
else: 
     (Handle success)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.