Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run assembly in gdb directly?

Tags:

assembly

gdb

I can use call to run c functions ,but how to run assembly or even shell code directly?

like image 756
compiler Avatar asked Apr 14 '11 09:04

compiler


People also ask

How to show assembly code in gdb?

From within gdb press Ctrl x 2 and the screen will split into 3 parts. First part will show you the normal code in high level language. Second will show you the assembly equivalent and corresponding instruction Pointer .

How do I debug an assembly code?

You start debugging by clicking Start Debugging on the Debug menu. On the Start Debugging dialog box, check Enable Assembler debugging, then click OK. If you debug the module again during the same session, you can start it by clicking Start Debugging, Run or Debug.

How do I run a line in GDB?

To execute one line of code, type "step" or "s". If the line to be executed is a function call, gdb will step into that function and start executing its code one line at a time. If you want to execute the entire function with one keypress, type "next" or "n".

What does disassemble do in gdb?

If AUTO, GDB will display disassembly of next instruction only if the source line cannot be displayed. This setting causes GDB to display some feedback when you step through a function with no line info or whose source file is unavailable.


1 Answers

To execute shell code, you can edit a function's contents directly:

(gdb) b foo
Breakpoint 1 at 0x400608
(gdb) run
Breakpoint 1, 0x0000000000400608 in foo ()
(gdb) x/16bx foo
0x400604 <foo>:        0x55    0x48    0x89    0xe5    0x53    0x48    0x81   0xec
(gdb) set ((unsigned char *)foo)[6] = 0x85
(gdb) x/16bx foo
0x400604 <foo>:        0x55    0x48    0x89    0xe5    0x53    0x48    0x85   0xec
(gdb) cont

I don't know how to execute opcodes from within gdb, but you can certainly do whatever you want with registers. For instance, instead of mov %rbx, %rax you can use set $rax = $rbx:

(gdb) p $rax
$1 = 3671197290184
(gdb) set $rax = $rbx
(gdb) p $rax
$2 = 0
(gdb)
like image 114
sam hocevar Avatar answered Sep 20 '22 16:09

sam hocevar