Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.71 KB | None | 0 0
  1.     # mark options that require arguments with :, and options that can optionally take arguments with ?
  2.     short_opts = "hHS?A?c:d:npliP:b:B:t:U:L:I:"
  3.     long_opts = [
  4.         "help",
  5.         "header",
  6.         "scanhelp?",
  7.         "attackhelp?",
  8.         "conc:",
  9.         "delay:",
  10.         "nonhtml",
  11.         "php",
  12.         "listable",
  13.         "ignored",
  14.         "proxy:",
  15.         "low_bound:",
  16.         "high_bound:",
  17.         "timeout:",
  18.         "user-agents:",
  19.         "listable_dict:",
  20.         "ignore_dict:"
  21.     ]
  22.  
  23.     # the below is a complicated procedure for mapping all passed args into positional args and
  24.     # option value pairs. you do not need to change it to add additional options or modify
  25.     # how options are handled once mapped, so DON'T fuck with it unless you know what you're
  26.     # doing or you will break it. if you just want to add more options, add them in the two
  27.     # variables short_opts and long_opts defined above. to change what happens with args once
  28.     # they're mapped, go to the next block down.
  29.     positional_args = []
  30.     option_value_pairs = []
  31.     mapped_flag = False # used to avoid handling args that were handled already by being passed to previous arg
  32.  
  33.     def get_passed(pos): # used to look for an arg that's been passed to an option
  34.         passed = None
  35.         if pos + 1 != len(args) and args[pos + 1][0] != "-":
  36.             passed = args[pos + 1]
  37.         return passed
  38.  
  39.     for argpos, arg in enumerate(args):
  40.         # if mapped flag is on, arg was handled already and all we need to do is reset the flag
  41.         if mapped_flag:
  42.             mapped_flag = False
  43.         # if arg is flagged handle as optional
  44.         elif arg[0] == '-':
  45.             # if arg flagged, we need to look for a matching option in short opts and long opts
  46.             # short opts may be passed in clusters, so valid options is created as a list
  47.             options = []
  48.  
  49.             # if long
  50.             if arg[:2] == '--':
  51.                 for l in long_opts:
  52.                     if arg[2:] == l.replace(":","").replace("?",""):
  53.                         options.append(l)
  54.                 if len(options) == 0:
  55.                     print("Invalid option: '%s'" % arg[2:])
  56.                     usage()
  57.  
  58.             # if short
  59.             else:
  60.                 for a in arg[1:]:
  61.                     if a in short_opts:
  62.                         i = short_opts.index(a)
  63.                         if i + 1 != len(short_opts) and short_opts[i + 1] in ":?":
  64.                             options.append(a + short_opts[i + 1])
  65.                         else:
  66.                             options.append(a)
  67.                     else:
  68.                         print("Invalid option: '%s'" % a)
  69.                         usage()
  70.            
  71.             # if we found valid options, procceed. If invalid options were found, we've already called usage()
  72.             for o in options:
  73.                 # if option may take argument, look for one to overwrite the None value
  74.                 value = None
  75.                 if o[-1] in ":?":
  76.                     v = get_passed(argpos)
  77.                     if v == None:
  78.                         if o[-1] == ":":
  79.                             print("Option '%s' requires an argument" % o.replace(":",""))
  80.                             usage()
  81.                     else:
  82.                         mapped_flag = True
  83.                 # now create the option value pairs
  84.                 if arg[:2] == "--":
  85.                     option_value_pairs.append([arg, value])
  86.                 else:
  87.                     option_value_pairs.append(["-" + o[0], value])
  88.  
  89.         # if not mapped and not option, append to positional args
  90.         else:
  91.             positional_args.append(arg)
  92.  
  93.            
  94.     # now we can handle our positional args and option/value pairs
  95.     if ("-h", "") in option_value_pairs or ("--help", "") in option_value_pairs:
  96.         usage()
  97.  
  98.     runtime_configs = default_configs
  99.     try:
  100.         for opt, arg in option_value_pairs:
  101.             if   opt in ["-H", "--header"]:
  102.                 runtime_configs["header"] = not runtime_configs["header"]
  103.             elif opt in ["-c", "--conc"]:
  104.                 runtime_configs["concurrent"] = int(arg)
  105.             elif opt in ["-d", "--delay"]:
  106.                 runtime_configs["delay"] = int(arg)
  107.             elif opt in ["-n", "--nonhtml"]:
  108.                 runtime_configs["scan_nonhtml"] = not runtime_configs["scan_nonhtml"]
  109.             elif opt in ["-p", "--php"]:
  110.                 runtime_configs["scan_php"] = not runtime_configs["scan_php"]
  111.             elif opt in ["-l", "--listable"]:
  112.                 runtime_configs["scan_listable"] = not runtime_configs["scan_listable"]
  113.             elif opt in ["-i", "--ignored"]:
  114.                 runtime_configs["scan_ignored"] = not runtime_configs["scan_ignored"]
  115.             elif opt in ["-P", "--proxy"]:
  116.                 runtime_configs["proxy"] = arg
  117.             elif opt in ["-b", "--lower_bound"]:
  118.                 runtime_configs["lower_status_code_bound"] = int(arg)
  119.             elif opt in ["-B", "--upper_bound"]:
  120.                 runtime_configs["upper_status_code_bound"] = int(arg)
  121.             elif opt in ["-t", "--timeout"]:
  122.                 runtime_configs["timeout"] = int(arg)
  123.             elif opt in ["-U", "--user-agents"]:
  124.                 runtime_configs["user-agents"] = arg
  125.             elif opt in ["-L", "--listable_dict"]:
  126.                 runtime_configs["listable_dict"] = arg
  127.             elif opt in ["-I", "--ignore_dict"]:
  128.                 runtime_configs["ignore_dict"] = arg
  129.             else:
  130.                 print("warning: unhandled option %s" % opt)
  131.     except ValueError:
  132.         print("option %s requires an integer\n" % opt)
  133.         usage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement