2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

GUN C內(nèi)聯(lián)匯編

 立志德美 2019-04-10

#一、背景

  1. 在Linux內(nèi)核的代碼中,大部分以C內(nèi)聯(lián)匯編編寫。

  2. 在編寫病毒時(shí),也會(huì)常常用到,比如,要編寫一個(gè)不依賴libc的注入代碼時(shí),需要調(diào)用mmap進(jìn)行內(nèi)存申請(qǐng)時(shí),就要使用到syscall進(jìn)行系統(tǒng)調(diào)用。這時(shí)就需要使用到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編譯器使用的寄存器列表
    );
  1. 由于避免命名沖突,asm與__asm__等價(jià),volatile 與 __volatile__等價(jià)

  2. volatile 用于指示,當(dāng)添加此關(guān)鍵字時(shí),不允許gcc編譯器對(duì)assmbly code進(jìn)行代碼優(yōu)化。

  3. 以上中括號(hào),代表可選參數(shù)。也就是可以完全只包含匯編代碼,而不包含,輸入操作數(shù)和輸出操作數(shù)以及不允許gcc編譯器使用的寄存器列表。

#三、實(shí)例
下面的代碼就是使用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);
}
  1. 其中用到的%0 , %1這樣的數(shù)字,是用來引用,輸入?yún)?shù)和輸出參數(shù)。
    編號(hào)的方式是,%0是第一個(gè)輸出參數(shù),%n是最后一個(gè)輸入?yún)?shù)。

  2. 其中的$60,是立即數(shù),也就是60。十六進(jìn)制需要使用0x10這樣的寫法。

  3. 能操作的寄存器包括以下寄存器。

寄存器操作數(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 |

  1. 其中的"g"(fd)這種叫約束。
    這樣的約束有很多種。比如以下常用的約束。

  • = 代表輸出變量用作輸出,原來的值會(huì)被新值替換。

  • + 代表即可用作輸入,也可用作輸出。

  • “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ù),但前提是該地址是可偏移的。 即,向地址添加一個(gè)小偏移量會(huì)給出一個(gè)有效的地址。

  • “V” : A memory operand that is not offsettable. In other words, anything that would fit the m’ constraint but not theo’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’.一個(gè)立即數(shù)操作使用一個(gè)已知的數(shù)值是被允許的,很多系統(tǒng)不支持為操作少于一個(gè)字大小的對(duì)象匯編時(shí)構(gòu)建。這個(gè)時(shí)候,應(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ù)操作都是被允許的,但是,寄存器必須是通用寄存器。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多