利用certutil实现在批处理中嵌入exe文件的方法(不依赖debug命令)
http://bbs.bathome.net/thread-41604-1-1.html
【BAT】利用Debug把文件放到批处理脚本中
原文地址:http://www.cn-dos.net/forum/viewthread.php?tid=46874- ::=====================================================
- :: code by freeants CMD@XP version:1.00
- ::=====================================================
- @echo off
- cd/d "%~dp0">nul
- if "%~z1" equ "" goto:usage
- if %~z1 gtr 65280 goto:NOT_SUPPORT
- setlocal ENABLEDELAYEDEXPANSION
- copy/b "%~1" $$$>nul
- set/a D_NUM=%~z1+255
- call:DEC_HEX %D_NUM% >nul
- echo @echo off>"%~n1.cmd"
- echo more +5 "%%~0"^|debug^&graftabl 936^>nul>>"%~n1.cmd"
- echo copy/b/y $tmp$ "%~nx1"^>nul>>"%~n1.cmd"
- echo del/a/f/q $tmp$>>"%~n1.cmd"
- echo goto:eof>>"%~n1.cmd"
- for /f "eol=- skip=1 tokens=*" %%i in ('^(echo d100,%FILE_SIZE%^&echo q^)^|debug $$$^&^&del $$$') do (
- set op=%%i
- set op=!op:-= !
- echo e!op:~5,53! 2>nul>>"%~n1.cmd"
- )
- echo rcx>>"%~n1.cmd"
- call:DEC_HEX %~z1 >nul
- echo %FILE_SIZE%>>"%~n1.cmd"
- echo n$tmp$>>"%~n1.cmd"
- echo w>>"%~n1.cmd"
- echo q>>"%~n1.cmd"
- goto :EOF
-
- :DEC_HEX
- set FILE_SIZE=
- set PATTERN=123456789ABCDEF
- set dec=%1
- :WHILE
- set /a res=%dec%%%16-1
- set /a dec/=16
- set res=!PATTERN:~%res%,1!
- set FILE_SIZE=%res%%FILE_SIZE%
- if "%dec%" neq "0" goto WHILE
- goto:eof
-
- :usage
- echo.================================================
- echo 你没有指定要处理的文件或指定的文件不存在……
- echo.
- echo 用法:
- echo %~n0 fileName
- echo.================================================
- goto:eof
-
- :NOT_SUPPORT
- echo 指定的文件过大,脚本(%~nx0)不能处理……
- pause
- goto:eof
复制代码 【VBS】any2bat.vbs
原文地址:http://www.cn-dos.net/forum/viewthread.php?tid=36797- 'any2bat {s11ss 2008-1-5}
-
- '获取要转换的文件:
- If Not WScript.Arguments.Count=1 Then WScript.Quit
- Set fso=CreateObject("scripting.filesystemobject")
- Set f=fso.GetFile(WScript.Arguments(0))
- If Err Then WScript.Quit
- If f.Size=0 Then WScript.Quit
-
- '获取文件2进制代码:
- Set stream=CreateObject("adodb.stream")
- With stream
- .Type=1:.Open:.LoadFromFile f
- End With
- bin=stream.Read(f.Size)
-
- '生成bat:
- n=&h100:str=""
- Set bat=fso.CreateTextFile(f.Path&".bat")
- bat.WriteLine "@more <""%~f0"" +1|debug>nul&&move t """&f.Name&"""&goto :eof"
- For i=1 To Lenb(bin)
- zero=""
- one=Ascb(Midb(bin,i,1))
- If one<16 Then zero="0"
- str=str&" "&zero& Hex(one)
- If i Mod 16=0 Then bat.WriteLine "e"& Hex(n)&str:str="":n=n+16
- Next
- If Not str="" Then bat.WriteLine "e"& Hex(n)&str
- bat.WriteLine "n t"
- bat.WriteLine "rcx"
- bat.WriteLine Hex(f.Size)
- bat.WriteLine "w"
- bat.WriteLine "q"
-
- stream.Close:bat.Close
- Set stream=Nothing:Set f=Nothing:Set bat=Nothing:Set fso=Nothing
-
- WScript.Echo "Successful!"
复制代码 在与knoppix7兄的探讨中,知道了bx的作用,特作修改使待转换的文件的大小“理论上”无限制(因为处理64KB以上的文件还是比较慢):- 'any2bat {s11ss 2008-1-6}
-
- '获取要转换的文件:
- If Not WScript.Arguments.Count=1 Then WScript.Echo "把要转换的文件托拽到我身上!":WScript.Quit
- Set fso=CreateObject("scripting.filesystemobject")
- Set f=fso.GetFile(WScript.Arguments(0))
- If Err Then WScript.Quit
- If f.Size=0 Then WScript.Quit
-
- '获取文件2进制代码:
- Set stream=CreateObject("adodb.stream")
- With stream
- .Type=1:.Open:.LoadFromFile f
- End With
- bin=stream.Read(f.Size)
-
- '生成bat:
- n=&h100:str=""
- Set bat=fso.CreateTextFile(f.Path&".bat")
- bat.WriteLine "@more <""%~f0"" +1|debug>nul&&move t """&f.Name&"""&goto :eof"
- For i=1 To Lenb(bin)
- zero=""
- one=Ascb(Midb(bin,i,1))
- If one<16 Then zero="0"
- str=str&" "&zero& Hex(one)
- If i Mod 16=0 Then bat.WriteLine "e"& Hex(n)&str:str="":n=n+16
- Next
- If Not str="" Then bat.WriteLine "e"& Hex(n)&str
- size=Hex(f.Size):l=Len(size)-4
- If l<=0 Then bx="0":cx=size
- If l>0 Then bx=Left(size,l):cx=Right(size,4)
- bat.WriteLine "n t"
- bat.WriteLine "rbx"
- bat.WriteLine bx
- bat.WriteLine "rcx"
- bat.WriteLine cx
- bat.WriteLine "w"
- bat.WriteLine "q"
-
- stream.Close:bat.Close
- Set stream=Nothing:Set f=Nothing:Set bat=Nothing:Set fso=Nothing
-
- WScript.Echo "Successful!"
复制代码 在与knoppix7兄的探讨中,又发现由于debug的e命令的限制使上面的代码不能正确还原成原文件。特改变方案,采用copy/b的方式,可对64KB以上的文件完全还原。大家可用"%ALLUSERSPROFILE%\Documents\My Pictures\示例图片\Sunset.jpg"实验一下(可用fc/b来比较原文件和还原出来的文件)。
修改后的代码:(感谢zh159兄的建议)- 'any2bat {s11ss 2008-1-17}
-
- '获取要转换的文件:
- If Not WScript.Arguments.Count=1 Then WScript.Echo "将要转换的文件托拽到我身上!":WScript.Quit
- Set fso=CreateObject("scripting.filesystemobject")
- Set f=fso.GetFile(WScript.Arguments(0))
- If Err Then WScript.Quit
- If f.Size=0 Then WScript.Quit
-
- '获取文件2进制代码:
- Set stream=CreateObject("adodb.stream")
- With stream
- .Type=1:.Open:.LoadFromFile f
- End With
- bin=stream.Read(f.Size)
-
- '生成bat:
- cr=vbCrLf:n=&h100:line=1:seg=1:segeof=false:str=""
- Set bat=fso.CreateTextFile(f.Path&".bat")
- bat.WriteLine "@md %tmp%\t1.17&&pushd %tmp%\t1.17&&more <""%~f0"" +1|debug>nul&&popd&©/b %tmp%\t1.17\t* """&f.Name&"""&&rd/s/q %tmp%\t1.17&goto :eof"
- For i=1 To Lenb(bin)
- zero="":segeof=false:one=Ascb(Midb(bin,i,1))
- If one<16 Then zero="0"
- str=str&" "&zero& Hex(one)
- If i Mod 16=0 Then bat.WriteLine "e"& Hex(n)&str:str="":n=n+16:line=line+1
- If line Mod 4081=0 Then bat.WriteLine "n t"&seg&cr&"rcx"&cr&"ff00"&cr&"w":seg=seg+1:n=&h100:line=1:segeof=true
- Next
- If Not str="" Then bat.WriteLine "e"& Hex(n)&str
- If Not segeof Then bat.WriteLine "n t"&seg&cr&"rcx"&cr& Hex(f.Size-(seg-1)*65280)&cr&"w"&cr&"q"
-
- stream.Close:bat.Close
- Set stream=Nothing:Set f=Nothing:Set bat=Nothing:Set fso=Nothing
-
- WScript.Echo "Successful!"
复制代码
|