Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python code will not compile

  • 04-12-2015 1:08pm
    #1
    Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭


    The below code is for a port scanner. (looking to get into penetration testing/ computer security area of work)

    I'm using python 2.7.10 in the PyCharm IDE. The error upon running the script is below the code....

    Thanks.
    import optparse
    from socket import *
    from threading import *
    
    screenLock = Semaphore(value=1)
    
    
    def connScan(tgtHost, tgtPort):
        try:
            connSkt = socket(AF_INET, SOCK_STREAM)
            connSkt.connect((tgtHost, tgtPort))
            connSkt.send('ViolentPython\r\n')
            results = connSkt.recv(100)
            screenLock.acquire()
            print
            '[+]%d/tcp open' % tgtPort
            print
            '[+] ' + str(results)
        except:
            screenLock.acquire()
            print
            '[-]%d/tcp closed' % tgtPort
        finally:
            screenLock.release()
            connSkt.close()
    
    
    def portScan(tgtHost, tgtPorts):
        try:
            tgtIP = gethostbyname(tgtHost)
        except:
            print
            "[-] Cannot resolve '%s': Unknown host" % tgtHost
            return
    
        try:
            tgtName = gethostbyaddr(tgtIP)
            print
            '\n[+] Scan Results for: ' + tgtName[0]
        except:
            print
            '\n[+] Scan Results for: ' + tgtIP
        setdefaulttimeout(1)
        for tgtPort in tgtPorts:
            t = Thread(target=connScan, args=(tgtHost, int(tgtPort)))
            t.start()
    
    
    def main():
    
    
        parser = optparse.OptionParser('usage%prog ' + \
                                       '-H <target host> -p <target port>')
    parser.add_option('-H', dest='tgtHost', type='string', \
                      help='specify target host')
    parser.add_option('-p', dest='tgtPort', type='string', \
                      help='specify target port[s] separated by comma')
    (options, args) = parser.parse_args()
    tgtHost = options.tgtHost
    tgtPorts = str(options.tgtPort).split(', ')
    if (tgtHost == None) | (tgtPorts[0] == None):
        print
        parser.usage
        exit(0)
    portScan(tgtHost, tgtPorts)
    if __name__ == "__main__":
        main()
    
    

    If I try to run portscan.py (below) it errors with invalid syntax.
    >>>portscan.py -H 127.0.0.1 -p 21
    
      File "<input>", line 1
        portscan.py -H 127.0.0.1 -p 21
                           ^
    SyntaxError: invalid syntax
    


