mez0
mez0

Reputation: 17

Scapy: AttributeError: 'NoneType' object has no attribute 'getlayer'

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!

Upvotes: 0

Views: 2813

Answers (1)

mez0
mez0

Reputation: 17

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)

Upvotes: 1

Related Questions