#一、背景
在Linux內(nèi)核的代碼中,大部分以C內(nèi)聯(lián)匯編編寫。
在編寫病毒時,也會常常用到,比如,要編寫一個不依賴libc的注入代碼時,需要調(diào)用mmap進(jìn)行內(nèi)存申請時,就要使用到syscall進(jìn)行系統(tǒng)調(diào)用。這時就需要使用到C語言的內(nèi)聯(lián)匯編。
static inline volatile int evil_open(const char *path, unsigned long flags)
{
long ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov $2, %%rax\n"
"syscall" : : "g"(path), "g"(flags));
asm ("mov %%rax, %0" : "=r"(ret));
return ret;
}
#二、內(nèi)聯(lián)匯編基本格式
asm [ volatile ] (
assembler template 匯編代碼。通常分行編寫
[ : output operands ] /* optional */ 輸出操作數(shù)
[ : input operands ] /* optional */ 輸入操作數(shù)
[ : list of clobbered registers ] /* optional */ 指定不允許gcc編譯器使用的寄存器列表
);
由于避免命名沖突,asm與__asm__等價,volatile 與 __volatile__等價
volatile 用于指示,當(dāng)添加此關(guān)鍵字時,不允許gcc編譯器對assmbly code進(jìn)行代碼優(yōu)化。
以上中括號,代表可選參數(shù)。也就是可以完全只包含匯編代碼,而不包含,輸入操作數(shù)和輸出操作數(shù)以及不允許gcc編譯器使用的寄存器列表。
#三、實例
下面的代碼就是使用C語言的內(nèi)聯(lián)匯編,syscall進(jìn)行系統(tǒng)調(diào)用。在屏幕上輸出“I am HotIce0”。并且調(diào)用exit(0)退出程序,這是一段可以編譯為位置獨(dú)立的注入代碼。
long _write(long fd, char *buf, unsigned long len)
{
long ret;
asm volatile (
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $1, %%rax\n"
"syscall"
:
:"g"(fd), "g"(buf), "g"(len)
);
asm("mov %%rax, %0":"=r"(ret));
return ret;
}
void _exit(long status)
{
asm(
"mov $60, %%rax\n"
"syscall"
:
:"r"(status)
);
}
_start()
{
_write(1, "I am HotIce0\n", sizeof("I am HotIce0\n"));
_exit(0);
}
其中用到的%0 , %1這樣的數(shù)字,是用來引用,輸入?yún)?shù)和輸出參數(shù)。
編號的方式是,%0是第一個輸出參數(shù),%n是最后一個輸入?yún)?shù)。
其中的$60,是立即數(shù),也就是60。十六進(jìn)制需要使用0x10這樣的寫法。
能操作的寄存器包括以下寄存器。
寄存器操作數(shù)約束:r (register operand constraint):gcc可以將變量保存在任何可用的GPR中g(shù)
GPR寄存器表 (通用目標(biāo)寄存器)
±–±-------------------+
| r | Register(s) |
±–±-------------------+
| a | %eax, %ax, %al |
| b | %ebx, %bx, %bl |
| c | %ecx, %cx, %cl |
| d | %edx, %dx, %dl |
| S | %esi, %si |
| D | %edi, %di |
其中的"g"(fd)
這種叫約束。
這樣的約束有很多種。比如以下常用的約束。
=
代表輸出變量用作輸出,原來的值會被新值替換。
+
代表即可用作輸入,也可用作輸出。
“m” : A memory operand is allowed, with any kind of address that the machine supports in general.允許內(nèi)存操作通過使用任何機(jī)器支持的地址類型。
“o” : A memory operand is allowed, but only if the address is offsettable. ie, adding a small offset to the address gives a valid address.允許使用內(nèi)存操作數(shù),但前提是該地址是可偏移的。 即,向地址添加一個小偏移量會給出一個有效的地址。
“V” : A memory operand that is not offsettable. In other words, anything that would fit the m’ constraint but not the
o’constraint.不可偏移的內(nèi)存操作,就是,任何的操作都適合用m修飾,但是不一定適用于o約束。
“i” : An immediate integer operand (one with constant value) is allowed. This includes symbolic constants whose values will be known only at assembly time.立即數(shù)操作
“n” : An immediate integer operand with a known numeric value is allowed. Many systems cannot support assembly-time constants for operands less than a word wide. Constraints for these operands should use ’n’ rather than ’i’.一個立即數(shù)操作使用一個已知的數(shù)值是被允許的,很多系統(tǒng)不支持為操作少于一個字大小的對象匯編時構(gòu)建。這個時候,應(yīng)該使用n而不是i。
“g” : Any register, memory or immediate integer operand is allowed, except for registers that are not general registers.任何寄存器,內(nèi)存或者立即數(shù)操作都是被允許的,但是,寄存器必須是通用寄存器。