Skip to content

Commit e894213

Browse files
author
ryan nemeth
committed
adding post
1 parent f0fb986 commit e894213

File tree

489 files changed

+25564
-17492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

489 files changed

+25564
-17492
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
[submodule "themes/hugo-coder"]
2020
path = themes/hugo-coder
2121
url = https://github.com/rnemeth90/hugo-coder.git
22+
[submodule "themes/book"]
23+
path = themes/book
24+
url = https://github.com/alex-shpak/hugo-book

config.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ languageCode = "en-us"
22
name = "GeekyRyan"
33
baseUrl = "https://rnemeth90.github.io"
44
title = "GeekyRyan"
5-
theme = "hugo-coder"
5+
# theme = ["hugo-coder", "book"]
66
enableRobotsTXT = true
77

88
[pagination]
@@ -99,3 +99,12 @@ url = "projects/"
9999
name = "Tags"
100100
url = "/tags/"
101101
weight = 5
102+
103+
# [module]
104+
# [[module.imports]]
105+
# path = "github.com/alex-shpak/hugo-book"
106+
#
107+
# [[module.imports.mounts]]
108+
# source = "themes/book/layouts"
109+
# target = "layouts/notes"
110+
#
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: "Modify Machine Code in Executables"
3+
date: '2025-11-26T16:01:00+00:00'
4+
author: Ryan
5+
layout: post
6+
draft: false
7+
categories:
8+
- Programming
9+
- Systems
10+
- Assembly
11+
- Reverse Engineering
12+
---
13+
14+
# Modifying Machine Code in Executables
15+
16+
In this post, we’ll walk through a simple but fun reverse-engineering exercise: taking a compiled C program, locating the machine instructions responsible for printing characters, and modifying those bytes directly in the executable.
17+
18+
This is a great way to build intuition around how compilers translate code, how functions map to assembly, and how tools like `objdump` and `xxd` help us inspect and patch binaries.
19+
20+
---
21+
22+
## The Source Program
23+
24+
We’ll start with a minimal C program that prints `"ab\n"`:
25+
26+
```c
27+
#include <stdio.h>
28+
29+
int main() {
30+
putchar('a');
31+
putchar('b');
32+
putchar('\n');
33+
34+
return 0;
35+
}
36+
```
37+
38+
Compile it normally:
39+
40+
```bash
41+
gcc -o main main.c
42+
```
43+
44+
---
45+
46+
## Inspecting the Binary with `objdump`
47+
48+
Let’s look at the generated machine code:
49+
50+
```bash
51+
objdump -d main
52+
```
53+
54+
This produces a lot of output (as expected), but the portion we care about is the `main` symbol. We can isolate it:
55+
56+
```bash
57+
objdump --disassemble=main -f main
58+
```
59+
60+
The disassembly for `main` looks like this:
61+
62+
```bash
63+
0000000000001139 <main>:
64+
1139: 55 push %rbp
65+
113a: 48 89 e5 mov %rsp,%rbp
66+
113d: bf 61 00 00 00 mov $0x61,%edi
67+
1142: e8 e9 fe ff ff call 1030 <putchar@plt>
68+
1147: bf 62 00 00 00 mov $0x62,%edi
69+
114c: e8 df fe ff ff call 1030 <putchar@plt>
70+
1151: bf 0a 00 00 00 mov $0x0a,%edi
71+
1156: e8 d5 fe ff ff call 1030 <putchar@plt>
72+
115b: b8 00 00 00 00 mov $0x0,%eax
73+
1160: 5d pop %rbp
74+
1161: c3 ret
75+
```
76+
77+
Each call to `putchar` is preceded by a `mov $VALUE, %edi`, where `%edi` contains the character to print.
78+
79+
- `0x61` > `'a'`
80+
- `0x62` > `'b'`
81+
- `0x0a` > newline
82+
83+
If we want the program to print `"ac"` instead of `"ab"`, we need to modify the instruction at `0x1147` so that it loads `0x63` (ASCII `'c'`) instead of `0x62`.
84+
85+
---
86+
87+
## Producing a Hex Dump
88+
89+
To patch the executable, we’ll create a hex dump:
90+
91+
```bash
92+
xxd main > main.asm
93+
```
94+
95+
Here’s the relevant portion (trimmed for clarity):
96+
97+
```bash
98+
00001140: 0000 e8e9 feff ffbf 6200 0000 e8df feff ........b....... < HERE
99+
```
100+
101+
The byte at address `0x1147` falls 14 bytes (`0xE`) into this block.
102+
That `62` is the value we want to change.
103+
104+
We want this:
105+
106+
```bash
107+
00001140: 0000 e8e9 feff ffbf 6300 0000 e8df feff ........c.......
108+
```
109+
110+
Notice that only **one byte** changes: `0x62` > `0x63`.
111+
112+
---
113+
114+
## Writing the Modified Binary Back
115+
116+
Save the modified hex dump, then run:
117+
118+
```bash
119+
xxd -r main.asm modified_main
120+
```
121+
122+
Now run it:
123+
124+
```bash
125+
./modified_main
126+
ac
127+
```
128+
129+
Success. We changed the program’s behavior **without recompiling**, simply by patching the machine code. This is the foundation for advanced topics like reverse engineering, binary instrumentation, and exploit development. If you'd like to extend this example, such as patching instructions of different sizes, modifying control flow, or injecting new code, let me know. I’m happy to build on this.
130+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/rnemeth90/rnemeth90.github.io
22

33
go 1.18
44

5-
require github.com/Mitrichius/hugo-theme-anubis v0.0.0-20220725170731-d4ba47cd2196 // indirect
5+
require github.com/alex-shpak/hugo-book v0.0.0-20251117144900-e08d9b796d90 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/Mitrichius/hugo-theme-anubis v0.0.0-20220725170731-d4ba47cd2196 h1:TzahMQaJXpezO9D2iQHnifGWPUn+41IWgYNBg/wNtQE=
2-
github.com/Mitrichius/hugo-theme-anubis v0.0.0-20220725170731-d4ba47cd2196/go.mod h1:FKD3NbdsQzL+cXsC3x99XD/t/Rd5EyhDlNBAxQhULbo=
1+
github.com/alex-shpak/hugo-book v0.0.0-20251117144900-e08d9b796d90 h1:1Wb766TGSqPG0inwhdfx8cbKALB1/kP4lxOOfCOxPco=
2+
github.com/alex-shpak/hugo-book v0.0.0-20251117144900-e08d9b796d90/go.mod h1:L4NMyzbn15fpLIpmmtDg9ZFFyTZzw87/lk7M2bMQ7ds=

public/.DS_Store

-6 KB
Binary file not shown.

0 commit comments

Comments
 (0)