[英]Calling x86 local function using shellcode
我想使用將流重定向到本地函數,然后使用 shellcode 返回到原始函數。
我定義了兩個簡單的函數並使用 objdump 來獲取它們的匯編代碼:
// unsigned char *g_code = "\x55\x48\x89\xe5\xb8\x2a\x00\x00\x00\x5d\xc3";
int g() {
return 42;
}
// unsigned char *f_code_original = "\x55\x48\x89\xe5\x48\x83\xec\x10\xb8\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x89\x45\xfc\xb8\x2a\x00\x00\x00\xc9\xc3";
int f() {
int x = g();
return 42;
}
在另一個文件中,我有一個要在 f 的兩條指令之間調用的函數:
void redirect() {
FILE *out = fopen("redirect.txt", "w");
fprintf(out, "REDIRECT WORKED");
fclose(out);
}
為此,我使用以下代碼,使用 -fPIC -fno-stack-protector -z execstack 編譯:
void f_func() {
unsigned char *f_code_original = "\x55\x48\x89\xe5\x48\x83\xec\x10\xb8\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x89\x45\xfc\xb8\x2a\x00\x00\x00\xc9\xc3";
unsigned char f_code_modified[] = "\x55\x48\x89\xe5\x48\x83\xec\x10\xb8\x00\x00\x00\x00\xe8\xfb\xfe\xff\xff\xb8\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x89\x45\xfc\x8b\x45\xfc\xc9\xc3";
int value = 0;
int (*f)() = (int (*)())f_code_modified;
value = f();
printf("%d\n", value);
}
如果我使用 f 的原始代碼(因為我是從 objdump 得到的),它就可以工作。 我想修改它以調用我的重定向函數,然后恢復當前執行。
匯編代碼(用於 f_code_modified):
0: 55 push ebp
1: 48 dec eax
2: 89 e5 mov ebp,esp
4: 48 dec eax
5: 83 ec 10 sub esp,0x10
8: b8 00 00 00 00 mov eax,0x0 <==
d: e8 fb fe ff ff call 0xffffff0d <==
12: b8 00 00 00 00 mov eax,0x0
17: e8 00 00 00 00 call 0x1c
1c: 89 45 fc mov DWORD PTR [ebp-0x4],eax
1f: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
22: c9 leave
23: c3 ret
如果我直接從 main (int x = g(); redirect(); return 42;) 進行調用,這看起來很相似,但我認為 d: e8 .. .. .. .. 處的調用指令是相對的當前指令指針。
如果我像這樣運行程序,則會出現分段錯誤。
問題:有沒有辦法在運行時找到當前的指令指針,然后把shellcode寫成\\xe8\\x??\\x??\\x??\\x?? 調用函數重定向? 我需要修改什么? 我已經嘗試過使用 -fPIC 並獲取重定向地址(使用 &),但它不起作用。
按照 Peter Cordes 的評論,您可以將目標函數的絕對地址嵌入到 shellcode 中。
要查找函數地址redirect()
,我使用的是nm
,所以命令是:
% nm <binary> | grep redirect
輸出: 080484bb T redirect
所以,我重寫了你的 shellcode 進行修改,添加了一些redirect()
函數地址:
test_shellcode :
push ebp
dec eax
mov ebp,esp
dec eax
sub esp,0x10
mov eax,0x080484bb ; redirect() function address
call eax
mov dword [ebp-0x4],eax
mov eax,dword [ebp-0x4]
leave
ret
test_shellcode2 :
push ebp
dec eax
mov ebp,esp
dec eax
sub esp,0x10
mov eax,0x80484bb ; redirect() function address
call eax
mov eax,0x0
call 0x1c
mov dword [ebp-0x4],eax
mov eax,dword [ebp-0x4]
leave
ret
在這里,我修改了您的代碼:
#include <stdio.h>
#include <string.h>
void redirect() {
FILE *out = fopen("redirect.txt", "w");
fprintf(out, "REDIRECT WORKED");
fclose(out);
}
void f_func() {
// unsigned char *f_code_original = "\x55\x48\x89\xe5\x48\x83\xec\x10\xb8\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x89\x45\xfc\xb8\x2a\x00\x00\x00\xc9\xc3";
// unsigned char f_code_modified[] = "\x55\x48\x89\xe5\x48\x83\xec\x10\xb8\x00\x00\x00\x00\xe8\xbb\x84\x04\x08\xb8\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x89\x45\xfc\x8b\x45\xfc\xc9\xc3";
// Here shellcode, I wrote :
unsigned char *test_shellcode = "\x55\x48\x89\xe5\x48\x83\xec\x10\xb8\xbb\x84\x04\x08\xff\xd0\x89\x45\xfc\x8b\x45\xfc\xc9\xc3";
// unsigned char *test_shellcode2 = "\x55\x48\x89\xe5\x48\x83\xec\x10\xb8\xbb\x84\x04\x08\xff\xd0\xb8\x00\x00\x00\x00\xe8\xa3\x7f\xfb\xf7\x89\x45\xfc\x8b\x45\xfc\xc9\xc3";
int value = 0;
int (*f)() = (int (*)())test_shellcode;
value = f();
printf("%d\n", value);
}
int main(int argc, char **argv) {
f_func();
}
用-fPIC -fno-stack-protector -z execstack
編譯,所以是的,它可以工作。 這是例如我使用test_shellcode :
% ls -l
total 16
-rwxrwxr-x 1 febri febri 7548 Jan 1 08:40 shell
-rw-rw-r-- 1 febri febri 1075 Jan 1 08:39 shell.c
drwxrwxr-x 2 febri febri 4096 Jan 1 08:16 shellcode
% ./shell
0
% ls -l
total 20
-rw-rw-r-- 1 febri febri 15 Jan 1 08:41 redirect.txt
-rwxrwxr-x 1 febri febri 7548 Jan 1 08:40 shell
-rw-rw-r-- 1 febri febri 1075 Jan 1 08:39 shell.c
drwxrwxr-x 2 febri febri 4096 Jan 1 08:16 shellcode
% cat redirect.txt
REDIRECT WORKED
但是,如果我使用test_shellcode2 ,我會得到Segmentation fault
,但它的工作:
% ls -l
total 16
-rwxrwxr-x 1 febri febri 7548 Jan 1 08:46 shell
-rw-rw-r-- 1 febri febri 1076 Jan 1 08:46 shell.c
drwxrwxr-x 2 febri febri 4096 Jan 1 08:16 shellcode
% ./shell
[1] 7465 segmentation fault (core dumped) ./shell
% ls -l
total 20
-rw-rw-r-- 1 febri febri 15 Jan 1 08:46 redirect.txt
-rwxrwxr-x 1 febri febri 7548 Jan 1 08:46 shell
-rw-rw-r-- 1 febri febri 1076 Jan 1 08:46 shell.c
drwxrwxr-x 2 febri febri 4096 Jan 1 08:16 shellcode
% cat redirect.txt
REDIRECT WORKED %
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.