Board logo

标题: [问题求助] VBS如何以二进制形式输出一个exe文件 [打印本页]

作者: wskwfkbdn    时间: 2016-8-28 11:17     标题: VBS如何以二进制形式输出一个exe文件

VBS如何以二进制形式输出一个exe文件
求教
目的是不想附带一个第三方命令行程序,直接让vbs脚本写出exe文件。
作者: CrLf    时间: 2016-8-28 11:52

http://www.bathome.net/viewthread.php?tid=4435

[拓展]批处理的另一种解决方案:
http://www.bathome.net/thread-12349-1-1.html
作者: wskwfkbdn    时间: 2016-8-28 11:56

回复 2# CrLf


    感谢
作者: codegay    时间: 2016-8-28 13:40

本帖最后由 codegay 于 2016-8-28 16:54 编辑

编码后再解码,比如用base64编码。是更简单通用的玩法吧?
x64的系统已经不带debug了
作者: wskwfkbdn    时间: 2016-8-28 13:51

回复 4# codegay


    能不能给个代码
作者: codegay    时间: 2016-8-28 14:11

回复 5# wskwfkbdn


    我又不玩VBS。
不过道理上相通。
base64的编码方法在很多语言里都有标准库。

#以下python3 代码是把wget.exe 的二进制流经过base64编码,然后再把base64编码的字符串还原成xxx.exe,测试还能运行。
  1. """
  2. python base64 模块学习
  3. #2016年8月28日 13:50:30 codegay
  4. """
  5. import base64
  6. with open("wget.exe","rb") as f:
  7.     txt=base64.encodebytes(f.read())
  8. with open("xxx.exe","wb") as f:
  9.     f.write(base64.standard_b64decode(txt.decode()))
复制代码

作者: codegay    时间: 2016-8-28 15:50

python3

我把sleep.exe 编码成了字符串。
sleep.exe 是一个第三方命令行工具,用来延迟的。

运行以下python代码可以还原得到resleep.exe
  1. import base64
  2. ss = """TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  3. AAAA4AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1v
  4. ZGUuDQ0KJAAAAAAAAAD+ls8EuvehV7r3oVe696FXOf+uV7v3oVe696BXrvehVzn//Fe/96FXNP/+
  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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  85. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  86. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  87. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  88. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  89. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  90. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  91. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="""
  92. with open("resleep.exe","wb") as f:
  93.     f.write(base64.standard_b64decode(ss))
复制代码

作者: codegay    时间: 2016-8-29 00:57

PowerShell 脚本中嵌入二进制文件
http://www.pstips.net/powershell ... in-your-script.html
作者: B魔方大人    时间: 2016-9-13 05:04

本帖最后由 B魔方大人 于 2016-9-13 05:11 编辑

base64处理后数据量会变大,事实上不如用原码,如果对编码方面有需求,当然base64是不错的!
如果是稍微大点的数据或许可以考虑LZ算法系列,不过由于解码过程抵消了一部分压缩量,所以处理稍微大点的数据比较合适!
作者: codegay    时间: 2016-9-13 08:01

回复 9# B魔方大人


   光给出一个“最NB的”解决方向,但是没有可行性或者很难实现,是没用的。

类似“坐直升机上班快”这种建议对大家真的一点用的都没有。因为都造不出直升机也买不起。
作者: B魔方大人    时间: 2016-9-13 08:13

回复 10# codegay


    呵呵,请认真看!
作者: more    时间: 2016-9-18 09:46     标题: RE: VBS如何以二进制形式输出一个exe文件

本帖最后由 more 于 2016-9-18 09:53 编辑

