Domanda

I've been trying to run this little port scanner-ish program for a while and I still don't know why it's giving me this error: [EDIT: I renamed the IP string into IPadd since it might have been confusing the two edited and now it says this error]

File "thing.py", line 63, in portScan
    if (str(type(fin_scan_resp))=="<type 'NoneType'>"):
TypeError: 'int' object is not callable

this is the code:

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

from socket import *
import urllib2
import sys


def portScan(target):
    validate = 0

    print("Simple Port Scanner v1.0")
    print("Available port scans")
    print("[1] TCP Connect")
    print("[2] SYN")
    print("[3] ACK")
    print("[4] FIN")
    #print("[5] XMAS")

    print("\n COMMON PORTS: 20, 21, 23, 80")
    getport = raw_input("What port will you scan?: ")
    port = int(getport)

    while validate != 1:    
        type = input("What kind of scan do you want to do?: ")
        print "Selected", type
        validate = 1        
        try:            
            IPadd = gethostbyname(target)
            print(IP) #trace
        except:
            print("ERROR: Cannot resolve connection... Exiting program")


        if type == 1:
            tcpconn = socket(AF_INET, SOCK_STREAM)
            tcpconn.settimeout(0.255)

            #for port in range(20, 25):
            isopen = tcpconn.connect_ex((IPadd, port))
            if isopen == 0:
                print ("TCP Connect: Port " + getport + " is Open")
            else:
                print ("TCP Connect: Port " + getport + " is Closed")
            tcpconn.close()

        elif type == 2:
            print ("SYN")
            synconn = socket(AF_INET, SOCK_STREAM)
            synconn.settimeout(0.255)


        elif type == 3:
            print ("ACK")
        elif type == 4:
            dst_ip = IPadd
            src_port = RandShort()
            dst_port= port

            fin_scan_resp = sr1(IP(dst=dst_ip)/TCP(dport=dst_port,flags="F"),timeout=10)
            if (str(type(fin_scan_resp))=="<type 'NoneType'>"):
                print "Open|Filtered"
            elif(fin_scan_resp.haslayer(TCP)):
                if(fin_scan_resp.getlayer(TCP).flags == 0x14):
                    print "Closed"
                elif(fin_scan_resp.haslayer(ICMP)):
                    if(int(fin_scan_resp.getlayer(ICMP).type)==3 and int(fin_scan_resp.getlayer(ICMP).code) in [1,2,3,9,10,13]):
                        print "Filtered"
            print ("FIN")
        else:
            print("Invalid input")
            validate = 0



def getTarget():
    target = raw_input("Enter target Host name/IP Address: ")
    '''
    #Validation of ip address still not working 
    #chk = socket(AF_INET, SOCK_STREAM)
    try:
            socket.inet_aton(target)
    except socket.error:
            print("IP address is invalid. QUITTING")
    chk.close()
    '''
    return target   

def main():
    validate = 0
    print("Launch which scan?")
    print("[1] Simple Port Scanner v1.0")
    print("[2] Service Fingerprinting v1.0")
    print("[3] OS Fingerprinting v1.0")
    print("[x] Exit")

    while validate != 1:
        firstMenu = raw_input("Choice: ")
        if firstMenu == "1":
            print("\n")
            validate = 1
            name = getTarget()
            portScan(name)
        elif firstMenu == "2":
            print("yep")
            validate = 1
        elif firstMenu == "3":
            print("this")
            validate = 1
        elif firstMenu == "x":
            print("Closing...")
            validate = 1
        else:
            print("Invalid choice")

main()

That part where there is supposed to be some error runs fine when I run that part on another .py file so I don't understand what's causing this and it's just frustrating

È stato utile?

Soluzione

You are assigning a string to IP:

try:            
    IP = gethostbyname(target)
    print(IP) #trace

but you are also trying to use the scapy IP() object:

fin_scan_resp = sr1(IP(dst=dst_ip)/TCP(dport=dst_port,flags="F"),timeout=10)

The string masks the object. Rename the string to ip (lowercase), everywhere in the portScan() function:

try:            
    ip = gethostbyname(target)
    print(ip) #trace

# ...

#for port in range(20, 25):
isopen = tcpconn.connect_ex((ip, port))

# ...

elif type == 4:
    dst_ip = ip

Instead of the rather ridiculous line:

if (str(type(fin_scan_resp))=="<type 'NoneType'>"):

use:

if fin_scan_resp is None:

although you really should not use type as a local variable as it masks the built-in function.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top