2016-01-04 13:30:11 +03:00
|
|
|
|
using Gee.External.Capstone;
|
|
|
|
|
using Gee.External.Capstone.X86;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using UnicornManaged;
|
|
|
|
|
using UnicornManaged.Const;
|
|
|
|
|
|
|
|
|
|
namespace UnicornSamples
|
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
internal static class X86Sample32
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private const long ADDRESS = 0x1000000;
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
|
|
|
|
public static void X86Code32()
|
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
byte[] X86_CODE32 =
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
|
|
|
|
// INC ecx; DEC edx
|
|
|
|
|
0x41, 0x4a
|
|
|
|
|
};
|
|
|
|
|
Run(X86_CODE32);
|
|
|
|
|
}
|
2022-10-14 17:57:46 +03:00
|
|
|
|
|
2016-01-04 13:30:11 +03:00
|
|
|
|
public static void X86Code32InvalidMemRead()
|
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
byte[] X86_CODE32_MEM_READ =
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
|
|
|
|
// mov ecx,[0xaaaaaaaa]; INC ecx; DEC edx
|
|
|
|
|
0x8B, 0x0D, 0xAA, 0xAA, 0xAA, 0xAA, 0x41, 0x4a
|
|
|
|
|
};
|
2018-07-05 16:30:33 +03:00
|
|
|
|
Run(X86_CODE32_MEM_READ);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-04 18:59:05 +03:00
|
|
|
|
public static void X86Code32InvalidMemWriteWithRuntimeFix()
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
byte[] X86_CODE32_MEM_WRITE =
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
|
|
|
|
// mov [0xaaaaaaaa], ecx; INC ecx; DEC edx
|
|
|
|
|
0x89, 0x0D, 0xAA, 0xAA, 0xAA, 0xAA, 0x41, 0x4a
|
|
|
|
|
};
|
2016-01-04 18:59:05 +03:00
|
|
|
|
Run(X86_CODE32_MEM_WRITE);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-04 18:59:05 +03:00
|
|
|
|
public static void X86Code32InOut()
|
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
byte[] X86_CODE32_INOUT =
|
2016-01-04 18:59:05 +03:00
|
|
|
|
{
|
|
|
|
|
// INC ecx; IN AL, 0x3f; DEC edx; OUT 0x46, AL; INC ebx
|
|
|
|
|
0x41, 0xE4, 0x3F, 0x4a, 0xE6, 0x46, 0x43
|
|
|
|
|
};
|
|
|
|
|
Run(X86_CODE32_INOUT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private static void Run(byte[] code, bool raiseException = false)
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
var stackTrace = new StackTrace();
|
|
|
|
|
var stackFrame = stackTrace.GetFrames()[1];
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var methodName = stackFrame.GetMethod()?.Name;
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine($"*** Start: {methodName}");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
Exception e = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-01-04 18:59:05 +03:00
|
|
|
|
RunTest(code, ADDRESS, Common.UC_MODE_32);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
}
|
|
|
|
|
catch (UnicornEngineException ex)
|
|
|
|
|
{
|
|
|
|
|
e = ex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!raiseException && e != null)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Emulation FAILED! " + e.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("*** End: " + methodName);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private static void RunTest(byte[] code, long address, int mode)
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
using var u = new Unicorn(Common.UC_ARCH_X86, mode);
|
|
|
|
|
using var disassembler = CapstoneDisassembler.CreateX86Disassembler(X86DisassembleMode.Bit32);
|
|
|
|
|
Console.WriteLine($"Unicorn version: {u.Version()}");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// map 2MB of memory for this emulation
|
|
|
|
|
u.MemMap(address, 2 * 1024 * 1024, Common.UC_PROT_ALL);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// initialize machine registers
|
|
|
|
|
u.RegWrite(X86.UC_X86_REG_EAX, 0x1234);
|
|
|
|
|
u.RegWrite(X86.UC_X86_REG_ECX, 0x1234);
|
|
|
|
|
u.RegWrite(X86.UC_X86_REG_EDX, 0x7890);
|
2016-01-04 18:59:05 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// write machine code to be emulated to memory
|
|
|
|
|
u.MemWrite(address, code);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// initialize machine registers
|
|
|
|
|
u.RegWrite(X86.UC_X86_REG_ESP, Utils.Int64ToBytes(address + 0x200000));
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// handle IN & OUT instruction
|
|
|
|
|
u.AddInHook(InHookCallback);
|
|
|
|
|
u.AddOutHook(OutHookCallback);
|
2016-01-04 18:59:05 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// tracing all instructions by having @begin > @end
|
|
|
|
|
u.AddCodeHook((uc, addr, size, userData) => CodeHookCallback(disassembler, uc, addr, size, userData), 1, 0);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// handle interrupt ourself
|
|
|
|
|
u.AddInterruptHook(InterruptHookCallback);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// handle SYSCALL
|
|
|
|
|
u.AddSyscallHook(SyscallHookCallback);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// intercept invalid memory events
|
|
|
|
|
u.AddEventMemHook(MemMapHookCallback, Common.UC_HOOK_MEM_READ_UNMAPPED | Common.UC_HOOK_MEM_WRITE_UNMAPPED);
|
2016-01-04 18:59:05 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine(">>> Start tracing code");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// emulate machine code in infinite time
|
|
|
|
|
u.EmuStart(address, address + code.Length, 0u, 0u);
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
// print registers
|
|
|
|
|
var ecx = u.RegRead(X86.UC_X86_REG_ECX);
|
|
|
|
|
var edx = u.RegRead(X86.UC_X86_REG_EDX);
|
|
|
|
|
var eax = u.RegRead(X86.UC_X86_REG_EAX);
|
|
|
|
|
Console.WriteLine($"[!] EAX = {eax:X}");
|
|
|
|
|
Console.WriteLine($"[!] ECX = {ecx:X}");
|
|
|
|
|
Console.WriteLine($"[!] EDX = {edx:X}");
|
2016-01-04 18:59:05 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine(">>> Emulation Done!");
|
2016-01-04 18:59:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private static int InHookCallback(Unicorn u, int port, int size, object userData)
|
2016-01-04 18:59:05 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var eip = u.RegRead(X86.UC_X86_REG_EIP);
|
|
|
|
|
Console.WriteLine($"[!] Reading from port 0x{port:X}, size: {size:X}, address: 0x{eip:X}");
|
|
|
|
|
var res = size switch
|
2016-01-04 18:59:05 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
1 =>
|
2016-01-04 18:59:05 +03:00
|
|
|
|
// read 1 byte to AL
|
2022-10-14 17:57:46 +03:00
|
|
|
|
0xf1,
|
|
|
|
|
2 =>
|
2016-01-04 18:59:05 +03:00
|
|
|
|
// read 2 byte to AX
|
2022-10-14 17:57:46 +03:00
|
|
|
|
0xf2,
|
|
|
|
|
4 =>
|
2016-01-04 18:59:05 +03:00
|
|
|
|
// read 4 byte to EAX
|
2022-10-14 17:57:46 +03:00
|
|
|
|
0xf4,
|
|
|
|
|
_ => 0
|
|
|
|
|
};
|
2016-01-04 18:59:05 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine($"[!] Return value: {res:X}");
|
2016-01-04 18:59:05 +03:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private static void OutHookCallback(Unicorn u, int port, int size, int value, object userData)
|
2016-01-04 18:59:05 +03:00
|
|
|
|
{
|
|
|
|
|
var eip = u.RegRead(X86.UC_X86_REG_EIP);
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine($"[!] Writing to port 0x{port:X}, size: {size:X}, value: 0x{value:X}, address: 0x{eip:X}");
|
2016-01-04 18:59:05 +03:00
|
|
|
|
|
|
|
|
|
// confirm that value is indeed the value of AL/ AX / EAX
|
|
|
|
|
var v = 0L;
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var regName = string.Empty;
|
2016-01-04 18:59:05 +03:00
|
|
|
|
switch (size)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
// read 1 byte in AL
|
|
|
|
|
v = u.RegRead(X86.UC_X86_REG_AL);
|
|
|
|
|
regName = "AL";
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
// read 2 byte in AX
|
|
|
|
|
v = u.RegRead(X86.UC_X86_REG_AX);
|
|
|
|
|
regName = "AX";
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
// read 4 byte in EAX
|
|
|
|
|
v = u.RegRead(X86.UC_X86_REG_EAX);
|
|
|
|
|
regName = "EAX";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine("[!] Register {0}: {1:X}", regName, v);
|
2016-01-04 18:59:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private static bool MemMapHookCallback(Unicorn u, int eventType, long address, int size, long value, object userData)
|
2016-01-04 18:59:05 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
if (eventType != Common.UC_MEM_WRITE_UNMAPPED) return false;
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"[!] Missing memory is being WRITE at 0x{address:X}, data size = {size:X}, data value = 0x{value:X}. Map memory.");
|
|
|
|
|
u.MemMap(0xaaaa0000, 2 * 1024 * 1024, Common.UC_PROT_ALL);
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-01-04 18:59:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CodeHookCallback1(
|
2022-10-14 17:57:46 +03:00
|
|
|
|
CapstoneX86Disassembler disassembler,
|
2016-01-04 18:59:05 +03:00
|
|
|
|
Unicorn u,
|
2022-10-14 17:57:46 +03:00
|
|
|
|
long addr,
|
|
|
|
|
int size,
|
|
|
|
|
object userData)
|
2016-01-04 18:59:05 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.Write($"[+] 0x{addr:X}: ");
|
2016-01-04 18:59:05 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var eipBuffer = new byte[4];
|
2016-01-04 18:59:05 +03:00
|
|
|
|
u.RegRead(X86.UC_X86_REG_EIP, eipBuffer);
|
|
|
|
|
|
|
|
|
|
var effectiveSize = Math.Min(16, size);
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var tmp = new byte[effectiveSize];
|
2016-01-04 18:59:05 +03:00
|
|
|
|
u.MemRead(addr, tmp);
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var t in tmp)
|
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
sb.AppendFormat($"{(0xFF & t):X} ");
|
2016-01-04 18:59:05 +03:00
|
|
|
|
}
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.Write($"{sb,-20}");
|
2016-01-04 18:59:05 +03:00
|
|
|
|
Console.WriteLine(Utils.Disassemble(disassembler, tmp));
|
|
|
|
|
}
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
|
|
|
|
private static void CodeHookCallback(
|
2022-10-14 17:57:46 +03:00
|
|
|
|
CapstoneX86Disassembler disassembler,
|
2016-01-04 13:30:11 +03:00
|
|
|
|
Unicorn u,
|
2022-10-14 17:57:46 +03:00
|
|
|
|
long addr,
|
|
|
|
|
int size,
|
|
|
|
|
object userData)
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.Write($"[+] 0x{addr:X}: ");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var eipBuffer = new byte[4];
|
2016-01-04 13:30:11 +03:00
|
|
|
|
u.RegRead(X86.UC_X86_REG_EIP, eipBuffer);
|
|
|
|
|
|
|
|
|
|
var effectiveSize = Math.Min(16, size);
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var tmp = new byte[effectiveSize];
|
2016-01-04 13:30:11 +03:00
|
|
|
|
u.MemRead(addr, tmp);
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var t in tmp)
|
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
sb.AppendFormat($"{(0xFF & t):X} ");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
}
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.Write($"{sb,-20}");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
Console.WriteLine(Utils.Disassemble(disassembler, tmp));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private static void SyscallHookCallback(Unicorn u, object userData)
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var eaxBuffer = new byte[4];
|
2016-01-04 13:30:11 +03:00
|
|
|
|
u.RegRead(X86.UC_X86_REG_EAX, eaxBuffer);
|
|
|
|
|
var eax = Utils.ToInt(eaxBuffer);
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine($"[!] Syscall EAX = 0x{eax:X}");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
|
|
|
|
u.EmuStop();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
private static void InterruptHookCallback(Unicorn u, int intNumber, object userData)
|
2016-01-04 13:30:11 +03:00
|
|
|
|
{
|
|
|
|
|
// only handle Linux syscall
|
|
|
|
|
if (intNumber != 0x80)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var eaxBuffer = new byte[4];
|
|
|
|
|
var eipBuffer = new byte[4];
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
|
|
|
|
u.RegRead(X86.UC_X86_REG_EAX, eaxBuffer);
|
|
|
|
|
u.RegRead(X86.UC_X86_REG_EIP, eipBuffer);
|
|
|
|
|
|
|
|
|
|
var eax = Utils.ToInt(eaxBuffer);
|
|
|
|
|
var eip = Utils.ToInt(eipBuffer);
|
|
|
|
|
|
|
|
|
|
switch (eax)
|
|
|
|
|
{
|
|
|
|
|
default:
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine($"[!] Interrupt 0x{eip:X} num {intNumber:X}, EAX=0x{eax:X}");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
break;
|
|
|
|
|
case 1: // sys_exit
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine($"[!] Interrupt 0x{eip:X} num {intNumber:X}, SYS_EXIT");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
u.EmuStop();
|
|
|
|
|
break;
|
|
|
|
|
case 4: // sys_write
|
|
|
|
|
|
|
|
|
|
// ECX = buffer address
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var ecxBuffer = new byte[4];
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
|
|
|
|
// EDX = buffer size
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var edxBuffer = new byte[4];
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
|
|
|
|
u.RegRead(X86.UC_X86_REG_ECX, ecxBuffer);
|
|
|
|
|
u.RegRead(X86.UC_X86_REG_EDX, edxBuffer);
|
|
|
|
|
|
|
|
|
|
var ecx = Utils.ToInt(ecxBuffer);
|
|
|
|
|
var edx = Utils.ToInt(edxBuffer);
|
|
|
|
|
|
|
|
|
|
// read the buffer in
|
|
|
|
|
var size = Math.Min(256, edx);
|
2022-10-14 17:57:46 +03:00
|
|
|
|
var buffer = new byte[size];
|
2016-01-04 13:30:11 +03:00
|
|
|
|
u.MemRead(ecx, buffer);
|
|
|
|
|
var content = Encoding.Default.GetString(buffer);
|
|
|
|
|
|
2022-10-14 17:57:46 +03:00
|
|
|
|
Console.WriteLine($"[!] Interrupt 0x{eip:X}: num {ecx:X}, SYS_WRITE. buffer = 0x{edx:X}, size = {size:X}, content = '{content}'");
|
2016-01-04 13:30:11 +03:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|