这是我以前写的一个还原铃声的代码,可以借鉴一下:
  1. Option Explicit
  2. '把保存为 TXT 的文件恢复为原文件
  3. Dim arrBit(), arrChr, objFso, objFile, objADODB, lngCnt, strFile, i
  4. Dim blnStart, strTmp
  5. blnStart = False
  6. lngCnt = 0
  7. Set objFso = CreateObject("Scripting.FileSystemObject")
  8. strFile = objFso.GetSpecialFolder(2) & "\不要用我的爱来伤害我.mp3" '文件还原到"临时文件夹"
  9. '如果已经存在指定文件(非第一次运行此脚本)则直接调用播放的过程
  10. If objFso.FileExists(strFile) Then
  11. Call PlaySong(strFile)
  12. Set objFso = Nothing
  13. WScript.Quit
  14. End If
  15. Set objFile = objFso.OpenTextFile(WScript.ScriptFullName, 1, False)
  16. Set objADODB = CreateObject("ADODB.Stream")
  17. '把文件内容赋值给数组
  18. Do Until objFile.AtEndOfStream
  19. If blnStart = True Then
  20. ReDim Preserve arrBit(lngCnt)
  21. strTmp = objFile.ReadLine
  22. arrBit(lngCnt) = Right(strTmp, Len(strTmp) - 1)
  23. lngCnt = lngCnt + 1
  24. Else
  25. If objFile.ReadLine = "'不要用我的爱来伤害我.mp3" Then blnStart = True
  26. End If
  27. Loop
  28. objFile.Close
  29. Set objFile = Nothing
  30. '还原文件
  31. lngCnt = lngCnt - 1
  32. ReDim arrChr(lngCnt \ 2)
  33. For i = 0 To lngCnt - 1 Step 2
  34. arrChr(i \ 2) = ChrW(arrBit(i + 1) * 256 + arrBit(i))
  35. Next
  36. If i = lngCnt Then arrChr(i \ 2) = ChrW(arrBit(i))
  37. arrChr = Join(arrChr, "")
  38. objADODB.Type = 1
  39. objADODB.Open
  40. With CreateObject("ADODB.Stream")
  41. .Type = 2
  42. .Open
  43. .Writetext arrChr
  44. .Position =  2
  45. .Copyto objADODB
  46. .Close
  47. End With
  48. objADODB.SaveToFile strFile, 2
  49. objADODB.Close
  50. Set objADODB = Nothing
  51. Set objFso = Nothing
  52. Call PlaySong(strFile)
  53. Sub PlaySong(strMusic)
  54. Dim i
  55. For i = 0 To 2 '播放三次
  56. With CreateObject("WMPlayer.ocx")
  57. .url = strMusic
  58. .controls.play
  59. Do Until .playstate = 1
  60. WScript.Sleep 500
  61. Loop
  62. End With
  63. Next
  64. End Sub
  65. '不要用我的爱来伤害我.mp3
  66. '255
  67. '250
  68. '179
复制代码
'由于 2M 的铃声文件(txt后缀)提示文件过大而无法上传,遗憾

'分隔符#######################
'这个函数可以把文件以二进制分解后存储为 txt 后缀
  1. Sub Backup(srcFile)
  2.     Dim objADODB, objFso, objFl, i, arrBit(0)
  3.     Set objADODB = CreateObject("ADODB.Stream")
  4.     Set objFso = CreateObject("Scripting.FileSystemObject")
  5.     Set objFl = objFso.OpenTextFile(srcFile & "_Back.txt", 2, True)
  6.     With objADODB
  7.         .Open
  8.         .Type = 1    'adTypeBinary = 1
  9.         .LoadFromFile srcFile
  10.         For i = 0 To .Size - 1
  11.             arrBit(0) = AscB(.Read(1))
  12.             objFl.WriteLine arrBit(0)
  13.         Next
  14.         .Close
  15.     End With
  16.     Set objADODB = Nothing
  17.     objFl.Close
  18.     Set objFl = Nothing
  19.     Set objFso = Nothing
  20. End Sub
复制代码

作者: yu2n    时间: 2016-9-18 12:43

