EvilZone

Programming and Scripting => Scripting Languages => Topic started by: Traitor4000 on January 20, 2014, 02:11:44 PM

Title: Stumped
Post by: Traitor4000 on January 20, 2014, 02:11:44 PM
So I am trying to write a basic Port Scanner to learn some simple Python however i keep getting this error:
Code: [Select]
  File "ports.py", line 42
    def main():
    ^
IndentationError: unexpected unindent
It is probably something stupid but I can not find it, here is the script:

Code: [Select]
import socket
import os
import sys

portList = []
failed = 'banner grab failed'

def testConnection(ip, port):
    socket.setdefaulttimeout(10)
    s = socket.socket()
    try:
        s.connect((ip, port))
        portList.append(port)
        s.close()
        return()
    except Exception, e:
        print(e)

def checkVulns(banner):
    vulns = open('vulnlist','r')
    for line in vulns.readlines():
        if line.strip('\n') in banner:
            print'Vulnerable by: '+line.strip('\n')+'' + banner.strip('\n')


def grabbanner(ip, port):
    try:
        socket.setdefaulttimeout(20)
        s = socket.socket()
        s.connect((ip, port))
        try:
            banner = s.recv(2000)
            if banner == "":
                return('No banner on first pass')
            else:   
                return banner
        except Exception, e:
            print'Um well... '+ e
            return failed
           

def main():
    if len(sys.argv) == 2:
        ip = sys.argv[1]
    else:
        print'Improper usage, proper usage: python ports.py <target>'
        exit(0)

    for x in range(1, 4000):
        testConnection(ip, x)
    print portList

    for item in portList:
        banner = grabbanner(ip, item)
       
        print'Port '+str(item)+' Banner:'+ banner
        if banner != failed
            checkVulns(banner)


main()
Title: Re: Stumped
Post by: RedBullAddicted on January 20, 2014, 02:24:46 PM
IdentationError :P

Code: (Python) [Select]
def main():
    if len(sys.argv)
    ip = sys.argv[1]

Code: (Python) [Select]
def main():
    if len(sys.argv):
        ip = sys.argv[1]

See the difference? :) what are you trying to check with your if statement? You want to compare the length of sys.argv with what?
Title: Re: Stumped
Post by: Traitor4000 on January 20, 2014, 02:26:46 PM
Ha two errors i also forgot to insert the == 2 i must be really tired  :P
EDIT: made your indentation correction I still have the error, I posted the updated code above I think it might be a problem with the except or the else in grabbanner but I am not sure.
 
Title: Re: Stumped
Post by: Traitor4000 on January 20, 2014, 03:43:41 PM
Yeah fixed those in the if i forgot to add exit(0) in the else + I added a return statment to the except in grabbanner; however, same error except it has moved to line 42 because of lines i added.
Title: Re: Stumped
Post by: RedBullAddicted on January 20, 2014, 03:58:09 PM
You are still missing an except here:

Code: (Python) [Select]
def grabbanner(ip, port):
    try:
        socket.setdefaulttimeout(20)
        s = socket.socket()
        s.connect((ip, port))
        try:
            banner = s.recv(2000)
            if banner == "":
                return('No banner on first pass')
            else:   
                return banner
        except Exception, e:
            print'Um well... '+ e
            return failed
    except:
        print "socket error"
Title: Re: Stumped
Post by: Traitor4000 on January 20, 2014, 04:04:04 PM
Oh did not see that  :P
EDIT: Yeah it works i took this from a different program i wrote and thought i could just cut and paste it in but i forgot i had a try that i did not need there sorry...
 
Title: Re: Stumped
Post by: Phage on January 20, 2014, 06:05:24 PM
It's important to read any code from a previous project before you implement it into a new project ;)