BINARY PATCHING

This method is used to flip the logic of a program to the more interesting parts of the code.

This is the process of modifying a compiled executable at the byte level to alter its behavior without access to the original source code. This method is commonly used in software reverse engineering to bypass checks, change program logic, or redirect execution flow to more interesting parts of the code such as hidden features or protected routines. By identifying and replacing key instructions (e.g., changing a JE to a JNE), reverse engineers can flip conditions, disable security mechanisms, or force specific code paths. This method requires a solid understanding of assembly, file formats, and the program's control flow to ensure stability and effectiveness.

SOURCE

#C SAMPLE CODE SNIPPET
root@dev:~$ visualstudio > 14_scanf_error_check.c

#include <stdio.h>
int main()
{
  int x;
  printf("Enter X:\n");
  
  if(scanf("%d", %x)==1)
    printf("You entered %d...\n", x);
  else
    printf("Woof.\n");
  return 0;
}

PS C:> cl /MD /Od /Zi /FA 14_scanf_error_check.c
 * ALT: cl /MD /Od /Zi /FA /Fa14_scanf_error_check_x64 /Fe14_scanf_error_check_x64 /Fo14_scanf_error_check_x64 14_scanf_error_check.c

PROCEDURE

In this example, binary patching will be used to display BOTH output of the printf statement regardless of whether the user input is valid. Aside from this, the main point is to make the changes persistent.

STEP 0: IMPORT AND AUTO-ANALYZE PROJECT FILE

STEP 1: BIG PICTURE OVERVIEW

STEP 2: REBASE

REBASE: IDENTIFY BASE ADDRESS (WINDBG)

REBASE: APPLY IDENTIFIED BASE ADDRESS (GHIDRA)

Ghidra must be set to the same "base address" found by WinDBG. When the interesting code parts are found and identified, no address translation will be required, as the addresses in WinDBG and Ghidra will match.

STEP 3: HUNT MAIN()

IDENTIFY PATTERNS (RUNNING PROGRAM)

this may not always be accurate

LOCATE PATTERNS (GHIDRA)

when you find the function that has similar patterns when the program is run, this could be your clue

STEP 4: LOCATE PERTINENT INSTRUCTION

Last updated