[英]My xstrcmp (which is copy of strcmp library function) gives incorrect results
[英]Desiging Shellcode gives incorrect results
我編寫了一個簡單的匯編程序:
.text
.globl _start
_start:
mov %20, %rbx
mov %1, %rax
int $0x80
顯然,這是在64位OS(Linux)上運行的。 然后,我將其編譯如下:
as -o ExitShellcode.o ExitShellcode.s
ld -o ExitShellcode ExitShellcode.o
最后運行該程序后,它以20狀態退出
echo $?
20
使用objdump轉儲該文件的shellcode會給出:
objdump -d ExitShellcode
ExitShellcode: file format elf64-x86-64
Disassembly of section .text:
0000000000400078 <_start>:
400078: 48 c7 c3 14 00 00 00 mov $0x14,%rbx
40007f: 48 c7 c0 01 00 00 00 mov $0x1,%rax
400086: cd 80 int $0x80
但是,將shellcode放入此程序后:
#include <stdio.h>
char shellcode[] = "\x48\xc7\xc3\x14\x00\x00\x00"
"\x48\xc7\xc0\x01\x00\x00\x00"
"\xcd\x80";
int main()
{
int *ret;
ret = (int *)&ret +2;
*ret = (int)shellcode;
}
並編譯:
gcc -g -o Shellcode Shellcode.c
Shellcode.c: In function ‘main’:
Shellcode.c:13:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
*ret = (int)shellcode;
並運行時,程序以0狀態退出:
echo $?
0
有什么問題? 它不應該以20退出嗎?
您的代碼錯誤地假定編譯器會將變量ret
放在相對於main
的返回地址的堆棧上的某個位置。 相反,編譯器將其放在允許的其他地方,因此您的代碼不執行任何操作。 您可能遵循在Internet上發現的一個設計不良的示例。
如果要在shellcode
數組中執行“ shellcode”,則可以嘗試向其強制轉換為函數指針,然后調用它:
char shellcode[] = "\x48\xc7\xc3\x14\x00\x00\x00"
"\x48\xc7\xc0\x01\x00\x00\x00"
"\xcd\x80";
int main()
{
((void (*)()) shellcode)();
}
但是,這可能仍然會失敗,因為放置shellcode
的.data
部分不可執行,因此程序在運行時將崩潰。 要解決該問題, -zexecstack
在鏈接程序時使用-zexecstack
選項。 例如:
gcc -zexecstack -g -o Shellcode Shellcode.c
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.