Выбрать главу

func.x.arg0=Arg_type,arg_name

.

.

.

func.X.ret=Return_type

func.X.cc=calling_convention

It should be self-explanatory. Let's do strncasecmp as an example for x86 arch for Linux machines. According to man pages, strncasecmp is defined as the following:

int strcasecmp(const char *s1, const char *s2, size_t n);

When converting it into its sdb representation it will look like the following:

strcasecmp=func

func.strcasecmp.args=3

func.strcasecmp.arg0=char *,s1

func.strcasecmp.arg1=char *,s2

func.strcasecmp.arg2=size_t,n

func.strcasecmp.ret=int

func.strcasecmp.cc=cdecl

Note that the .cc part is optional and if it didn't exist the default calling-convention for your target architecture will be used instead. There is one extra optional key

func.x.noreturn=true/false

This key is used to mark functions that will not return once called, such as exit and _exit.

Radare2 uses calling conventions to help in identifying function formal arguments and return types. It is used also as a guide for basic function prototype and type propagation.

[0x00000000]> afc?

|Usage: afc[agl?]

| afc convention Manually set calling convention for current function

| afc Show Calling convention for the Current function

| afc=([cctype]) Select or show default calling convention

| afcr[j] Show register usage for the current function

| afca Analyse function for finding the current calling convention

| afcf[j] [name] Prints return type function(arg1, arg2...), see afij

| afck List SDB details of call loaded calling conventions

| afcl List all available calling conventions

| afco path Open Calling Convention sdb profile from given path

| afcR Register telescoping using the calling conventions order

[0x00000000]>

   • To list all available calling conventions for current architecture using afcl command

[0x00000000]> afcl

amd64

ms

   • To display function prototype of standard library functions you have afcf command

[0x00000000]> afcf printf

int printf(const char *format)

[0x00000000]> afcf fgets

char *fgets(char *s, int size, FILE *stream)

All this information is loaded via sdb under /libr/anal/d/cc-[arch]-[bits].sdb

default.cc=amd64

ms=cc

cc.ms.name=ms

cc.ms.arg1=rcx

cc.ms.arg2=rdx

cc.ms.arg3=r8

cc.ms.arg3=r9

cc.ms.argn=stack

cc.ms.ret=rax

cc.x.argi=rax is used to set the ith argument of this calling convention to register name rax

cc.x.argn=stack means that all the arguments (or the rest of them in case there was argi for any i as counting number) will be stored in stack from left to right

cc.x.argn=stack_rev same as cc.x.argn=stack except for it means argument are passed right to left

There is a basic support of virtual tables parsing (RTTI and others). The most important thing before you start to perform such kind of analysis is to check if the anal.cpp.abi option is set correctly, and change if needed.

All commands to work with virtual tables are located in the av namespace. Currently, the support is very basic, allowing you only to inspect parsed tables.

|Usage: av[?jr*] C++ vtables and RTTI

| av search for vtables in data sections and show results

| avj like av, but as json

| av* like av, but as r2 commands

| avr[j@addr] try to parse RTTI at vtable addr (see anal.cpp.abi)

| avra[j] search for vtables and try to parse RTTI at each of them

The main commands here are av and avr. av lists all virtual tables found when r2 opened the file. If you are not happy with the result you may want to try to parse virtual table at a particular address with avr command. avra performs the search and parsing of all virtual tables in the binary, like r2 does during the file opening.

Radare2 allows manual search for assembly code looking like a syscall operation. For example on ARM platform usually they are represented by the svc instruction, on the others can be a different instructions, e.g. syscall on x86 PC.

[0x0001ece0]> /ad/ svc

...

0x000187c2 # 2: svc 0x76

0x000189ea # 2: svc 0xa9

0x00018a0e # 2: svc 0x82

...

Syscalls detection is driven by asm.os, asm.bits, and asm.arch. Be sure to setup those configuration options accordingly. You can use asl command to check if syscalls' support is set up properly and as you expect. The command lists syscalls supported for your platform.

[0x0001ece0]> asl

...

sd_softdevice_enable = 0x80.16

sd_softdevice_disable = 0x80.17

sd_softdevice_is_enabled = 0x80.18

...

If you setup ESIL stack with aei or aeim, you can use /as command to search the addresses where particular syscalls were found and list them.

[0x0001ece0]> aei

[0x0001ece0]> /as

0x000187c2 sd_ble_gap_disconnect

0x000189ea sd_ble_gatts_sys_attr_set

0x00018a0e sd_ble_gap_sec_info_reply

...

To reduce searching time it is possible to restrict the searching range for only executable segments or sections with /as @e:search.in=io.maps.x

Using the ESIL emulation radare2 can print syscall arguments in the disassembly output. To enable the linear (but very rough) emulation use asm.emu configuration variable:

[0x0001ece0]> e asm.emu=true

[0x0001ece0]> s 0x000187c2

[0x000187c2]> pdf~svc

0x000187c2 svc 0x76 ; 118 = sd_ble_gap_disconnect

[0x000187c2]>

In case of executing aae (or aaaa which calls aae) command radare2 will push found syscalls to a special syscall. flagspace, which can be useful for automation purpose:

[0x000187c2]> fs

0 0 * imports

1 0 * symbols

2 1523 * functions

3 420 * strings

4 183 * syscalls

[0x000187c2]> f~syscall

...

0x000187c2 1 syscall.sd_ble_gap_disconnect.0

0x000189ea 1 syscall.sd_ble_gatts_sys_attr_set

0x00018a0e 1 syscall.sd_ble_gap_sec_info_reply

...

It also can be interactively navigated through within HUD mode (V_)

0> syscall.sd_ble_gap_disconnect

- 0x000187b2 syscall.sd_ble_gap_disconnect

0x000187c2 syscall.sd_ble_gap_disconnect.0

0x00018a16 syscall.sd_ble_gap_disconnect.1

0x00018b32 syscall.sd_ble_gap_disconnect.2

0x0002ac36 syscall.sd_ble_gap_disconnect.3

When debugging in radare2, you can use dcs to continue execution until the next syscall. You can also run dcs* to trace all syscalls.

[0xf7fb9120]> dcs*

Running child until syscalls:-1

child stopped with signal 133

--> SN 0xf7fd3d5b syscall 45 brk (0xffffffda)

child stopped with signal 133

--> SN 0xf7fd28f3 syscall 384 arch_prctl (0xffffffda 0x3001)

child stopped with signal 133

--> SN 0xf7fc81b2 syscall 33 access (0xffffffda 0xf7fd8bf1)

child stopped with signal 133

radare2 also has a syscall name to syscall number utility. You can return the syscall name of a given syscall number or vice versa, without leaving the shell.

[0x08048436]> asl 1

exit

[0x08048436]> asl write

4

[0x08048436]> ask write

0x80,4,3,iZi

See as? for more information about the utility.