参考:
VBS 二进制文件 与 Base64编码文本文件 互转工具
  1. Option Explicit
  2. Dim sTitle
  3. sTitle = "二进制文件 与 Base64编码文本文件 互转工具"
  4. If WScript.Arguments.Count = 0 Then
  5. MsgBox "使用方法:请将要格式化的文件拖动到这个文件上。", vbInformation, sTitle
  6. WScript.Quit
  7. End If
  8. Dim sReturn
  9. sReturn = Msgbox("请选择转化模式:" & vbCrLf & vbCrLf & _
  10.                 "   [ 是(Y) ]   二进制文件 转 Base64编码文本文件" & vbCrLf & _
  11.                 "   [ 否(N) ]   Base64编码文本文件 转 二进制文件 " & vbCrLf , _
  12.                 vbQuestion + vbYesNoCancel, sTitle)
  13.                
  14. If sReturn = vbCancel Then WScript.Quit
  15. Dim fso, wso
  16. Set fso = Createobject("Scripting.FileSystemObject")
  17. Set wso = CreateObject("WScript.Shell")
  18. Dim i
  19. For Each i In WScript.Arguments
  20.     If sReturn = vbYes Then BinaryToBase64 i, i & ".base64.txt"
  21.     If sReturn = vbNo  Then Base64ToBinary i, i & ".bin"
  22. Next
  23. wso.popup chr(13) + "文件格式化完成。    " + chr(13), 1, "提示", vbInformation
  24. WScript.Quit
  25. Function BinaryToBase64(ByVal BinaryFile, ByVal TextFile)
  26.     ' This script reads jpg picture named SuperPicture.jpg, converts it to base64
  27.     ' code using encoding abilities of MSXml2.DOMDocument object and saves
  28.     ' the resulting data to encoded.txt file
  29. ' http://stackoverflow.com/questions/496751/base64-encode-string-in-vbscript
  30.     'Option Explicit
  31.     Const fsDoOverwrite     = true  ' Overwrite file with base64 code
  32.     Const fsAsASCII         = false ' Create base64 code file as ASCII file
  33.     Const adTypeBinary      = 1     ' Binary file is encoded
  34.     ' Variables for writing base64 code to file
  35.     Dim objFSO
  36.     Dim objFileOut
  37.     ' Variables for encoding
  38.     Dim objXML
  39.     Dim objDocElem
  40.     ' Variable for reading binary picture
  41.     Dim objStream
  42.     ' Open data stream from picture
  43.     Set objStream = CreateObject("ADODB.Stream")
  44.     objStream.Type = adTypeBinary
  45.     objStream.Open()
  46.     'objStream.LoadFromFile("SuperPicture.jpg")
  47.     objStream.LoadFromFile(BinaryFile)
  48.     ' Create XML Document object and root node
  49.     ' that will contain the data
  50.     Set objXML = CreateObject("MSXml2.DOMDocument")
  51.     Set objDocElem = objXML.createElement("Base64Data")
  52.     objDocElem.dataType = "bin.base64"
  53.     ' Set binary value
  54.     objDocElem.nodeTypedValue = objStream.Read()
  55.     ' Open data stream to base64 code file
  56.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  57.     'Set objFileOut = objFSO.CreateTextFile("encoded.txt", fsDoOverwrite, fsAsASCII)
  58.     Set objFileOut = objFSO.CreateTextFile(TextFile, fsDoOverwrite, fsAsASCII)
  59.     ' Get base64 value and write to file
  60.     objFileOut.Write objDocElem.text
  61.     objFileOut.Close()
  62.     ' Clean all
  63.     Set objFSO = Nothing
  64.     Set objFileOut = Nothing
  65.     Set objXML = Nothing
  66.     Set objDocElem = Nothing
  67.     Set objStream = Nothing
  68.    
  69. End Function
  70. Function Base64ToBinary(ByVal sTextFile, ByVal sBinaryFile)
  71.     Const foForReading          = 1 ' Open base 64 code file for reading
  72.     Const foAsASCII             = 0 ' Open base 64 code file as ASCII file
  73.     Const adSaveCreateOverWrite = 2 ' Mode for ADODB.Stream
  74.     Const adTypeBinary          = 1 ' Binary file is encoded
  75.     ' Variables for reading base64 code from file
  76.     Dim objFSO
  77.     Dim objFileIn
  78.     Dim objStreamIn
  79.     ' Variables for decoding
  80.     Dim objXML
  81.     Dim objDocElem
  82.     ' Variable for write binary picture
  83.     Dim objStream
  84.     ' Open data stream from base64 code filr
  85.     Set objFSO = CreateObject("Scripting.FileSystemObject")
  86.     'Set objFileIn   = objFSO.GetFile("encoded.txt")
  87.     Set objFileIn   = objFSO.GetFile(sTextFile)
  88.     Set objStreamIn = objFileIn.OpenAsTextStream(foForReading, foAsASCII)
  89.     ' Create XML Document object and root node
  90.     ' that will contain the data
  91.     Set objXML = CreateObject("MSXml2.DOMDocument")
  92.     Set objDocElem = objXML.createElement("Base64Data")
  93.     objDocElem.DataType = "bin.base64"
  94.     ' Set text value
  95.     objDocElem.text = objStreamIn.ReadAll()
  96.     ' Open data stream to picture file
  97.     Set objStream = CreateObject("ADODB.Stream")
  98.     objStream.Type = adTypeBinary
  99.     objStream.Open()
  100.     ' Get binary value and write to file
  101.     objStream.Write objDocElem.NodeTypedValue
  102.     'objStream.SaveToFile "SuperPicture.jpg", adSaveCreateOverWrite
  103.     objStream.SaveToFile sBinaryFile, adSaveCreateOverWrite
  104.     ' Clean all
  105.     Set objFSO = Nothing
  106.     Set objFileIn = Nothing
  107.     Set objStreamIn = Nothing
  108.     Set objXML = Nothing
  109.     Set objDocElem = Nothing
  110.     Set objStream = Nothing
  111.    
  112. End Function
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2