Author Topic: Stumped  (Read 962 times)

0 Members and 1 Guest are viewing this topic.

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Stumped
« 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()
« Last Edit: January 20, 2014, 03:41:57 pm by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline RedBullAddicted

  • Moderator
  • Sir
  • *
  • Posts: 519
  • Cookies: 189
    • View Profile
Re: Stumped
« Reply #1 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?
Deep into that darkness peering, long I stood there, wondering, fearing, doubting, dreaming dreams no mortal ever dared to dream before. - Edgar Allan Poe

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: Stumped
« Reply #2 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.
 
« Last Edit: January 20, 2014, 02:33:26 pm by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: Stumped
« Reply #3 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.
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline RedBullAddicted

  • Moderator
  • Sir
  • *
  • Posts: 519
  • Cookies: 189
    • View Profile
Re: Stumped
« Reply #4 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"
Deep into that darkness peering, long I stood there, wondering, fearing, doubting, dreaming dreams no mortal ever dared to dream before. - Edgar Allan Poe

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: Stumped
« Reply #5 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...
 
« Last Edit: January 20, 2014, 04:06:58 pm by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline Phage

  • VIP
  • Overlord
  • *
  • Posts: 1280
  • Cookies: 120
    • View Profile
Re: Stumped
« Reply #6 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 ;)
"Ruby devs do, in fact, get all the girls. No girl wants a python, but EVERY girl wants rubies" - connection

"It always takes longer than you expect, even when you take into account Hofstadter’s Law."