FlyFar

exe2bat.py

Mar 23rd, 2023
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | Cybersecurity | 0 0
  1. # Very poorly coded exe (or other file) to batch converter.
  2. #
  3. # If you want to use it, don't forget to append -zip to the command line,
  4. # because this is the best method for packing the file.
  5.  
  6. import sys, os, base64, zipfile
  7.  
  8. use_zip = "-zip" in sys.argv
  9. for_xp = "-xp" in sys.argv
  10. use_script = "-zip" in sys.argv or "-xp" in sys.argv
  11.  
  12. ZIP_NAME = "z.zip"
  13. EXE_NAME = sys.argv[3]
  14. BASE64_NAME = "x"
  15. JS_NAME = "x.js"
  16.  
  17. def writeScript(script, path):
  18.     out = ""
  19.     i = 0
  20.    
  21.     for line in script.splitlines():
  22.         if i == 0:
  23.             out += 'echo %s>%s\r\n' % (batchescape(line), path)
  24.         else:
  25.             out += 'echo %s>>%s\r\n' % (batchescape(line), path)
  26.        
  27.         i+=1
  28.        
  29.     return out
  30.    
  31. def batchescape(s):
  32.     chrs = '<>|"&'
  33.    
  34.     for c in chrs:
  35.         s = s.replace(c, "^"+c)
  36.        
  37.     return s#.replace("%", "%%")
  38.  
  39. out = "@echo off\r\n\r\n"
  40.  
  41. fn = sys.argv[1]
  42.  
  43. if use_zip:
  44.     with zipfile.ZipFile("temp.zip", "w") as z:
  45.         z.write(sys.argv[1], EXE_NAME, zipfile.ZIP_DEFLATED)
  46.    
  47.     fn = "temp.zip"
  48.  
  49. ifile = open(fn, "rb")
  50. inp = ifile.read()
  51. ifile.close()
  52.  
  53. b64 = base64.encodestring(inp)
  54. out += writeScript(b64, BASE64_NAME)
  55.  
  56. if use_zip:
  57.     os.remove("temp.zip")
  58.    
  59. if use_script:
  60.     out += "\r\n"
  61.    
  62. if use_zip:
  63.     js = """f=new ActiveXObject("Scripting.FileSystemObject");i=f.getFile("%s").openAsTextStream();
  64. x=new ActiveXObject("MSXml2.DOMDocument").createElement("Base64Data");x.dataType="bin.base64";
  65. x.text=i.readAll();o=new ActiveXObject("ADODB.Stream");o.type=1;o.open();o.write(x.nodeTypedValue);
  66. z=f.getAbsolutePathName("%s");o.saveToFile(z);s=new ActiveXObject("Shell.Application");
  67. s.namespace(26).copyHere(s.namespace(z).items());o.close();i.close();""" % (BASE64_NAME, ZIP_NAME)
  68.  
  69.     out += writeScript(js, JS_NAME)
  70. elif for_xp:
  71.     js = """i=WScript.createObject("Scripting.FileSystemObject").getFile("%s").openAsTextStream();
  72. x=WScript.createObject("MSXml2.DOMDocument").createElement("Base64Data");x.dataType="bin.base64";
  73. x.text=i.readAll();o=WScript.createObject("ADODB.Stream");o.type=1;o.open();o.write(x.nodeTypedValue);
  74. o.saveToFile("%s");o.close();i.close();""" % (BASE64_NAME, EXE_NAME)
  75.  
  76.     out += writeScript(js, JS_NAME)
  77.  
  78. out += "\r\n"
  79.  
  80. out += 'set v="%%appdata%%\\%s"\r\n' % EXE_NAME
  81. out += "del %v% >NUL 2>NUL\r\n"
  82.  
  83. if use_script:
  84.     out += "cscript %s >NUL 2>NUL\r\n" % JS_NAME
  85.     out += "del %s >NUL 2>NUL\r\n" % JS_NAME
  86. else:
  87.     out += "certutil -decode %s %%v%% >NUL 2>NUL\r\n" % BASE64_NAME
  88.  
  89. if for_xp:
  90.     out += "move %s %%v%% >NUL 2>NUL\r\n" % EXE_NAME
  91.    
  92. if use_zip:
  93.     out += "del %s >NUL 2>NUL\r\n" % ZIP_NAME
  94.    
  95. out += "del %s >NUL 2>NUL\r\n" % BASE64_NAME
  96. out += 'start "" %v%'
  97.  
  98. ofile = open(sys.argv[2], "wb")
  99. ofile.write(out)
  100. ofile.close()
  101.  
  102. print len(out), "characters."
Add Comment
Please, Sign In to add comment