💾 Archived View for radare.org › book › emulation › esil.gmi captured on 2024-08-18 at 17:28:12. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
ESIL stands for 'Evaluable Strings Intermediate Language'. It aims to describe a [Forth](https://en.wikipedia.org/wiki/Forth_%28programming_language%29)-like representation for every target CPU opcode semantics. ESIL representations can be evaluated (interpreted) in order to emulate individual instructions. Each command of an ESIL expression is separated by a comma. Its virtual machine can be described as this: ``` while ((word=haveCommand())) { if (word.isOperator()) { esilOperators[word](esil);
} else { esil.push (word); } nextCommand();
} ``` As we can see ESIL uses a stack-based interpreter similar to what is commonly used for calculators. You have two categories of inputs: values and operators. A value simply gets pushed on the stack, an operator then pops values (its arguments if you will) off the stack, performs its operation and pushes its results (if any) back on. We can think of ESIL as a post-fix notation of the operations we want to do.
So let's see an example: ``` 4,esp,-=,ebp,esp,=[4] ``` Can you guess what this is? If we take this post-fix notation and transform it back to in-fix we get ``` esp -= 4 4bytes(dword) [esp] = ebp ``` We can see that this corresponds to the x86 instruction `push ebp`! Isn't that cool? The aim is to be able to express most of the common operations performed by CPUs, like binary arithmetic operations, memory loads and stores, processing syscalls. This way if we can transform the instructions to ESIL we can see what a program does while it is running even for the most cryptic architectures you definitely don't have a device to debug on for.
r2's visual mode is great to inspect the ESIL evaluations.
There are 3 environment variables that are important for watching what a program does: ``` [0x00000000]> e emu.str = true ```
`asm.emu` tells r2 if you want ESIL information to be displayed. If it is set to true, you will see comments appear to the right of your disassembly that tell you how the contents of registers and memory addresses are changed by the current instruction. For example, if you have an instruction that subtracts a value from a register it tells you what the value was before and what it becomes after. This is super useful so you don't have to sit there yourself and track which value goes where.
One problem with this is that it is a lot of information to take in at once and sometimes you simply don't need it. r2 has a nice compromise for this. That is what the `emu.str` variable is for (`asm.emustr` on <= 2.2). Instead of this super verbose output with every register value, this only adds really useful information to the output, e.g., strings that are found at addresses a program uses or whether a jump is likely to be taken or not.
The third important variable is `asm.esil`. This switches your disassembly to no longer show you the actual disassembled instructions, but instead now shows you corresponding ESIL expressions that describe what the instruction does.
So if you want to take a look at how instructions are expressed in ESIL simply set "asm.esil" to true.
[0x00000000]> e asm.esil = true
In visual mode you can also toggle this by simply typing `O`.
- "ae" : Evaluate ESIL expression.
[0x00000000]> "ae 1,1,+" 0x2 [0x00000000]>
- "aes" : ESIL Step.
[0x00000000]> aes [0x00000000]>10aes
- "aeso" : ESIL Step Over.
[0x00000000]> aeso [0x00000000]>10aeso
- "aesu" : ESIL Step Until.
[0x00001000]> aesu 0x1035 ADDR BREAK [0x00001019]>
- "ar" : Show/modify ESIL registry.
[0x00001ec7]> ar r_00 = 0x1035 [0x00001ec7]> ar r_00 0x00001035 [0x00001019]>
Here is the complete instruction set used by the ESIL VM:
ESIL Opcode | Operands | Name | Operation| example =[]<br>=[\*]<br>=[1]<br>=[2]<br>=[4]<br>=[8] | src,dst | poke |\*dst=src | <br>> "ae 0xdeadbeef,0x10000,=[4],"<br><br>> pxw 4@0x10000<br>0x00010000 0xdeadbeef ....<br><br>> "ae 0x0,0x10000,=[4],"<br><br>> pxw 4@0x10000<br>0x00010000 0x00000000 []<br>[\*]<br>[1]<br>[2]<br>[4]<br>[8] | src | peek | stack=\*src | <br>> w test@0x10000<br><br>> "ae 0x10000,[4],"<br>0x74736574<br><br>> ar r_00=0x10000<br><br>> "ae r_00,[4],"<br>0x74736574 |=[]<br>|=[1]<br>|=[2]<br>|=[4]<br>|=[8] | reg | name | code | > <br>>
SWAP | | Swap | Swap two top elements | SWAP DUP | | Duplicate | Duplicate top element in stack | DUP NUM | | Numeric | If top element is a reference <br> (register name, label, etc),<br> dereference it and push its real value | NUM CLEAR | | Clear | Clear stack | CLEAR BREAK | | Break | Stops ESIL emulation | BREAK GOTO | n | Goto | Jumps to Nth ESIL word | GOTO 5 TODO | | To Do | Stops execution<br> (reason: ESIL expression not completed) | TODO
ESIL VM provides by default a set of helper operations for calculating flags.
They fulfill their purpose by comparing the old and the new value of the dst operand of the last performed eq-operation.
On every eq-operation (e.g. `=`) ESIL saves the old and new value of the dst operand.
Note, that there also exist weak eq operations (e.g. `:=`), which do not affect flag operations.
The `==` operation affects flag operations, despite not being an eq operation.
Flag operations are prefixed with `
z - zero flag, only set if the result of an operation is 0 b - borrow, this requires to specify from which bit (example: 4,$b - checks if borrow from bit 4) c - carry, same like above (example: 7,$c - checks if carry from bit 7) o - overflow p - parity r - regsize ( asm.bits/8 ) s - sign ds - delay slot state jt - jump target js - jump target set
A target opcode is translated into a comma separated list of ESIL expressions.
xor eax, eax -> 0,eax,=,1,zf,=
Memory access is defined by brackets operation: ``` mov eax, [0x80480] -> 0x80480,[],eax,= ``` Default operand size is determined by size of operation destination.
movb $0, 0x80480 -> 0,0x80480,=[1]
The `?` operator uses the value of its argument to decide whether to evaluate the expression in curly braces.
1. Is the value zero? -> Skip it.
2. Is the value non-zero? -> Evaluate it.
cmp eax, 123 -> 123,eax,==,$z,zf,= jz eax -> zf,?{,eax,eip,=,}
If you want to run several expressions under a conditional, put them in curly braces: ``` zf,?{,eip,esp,=[],eax,eip,=,$r,esp,-=,} ```
Whitespaces, newlines and other chars are ignored. So the first thing when processing a ESIL program is to remove spaces: ``` esil = r_str_replace (esil, " ", "", R_TRUE);
Syscalls need special treatment. They are indicated by ' at the beginning of an expression. You can pass an optional numeric value to specify a number of syscall. An ESIL emulator must handle syscalls. See (r_esil_syscall). ### Arguments Order for Non-associative Operations As discussed on IRC, the current implementation works like this:
a,b,- b - a a,b,/= b /= a ``` This approach is more readable, but it is less stack-friendly.
NOPs are represented as empty strings. As it was said previously, interrupts are marked by '