-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinlineassemblyforlinux.c
74 lines (50 loc) · 2.25 KB
/
inlineassemblyforlinux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//inline assembly
#include <stdio.h>
void other_function();
void other_function_2();
void (*backup)(); // backup address
int main() {
int i = 0x13; // we expect this to be on the stack
int j = 0;
__asm(".intel_syntax noprefix;" // intel is my preference
"mov ecx, eax;" // move i from eax into ecx
"inc ecx;"
".att_syntax prefix"
:"=c"(j) // outputs "c" -> ecx into j
:"a"(i) // the "a" -> i into eax
// clobbered registers, none
);
printf("Our i value is: %d\n", i); // we expect decimal 19
printf("Our j value is: %d\n\n", j); // we expect decimal 20
printf("Calling other_function...\n");
printf("-------------------------\n");
other_function(); // call other_function
printf("-------------------------\n");
return 0;
}
void other_function_2(){ // this will NOT execute
// we'll set our return
// here with inline assembly
printf("THIS IS other_function_2!\n");
}
void other_function(){
void (*p)();
p = other_function_2; // address of other_function_2
__asm(".intel_syntax noprefix;" // spoof stack frame
"mov %0, [rbp+0x8];"
"mov [rbp+0x8], %1;"
".att_syntax prefix" // ..._2 stack frame
:"=c"(backup)
:"a"(p)
);
printf("THIS IS other_function!\n"); // frame has other_function_2
__asm(".intel_syntax noprefix;" // restore old address
"mov [rbp+0x8], %0;"
".att_syntax prefix"
:
:"r"(backup)
);
return;
}
//gcc inlineassemblyforlinux.c -o inlineassemblyforlinux
//from:https://blog.malicious.group/inline-assembly/