Thursday, June 25, 2026
HomeCyber SecurityReversing a binary utilizing GDB: tutorial for Reverse Engineers

Reversing a binary utilizing GDB: tutorial for Reverse Engineers

[ad_1]

This weblog was written by an impartial visitor blogger.

Reversing binaries is a necessary talent if you wish to pursue a profession as exploit developer, reverse engineer or programming. The GNU Venture debugger is a broadly used debugger for debugging C and C++ functions on UNIX programs. A debugger is a developer’s greatest good friend to determine software program bugs and points. 

This tutorial intends to be useful to all builders who wish to create dependable and fault-free software program.

A debugger executes a number of packages and permits the programmer to handle them and analyze variables in the event that they trigger points.

GDB permits us to execute this system till it reaches a selected level. It could then output the contents of chosen variables at that second, or stroll via this system line by line and print the values of each parameter after each line executes. It has a command-line interface.

Let’s perceive GNU debugger with an instance

To put in the GDB within the Linux system, kind within the command to put in GDB.

install GDB

The code I’m utilizing for an instance right here is calculating factorial numbers inaccurately. The goal is to determine why the difficulty occurred.

#embody     

utilizing namespace std;     

lengthy factorial(int n);     

int fundamental()

{
    int n(0);

    cin>>n;

    lengthy val=factorial(n);

    cout<
			;>

GCC is a Linux compiler that comes pre-installed in Linux. Use the “g++” command to transform the supply code “take a look at.cpp” into an executable “fundamental.” Use “-g flag” so you may debug the code later as effectively.

GCC

Begin the GDB with the executable filename within the terminal.

Start GDB

You may seemingly want the code to cease at one stage so you may assess its standing. The breakpoint is the road the place you need the code to halt momentarily. On this situation, I’m setting a breakpoint on line 11 and working this system.

Set breakpoint

The instructions “subsequent” and “step” in GDB execute the code line by line.

  • The Step command screens the execution by way of perform calls.
  • The Subsequent command retains management solely within the present scope.

Utilizing “watchpoints” is akin to requesting the debugger to provide you a relentless stream of details about any modifications to the variables. The software program stops when an replace occurs and informs you of the specifics of the change.

Right here, we set the watchpoints for the calculation’s end result and the enter worth because it fluctuates. Final, the outcomes of the watchpoints are analyzed to establish any irregular exercise.

watchpoints in code

Discover the end in “previous” and “new” values.  To constantly discover the shift in values, press the Enter key.

Discover that “n” instantaneously reduces from 3 to 2.

By multiplying the earlier worth of the outcome by the “n” worth, the outcome now equals 2. The primary bug has been noticed!

It ought to assess the end result by multiplying 3 * 2 * 1. Nevertheless, the multiplication right here begins at 2. We’ll have to change the loop just a little to repair that.

Continue watchpoints

The result’s now 0. One other bug!

So, when 0 multiplies with the factorial, how can the output maintain the factorial worth? It have to be that loop halts earlier than “n” approaches 0.

When “n” values shift to -1, the loop could not execute anymore. Subsequent, name the perform. Discover when a variable is out of scope, watchpoint deletes it.

GDB continues

Inspecting native variables to find out whether or not something uncommon has occurred would possibly provide help to find the problematic part of your code. Since GDB refers to a line earlier than it runs, the “print val” command returns a trash worth.

Graphical user interface, application

Description automatically generated

Final, the error-free code would appear to be this.

examine values in GDB

To completely comprehend what the debugger is doing, study the meeting code and what’s taking place in reminiscence.

Use the “disass” command to output the checklist of Meeting directions. GDB’s default disassembly type is AT&T, which could perplex Home windows customers as this type is for Linux. When you don’t desire this, the disassembly type will be re-set as effectively.

error free code

Execute the “set disassembly-flavor command to vary to the Intel disassembly type.

disass GDB

The logic movement is vital to the success of any program. The movement of Meeting code will be easy or sophisticated, based mostly on the compiler and settings used throughout compiling.

  • The primary three traces are Reminiscence Setup.

 

  • DWORD PTR -> Transfer the worth at [rdp-0xc] and skim bytes.

 

  • QWORD PTR -> QWORD operand describes an deal with of quod-word dimension in Intel’s x86 household of processors; this implies 8 bytes. And PTR signifies the worth of the operand that must be handled as an deal with.

disassembly in GDB

In a nutshell

After ending this tutorial, you need to have a strong information of utilizing GNU Debugger to debug a C or C++ packages.

We took an instance of C++ code and tried a couple of issues with GDB. However if you wish to discover it additional, use the “assist” choice within the “(gdb) interface.”

Irfan Shakeel

In regards to the Writer: Irfan Shakeel, EH Academy

Irfan Shakeel is the founding father of ehacking.internet and creates future cyber safety professionals by providing high quality cyber safety training at EH Academy. You possibly can join with him on Twitter (@irfaanshakeel) and LinkedIn.

Learn extra posts from Irfan Shakeel ›

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments