Solution refactoring and bug fixing
This commit is contained in:
@@ -6,11 +6,11 @@ namespace UnicornSamples
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// X86 tests
|
||||
X86Sample.X86Code32();
|
||||
//X86Sample.X86Code32Loop();
|
||||
X86Sample.X86Code32InvalidMemRead();
|
||||
X86Sample.X86Code32InvalidMemWrite();
|
||||
// X86 tests 32bit
|
||||
X86Sample32.X86Code32();
|
||||
X86Sample32.X86Code32InvalidMemRead();
|
||||
X86Sample32.X86Code32InvalidMemWriteWithRuntimeFix();
|
||||
X86Sample32.X86Code32InOut();
|
||||
|
||||
// Run all shellcode tests
|
||||
ShellcodeSample.X86Code32Self();
|
||||
|
||||
@@ -69,11 +69,7 @@ namespace UnicornSamples
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
u.MemWrite(address, code);
|
||||
|
||||
//var read = new Byte[code.Length];
|
||||
//u.MemRead(address, read);
|
||||
//Console.WriteLine(Disassemble(disassembler, code));//
|
||||
|
||||
|
||||
// initialize machine registers
|
||||
u.RegWrite(X86.UC_X86_REG_ESP, Utils.Int64ToBytes(address + 0x200000));
|
||||
|
||||
@@ -88,7 +84,7 @@ namespace UnicornSamples
|
||||
|
||||
// handle SYSCALL
|
||||
u.AddSyscallHook(SyscallHookCallback);
|
||||
|
||||
|
||||
Console.WriteLine(">>> Start tracing code");
|
||||
|
||||
// emulate machine code in infinite time
|
||||
@@ -134,7 +130,7 @@ namespace UnicornSamples
|
||||
u.RegRead(X86.UC_X86_REG_EAX, eaxBuffer);
|
||||
var eax = Utils.ToInt(eaxBuffer);
|
||||
|
||||
Console.WriteLine("Syscall >>> EAX = 0x{0}", eax.ToString("X"));
|
||||
Console.WriteLine("[!] Syscall EAX = 0x{0}", eax.ToString("X"));
|
||||
|
||||
u.EmuStop();
|
||||
}
|
||||
@@ -159,10 +155,10 @@ namespace UnicornSamples
|
||||
switch (eax)
|
||||
{
|
||||
default:
|
||||
Console.WriteLine("Interrupt >>> 0x{0} num {1}, EAX=0x{2}", eip.ToString("X"), intNumber.ToString("X"), eax.ToString("X"));
|
||||
Console.WriteLine("[!] Interrupt 0x{0} num {1}, EAX=0x{2}", eip.ToString("X"), intNumber.ToString("X"), eax.ToString("X"));
|
||||
break;
|
||||
case 1: // sys_exit
|
||||
Console.WriteLine("Interrupt >>> 0x{0} num {1}, SYS_EXIT", eip.ToString("X"), intNumber.ToString("X"));
|
||||
Console.WriteLine("[!] Interrupt 0x{0} num {1}, SYS_EXIT", eip.ToString("X"), intNumber.ToString("X"));
|
||||
u.EmuStop();
|
||||
break;
|
||||
case 4: // sys_write
|
||||
@@ -186,7 +182,7 @@ namespace UnicornSamples
|
||||
var content = Encoding.Default.GetString(buffer);
|
||||
|
||||
Console.WriteLine(
|
||||
"Interrupt >>> 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
|
||||
"[!] Interrupt 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
|
||||
eip.ToString("X"),
|
||||
ecx.ToString("X"),
|
||||
edx.ToString("X"),
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ShellcodeSample.cs" />
|
||||
<Compile Include="Utils.cs" />
|
||||
<Compile Include="X86Sample.cs" />
|
||||
<Compile Include="X86Sample32.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
|
||||
@@ -12,7 +12,7 @@ using UnicornManaged.Const;
|
||||
|
||||
namespace UnicornSamples
|
||||
{
|
||||
internal class X86Sample
|
||||
internal class X86Sample32
|
||||
{
|
||||
private const Int64 ADDRESS = 0x1000000;
|
||||
|
||||
@@ -25,17 +25,7 @@ namespace UnicornSamples
|
||||
};
|
||||
Run(X86_CODE32);
|
||||
}
|
||||
|
||||
public static void X86Code32Loop()
|
||||
{
|
||||
Byte[] X86_CODE32_LOOP =
|
||||
{
|
||||
// INC ecx; DEC edx; JMP self-loop
|
||||
0x41, 0x4a, 0xeb, 0xfe
|
||||
};
|
||||
Run(X86_CODE32_LOOP);
|
||||
}
|
||||
|
||||
|
||||
public static void X86Code32InvalidMemRead()
|
||||
{
|
||||
Byte[] X86_CODE32_MEM_READ =
|
||||
@@ -46,16 +36,27 @@ namespace UnicornSamples
|
||||
Run(X86_CODE32_MEM_READ, true);
|
||||
}
|
||||
|
||||
public static void X86Code32InvalidMemWrite()
|
||||
public static void X86Code32InvalidMemWriteWithRuntimeFix()
|
||||
{
|
||||
Byte[] X86_CODE32_MEM_WRITE =
|
||||
{
|
||||
// mov [0xaaaaaaaa], ecx; INC ecx; DEC edx
|
||||
0x89, 0x0D, 0xAA, 0xAA, 0xAA, 0xAA, 0x41, 0x4a
|
||||
};
|
||||
Run(X86_CODE32_MEM_WRITE, true);
|
||||
Run(X86_CODE32_MEM_WRITE);
|
||||
}
|
||||
|
||||
public static void X86Code32InOut()
|
||||
{
|
||||
Byte[] X86_CODE32_INOUT =
|
||||
{
|
||||
// INC ecx; IN AL, 0x3f; DEC edx; OUT 0x46, AL; INC ebx
|
||||
0x41, 0xE4, 0x3F, 0x4a, 0xE6, 0x46, 0x43
|
||||
};
|
||||
Run(X86_CODE32_INOUT);
|
||||
}
|
||||
|
||||
|
||||
private static void Run(Byte[] code, Boolean raiseException = false)
|
||||
{
|
||||
Console.WriteLine();
|
||||
@@ -67,7 +68,7 @@ namespace UnicornSamples
|
||||
Exception e = null;
|
||||
try
|
||||
{
|
||||
RunTest(code, ADDRESS);
|
||||
RunTest(code, ADDRESS, Common.UC_MODE_32);
|
||||
}
|
||||
catch (UnicornEngineException ex)
|
||||
{
|
||||
@@ -83,9 +84,9 @@ namespace UnicornSamples
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
private static void RunTest(Byte[] code, Int64 address)
|
||||
private static void RunTest(Byte[] code, Int64 address, Int32 mode)
|
||||
{
|
||||
using (var u = new Unicorn(Common.UC_ARCH_X86, Common.UC_MODE_32))
|
||||
using (var u = new Unicorn(Common.UC_ARCH_X86, mode))
|
||||
using (var disassembler = CapstoneDisassembler.CreateX86Disassembler(DisassembleMode.Bit32))
|
||||
{
|
||||
Console.WriteLine("Unicorn version: {0}", u.Version());
|
||||
@@ -93,12 +94,21 @@ namespace UnicornSamples
|
||||
// map 2MB of memory for this emulation
|
||||
u.MemMap(address, 2 * 1024 * 1024, Common.UC_PROT_ALL);
|
||||
|
||||
// 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);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
u.MemWrite(address, code);
|
||||
|
||||
// initialize machine registers
|
||||
u.RegWrite(X86.UC_X86_REG_ESP, Utils.Int64ToBytes(address + 0x200000));
|
||||
|
||||
// handle IN & OUT instruction
|
||||
u.AddInHook(InHookCallback);
|
||||
u.AddOutHook(OutHookCallback);
|
||||
|
||||
// tracing all instructions by having @begin > @end
|
||||
u.AddCodeHook((uc, addr, size, userData) => CodeHookCallback(disassembler, uc, addr, size, userData), 1, 0);
|
||||
|
||||
@@ -108,14 +118,119 @@ namespace UnicornSamples
|
||||
// handle SYSCALL
|
||||
u.AddSyscallHook(SyscallHookCallback);
|
||||
|
||||
// intercept invalid memory events
|
||||
u.AddEventMemHook(MemMapHookCallback, Common.UC_HOOK_MEM_READ_UNMAPPED | Common.UC_HOOK_MEM_WRITE_UNMAPPED);
|
||||
|
||||
Console.WriteLine(">>> Start tracing code");
|
||||
|
||||
// emulate machine code in infinite time
|
||||
u.EmuStart(address, address + code.Length, 0u, 0u);
|
||||
|
||||
// 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 = {0}", eax.ToString("X"));
|
||||
Console.WriteLine("[!] ECX = {0}", ecx.ToString("X"));
|
||||
Console.WriteLine("[!] EDX = {0}", edx.ToString("X"));
|
||||
|
||||
Console.WriteLine(">>> Emulation Done!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Int32 InHookCallback(Unicorn u, Int32 port, Int32 size, Object userData)
|
||||
{
|
||||
var eip = u.RegRead(X86.UC_X86_REG_EIP);
|
||||
Console.WriteLine("[!] Reading from port 0x{0}, size: {1}, address: 0x{2}", port.ToString("X"), size.ToString("X"), eip.ToString("X"));
|
||||
var res = 0;
|
||||
switch (size)
|
||||
{
|
||||
case 1:
|
||||
// read 1 byte to AL
|
||||
res = 0xf1;
|
||||
break;
|
||||
case 2:
|
||||
// read 2 byte to AX
|
||||
res = 0xf2;
|
||||
break;
|
||||
case 4:
|
||||
// read 4 byte to EAX
|
||||
res = 0xf4;
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine("[!] Return value: {0}", res.ToString("X"));
|
||||
return res;
|
||||
}
|
||||
|
||||
private static void OutHookCallback(Unicorn u, Int32 port, Int32 size, Int32 value, Object userData)
|
||||
{
|
||||
var eip = u.RegRead(X86.UC_X86_REG_EIP);
|
||||
Console.WriteLine("[!] Writing to port 0x{0}, size: {1}, value: 0x{2}, address: 0x{3}", port.ToString("X"), size.ToString("X"), value.ToString("X"), eip.ToString("X"));
|
||||
|
||||
// confirm that value is indeed the value of AL/ AX / EAX
|
||||
var v = 0L;
|
||||
var regName = String.Empty;
|
||||
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;
|
||||
}
|
||||
|
||||
Console.WriteLine("[!] Register {0}: {1}", regName, v.ToString("X"));
|
||||
}
|
||||
|
||||
private static Boolean MemMapHookCallback(Unicorn u, Int32 eventType, Int64 address, Int32 size, Int64 value, Object userData)
|
||||
{
|
||||
if (eventType == Common.UC_MEM_WRITE_UNMAPPED)
|
||||
{
|
||||
Console.WriteLine("[!] Missing memory is being WRITE at 0x{0}, data size = {1}, data value = 0x{2}. Map memory.", address.ToString("X"), size.ToString("X"), value.ToString("X"));
|
||||
u.MemMap(0xaaaa0000, 2 * 1024 * 1024, Common.UC_PROT_ALL);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void CodeHookCallback1(
|
||||
CapstoneDisassembler<X86Instruction, X86Register, X86InstructionGroup, X86InstructionDetail> disassembler,
|
||||
Unicorn u,
|
||||
Int64 addr,
|
||||
Int32 size,
|
||||
Object userData)
|
||||
{
|
||||
Console.Write("[+] 0x{0}: ", addr.ToString("X"));
|
||||
|
||||
var eipBuffer = new Byte[4];
|
||||
u.RegRead(X86.UC_X86_REG_EIP, eipBuffer);
|
||||
|
||||
var effectiveSize = Math.Min(16, size);
|
||||
var tmp = new Byte[effectiveSize];
|
||||
u.MemRead(addr, tmp);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (var t in tmp)
|
||||
{
|
||||
sb.AppendFormat("{0} ", (0xFF & t).ToString("X"));
|
||||
}
|
||||
Console.Write("{0,-20}", sb);
|
||||
Console.WriteLine(Utils.Disassemble(disassembler, tmp));
|
||||
}
|
||||
|
||||
private static void CodeHookCallback(
|
||||
CapstoneDisassembler<X86Instruction, X86Register, X86InstructionGroup, X86InstructionDetail> disassembler,
|
||||
@@ -148,7 +263,7 @@ namespace UnicornSamples
|
||||
u.RegRead(X86.UC_X86_REG_EAX, eaxBuffer);
|
||||
var eax = Utils.ToInt(eaxBuffer);
|
||||
|
||||
Console.WriteLine("Syscall >>> EAX = 0x{0}", eax.ToString("X"));
|
||||
Console.WriteLine("[!] Syscall EAX = 0x{0}", eax.ToString("X"));
|
||||
|
||||
u.EmuStop();
|
||||
}
|
||||
@@ -173,10 +288,10 @@ namespace UnicornSamples
|
||||
switch (eax)
|
||||
{
|
||||
default:
|
||||
Console.WriteLine("Interrupt >>> 0x{0} num {1}, EAX=0x{2}", eip.ToString("X"), intNumber.ToString("X"), eax.ToString("X"));
|
||||
Console.WriteLine("[!] Interrupt 0x{0} num {1}, EAX=0x{2}", eip.ToString("X"), intNumber.ToString("X"), eax.ToString("X"));
|
||||
break;
|
||||
case 1: // sys_exit
|
||||
Console.WriteLine("Interrupt >>> 0x{0} num {1}, SYS_EXIT", eip.ToString("X"), intNumber.ToString("X"));
|
||||
Console.WriteLine("[!] Interrupt 0x{0} num {1}, SYS_EXIT", eip.ToString("X"), intNumber.ToString("X"));
|
||||
u.EmuStop();
|
||||
break;
|
||||
case 4: // sys_write
|
||||
@@ -200,7 +315,7 @@ namespace UnicornSamples
|
||||
var content = Encoding.Default.GetString(buffer);
|
||||
|
||||
Console.WriteLine(
|
||||
"Interrupt >>> 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
|
||||
"[!] Interrupt 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
|
||||
eip.ToString("X"),
|
||||
ecx.ToString("X"),
|
||||
edx.ToString("X"),
|
||||
Reference in New Issue
Block a user