64 SHEMU_STATUS shstatus;
78 if (NULL == Registers)
83 if (NULL == ShellcodeFlags)
101 memzero(ctx->Stack, ctx->StackSize);
105 memzero(ctx->Intbuf, ctx->IntbufSize);
113 Registers->Cr3, ctx->Shellcode, &sizeread);
116 ERROR(
"[ERROR] IntVirtMemRead shellcode failed for 0x%016llx : 0x%08x\n", Registers->Rip, status);
120 ctx->ShellcodeSize = sizeread;
127 ERROR(
"[ERROR] IntPhysicalMemRead shellcode failed for 0x%016llx : 0x%08x\n",
132 ctx->ShellcodeSize = 0x1000;
135 ctx->ShellcodeBase = Registers->Rip &
PAGE_MASK;
152 ERROR(
"[ERROR] IntGetCurrentMode failed: 0x%08x\n", status);
157 ctx->Registers.RegCr0 = Registers->Cr0;
158 ctx->Registers.RegCr2 = Registers->Cr2;
159 ctx->Registers.RegCr3 = Registers->Cr3;
160 ctx->Registers.RegCr4 = Registers->Cr4;
162 ctx->Registers.RegFlags = Registers->Flags;
163 ctx->Registers.RegRip = Registers->Rip;
164 ctx->Registers.RegRsp = Registers->Rsp;
167 ctx->Registers.RegRax = Registers->Rax;
168 ctx->Registers.RegRcx = Registers->Rcx;
169 ctx->Registers.RegRdx = Registers->Rdx;
170 ctx->Registers.RegRbx = Registers->Rbx;
171 ctx->Registers.RegRbp = Registers->Rbp;
172 ctx->Registers.RegRsi = Registers->Rsi;
173 ctx->Registers.RegRdi = Registers->Rdi;
174 ctx->Registers.RegR8 = Registers->R8;
175 ctx->Registers.RegR9 = Registers->R9;
176 ctx->Registers.RegR10 = Registers->R10;
177 ctx->Registers.RegR11 = Registers->R11;
178 ctx->Registers.RegR12 = Registers->R12;
179 ctx->Registers.RegR13 = Registers->R13;
180 ctx->Registers.RegR14 = Registers->R14;
181 ctx->Registers.RegR15 = Registers->R15;
188 ctx->Segments.Cs.Selector = 0x33;
189 ctx->Segments.Ds.Selector = 0x2b;
190 ctx->Segments.Es.Selector = 0x2b;
191 ctx->Segments.Ss.Selector = 0x2b;
192 ctx->Segments.Fs.Selector = 0x2b;
193 ctx->Segments.Gs.Selector = 0x53;
195 ctx->Segments.Fs.Base = 0;
200 WARNING(
"[WARNING] IntGsRead failed: 0x%08x\n", status);
201 ctx->Segments.Gs.Base = 0xBDBD0000;
204 ctx->TibBase = ctx->Segments.Gs.Base;
211 ERROR(
"[ERROR] IntGetSegs failed: 0x%08x\n", status);
215 ctx->Segments.Cs.Selector = segRegisters.
CsSelector;
216 ctx->Segments.Ds.Selector = segRegisters.
DsSelector;
217 ctx->Segments.Es.Selector = segRegisters.
EsSelector;
218 ctx->Segments.Ss.Selector = segRegisters.
SsSelector;
219 ctx->Segments.Fs.Selector = segRegisters.
FsSelector;
220 ctx->Segments.Gs.Selector = segRegisters.
GsSelector;
221 ctx->Segments.Fs.Base = segRegisters.
FsBase;
222 ctx->Segments.Gs.Base = segRegisters.
GsBase;
224 ctx->TibBase = ctx->Segments.Fs.Base;
228 ERROR(
"[ERROR] We don't support 16 bit!\n");
243 ctx->NopThreshold = SHEMU_DEFAULT_NOP_THRESHOLD;
244 ctx->StrThreshold = SHEMU_DEFAULT_STR_THRESHOLD;
245 ctx->MemThreshold = SHEMU_DEFAULT_MEM_THRESHOLD;
247 shstatus = ShemuEmulate(ctx);
252 ctx->Flags &= ~SHEMU_FLAG_TIB_ACCESS;
257 TRACE(
"[SHELLCODE] Emulation terminated with status 0x%08x, flags: 0x%lx, %d NOPs, emulated %d instructions, " 258 "RIP %lx.\n", shstatus, ctx->Flags, ctx->NopCount, ctx->InstructionsCount, ctx->Registers.RegRip);
INTSTATUS IntPhysicalMemRead(QWORD PhysicalAddress, DWORD Length, void *Buffer, DWORD *RetLength)
Reads data from a guest physical memory range, but only for a single page.
SHEMU_CONTEXT Shemucontext
Shellcode emulator context.
#define INT_STATUS_SUCCESS
#define INT_SUCCESS(Status)
BYTE ShemuInternal[SHEMU_SHELLCODE_SIZE+SHEMU_STACK_SIZE]
The shellcode emulator internal buffer.
#define SHEMU_SHELLCODE_SIZE
The shell code buffer size. It should be at least 2 pages in size.
Holds segment register state.
int INTSTATUS
The status data type.
INTRO_GUEST_TYPE OSType
The type of the guest.
BYTE ShemuShellcode[SHEMU_SHELLCODE_SIZE]
The shellcode emulator shellcode buffer.
INTSTATUS IntGetCurrentMode(DWORD CpuNumber, DWORD *Mode)
Read the current CS type.
#define IG_CURRENT_VCPU
For APIs that take a VCPU number as a parameter, this can be used to specify that the current VCPU sh...
#define INT_STATUS_INVALID_PARAMETER_4
INTSTATUS IntGsRead(DWORD CpuNumber, QWORD *GsValue)
Reads the IA32_GS_BASE guest MSR.
INTSTATUS IntShcIsSuspiciousCode(QWORD Gva, QWORD Gpa, DWORD CsType, IG_ARCH_REGS *Registers, QWORD *ShellcodeFlags)
Checks if the code located at the given guest virtual address is suspicious or not.
static void Shemuprint(PCHAR Data)
Log data.
BYTE ShemuStack[SHEMU_STACK_SIZE]
The shellcode emulator stack buffer.
QWORD ForceOff
Options that are forcibly disabled.
#define UNREFERENCED_PARAMETER(P)
#define SHEMU_STACK_SIZE
The size of the stack buffer used by shemu.
#define SHEMU_MAX_INSTRUCTIONS
The maximum instructions to be emulated by shemu.
GUEST_STATE gGuest
The current guest state.
INTSTATUS IntVirtMemRead(QWORD Gva, DWORD Length, QWORD Cr3, void *Buffer, DWORD *RetLength)
Reads data from a guest virtual memory range.
INTRO_PROT_OPTIONS ShemuOptions
Flags which describe the way shemu will give detections.
#define INT_STATUS_INVALID_PARAMETER_1
INTSTATUS IntGetSegs(DWORD CpuNumber, PIG_SEG_REGS Regs)
Read the guest segment registers.
#define INT_STATUS_INVALID_PARAMETER_2
#define INT_STATUS_INVALID_PARAMETER_3