Comments

  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    It may just be how it boards is formatting your post but from the 2nd line onwards of your main function, the text is not indented correctly. Python is white space sensitive, so this needs to be correct.

    Use Code->Reformat Code menu to correct this in PyCharm

    Have you tried using the debugger in PyCharm to see what's going on when you hit the syntax error.

    BTW, python is interpreted not compiled :)


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    What should it do?


    I don;t get any errors once I fixed the formatting.


  • Registered Users, Registered Users 2 Posts: 6,289 ✭✭✭Talisman


    What should it do?


    I don;t get any errors once I fixed the formatting.
    I bet it doesn't run properly though.


  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    Talisman wrote: »
    I bet it doesn't run properly though.

    Works for me once the indentation issue is fixed:
    $ python2 scanner.py -H 127.0.0.1 -p 22
    
    [+] Scan Results for: localhost.localdomain
    [-]22/tcp closed
    
    $ sudo systemctl start sshd
    $ python2 scanner.py -H 127.0.0.1 -p 22
    
    [+] Scan Results for: localhost.localdomain
    [+]22/tcp open
    [+] SSH-2.0-OpenSSH_7.1
    


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    deconduo wrote: »
    Works for me once the indentation issue is fixed:
    $ python2 scanner.py -H 127.0.0.1 -p 22
    
    [+] Scan Results for: localhost.localdomain
    [-]22/tcp closed
    
    $ sudo systemctl start sshd
    $ python2 scanner.py -H 127.0.0.1 -p 22
    
    [+] Scan Results for: localhost.localdomain
    [+]22/tcp open
    [+] SSH-2.0-OpenSSH_7.1
    

    What system and IDE are you using?


  • Advertisement
  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    euser1984 wrote: »
    What system and IDE are you using?

    Archlinux, no IDE just vim. Here's a pastebin with fixed formatting:

    http://pastebin.com/0j9urS2C


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    deconduo wrote: »
    Archlinux, no IDE just vim. Here's a pastebin with fixed formatting:

    http://pastebin.com/0j9urS2C

    Cool, I'm gonna set all this up on a Debian based system now....just incase I run into issues are you using the latest version of python2 and vim?

    Thanks.


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    deconduo wrote: »
    Archlinux, no IDE just vim. Here's a pastebin with fixed formatting:

    http://pastebin.com/0j9urS2C

    That ran in pyscripter for me there - brilliant....fair play to you; I've been scratching my head all day on that....I hope karma brings you luck....buy a scratchcard for yourself tomorrow :)


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    Can you try running it with two specified ports actually?

    The traceback is giving me this error....

    Traceback
    <module> C:\Python27\portscan2.py 56
    main C:\Python27\portscan2.py 53
    portScan C:\Python27\portscan2.py 37
    ValueError: invalid literal for int() with base 10: '21,'


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    you are passing a comma as an int


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    you are passing a comma as an int


    hmmm, but the code is meant to cater for multiple specified ports....


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    It looks like it splits strings on a comma then a space, so like: 23, 34. Did you maybe write it like this 23,34? Not at a computer to check it out


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    It looks like it splits strings on a comma then a space, so like: 23, 34. Did you maybe write it like this 23,34? Not at a computer to check it out


    The problem is with the comma not being an integer I think, as specified on line 37 here... http://pastebin.com/0j9urS2C


  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    euser1984 wrote: »
    The problem is with the comma not being an integer I think, as specified on line 37 here... http://pastebin.com/0j9urS2C

    No its the parser. The args are split by spaces so this won't work:
    scanner.py -H 127.0.0.1 -p 21, 22
    

    Instead you need to do wrap the argument in quotes:
    scanner.py -H 127.0.0.1 -p '21, 22'
    


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    When I run it in the debugger it shows tgtPorts as having the variable 21, (after specifying 21, 80

    If I run within quotes as per above the variable just filles with "'21'" (that's double quotes wrapped around a single quote)


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    Actually if I set a breakpoint on line 50, it shows tgtPort as having a value of 21 (no comma or anything) - that's one problem....

    The split seems to be adding the comma on to that 21


  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    euser1984 wrote: »
    Actually if I set a breakpoint on line 50, it shows tgtPort as having a value of 21 (no comma or anything) - that's one problem....

    The split seems to be adding the comma on to that 21

    It works fine for me:
    $ python2 scanner.py -H localhost -p '21, 22, 23, 24'
    
    [+] Scan Results for: localhost.localdomain
    [-]21/tcp closed
    [-]23/tcp closed
    [-]22/tcp closed
    [-]24/tcp closed
    
    


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    Just to be sure I copied that pastebin entry into a new file with pyscript and running with python2.7 and the following command line entry
    newmodule.py -H google.ie -p '21 22 23 24'
    

    It fails with a traceback error of:
    <module>	<module1>	56		
        main	<module1>	53		
        portScan	<module1>	37		
    "ValueError: invalid literal for int() with base 10: ""'21"""
    

    It is now populating the variable with "21" as it's picking up both single quotes from the command line...... I'm thinking the split isn't working right, but the documentation is difficult to discern for a beginner.....


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    Code is working fine. You have to use a comma and space to separate ports. type of quotes is irrelevant. This code shows you what to split on:
    tgtPorts = str(options.tgtPort).split(', ')
    

    If you wanted your code above to work it would be:
    tgtPorts = str(options.tgtPort).split(' ')
    

    This works fine with the original code:
    python testport.py  -H 127.0.0.1 -p '21, 22, 23, 24'
    


    I think you should go through the code and figure out what it means before using it further, will help you out.


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    Code is working fine. You have to use a comma and space to separate ports. type of quotes is irrelevant. This code shows you what to split on:
    tgtPorts = str(options.tgtPort).split(', ')
    

    If you wanted your code above to work it would be:
    tgtPorts = str(options.tgtPort).split(' ')
    

    This works fine with the original code:
    python testport.py  -H 127.0.0.1 -p '21, 22, 23, 24'
    


    I think you should go through the code and figure out what it means before using it further, will help you out.

    I am learning by a "reverse engineering" type method, but the documentation is difficult to understand for the parser....what version of python are you using?

    I'm using python 2.7.9 with ninja ide on a debian based system and 2.7.1 on windows with pycharm community edition....

    I get the same issue on each system....


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 Mod ✭✭✭✭Tar.Aldarion


    I am using 2.7.6, ubuntu, a text document and the command line and there is no problem with the code.


    What error do you get running

    python testport.py -H 127.0.0.1 -p '21, 22, 23, 24'


  • Registered Users, Registered Users 2 Posts: 1,977 ✭✭✭euser1984


    I am using 2.7.6, ubuntu, a text document and the command line and there is no problem with the code.


    What error do you get running

    python testport.py -H 127.0.0.1 -p '21, 22, 23, 24'


    Just copied it into vim saved it out as a .py file, ran it from the command line and it worked....


Advertisement