Capstone: hex editor
The goal
Build a hex dumper: hexdump file prints offset, hex bytes, and ASCII side by side, and hexpatch file offset byte patches one byte. It uses open, read, write, close, and a hex-digit lookup table.
Design
Read the file in 16-byte chunks into a .bss buffer. For each chunk, print the 8-digit hex offset, two spaces, then each byte as two hex digits from a table db "0123456789abcdef", then the printable characters with a dot for non-printables. The patcher opens O_RDWR (2), seeks with lseek (8) to the offset, writes one byte, and closes:
default rel %define SYS_READ 0 %define SYS_WRITE 1 %define SYS_OPEN 2 %define SYS_CLOSE 3 %define SYS_EXIT 60 %define O_RDONLY 0 section .data hexdigits db "0123456789abcdef" section .bss buf resb 16 line resb 80 section .text global _start _start: pop r12 ; argc cmp r12, 2 jl usage pop rdi ; argv[0] pop rdi ; argv[1] = file name mov rax, SYS_OPEN mov rsi, O_RDONLY xor rdx, rdx syscall test rax, rax js failed mov r12, rax ; fd xor r13, r13 ; offset read_loop: mov rax, SYS_READ mov rdi, r12 lea rsi, [buf] mov rdx, 16 syscall test rax, rax jle done mov r14, rax ; bytes in this chunk lea rdi, [line] mov rcx, 28 offset_digits: mov rax, r13 shr rax, cl and eax, 0xf lea rsi, [hexdigits] mov al, [rsi + rax] stosb sub rcx, 4 jns offset_digits mov al, ":" stosb mov al, " " stosb xor r15, r15 hex_loop: cmp r15, 16 jae hex_done lea rsi, [buf] cmp r15, r14 jae hex_blank movzx eax, byte [rsi + r15] mov edx, eax shr eax, 4 and edx, 0xf lea rcx, [hexdigits] mov al, [rcx + rax] stosb mov al, [rcx + rdx] stosb mov al, " " stosb jmp hex_next hex_blank: mov al, " " stosb stosb stosb hex_next: inc r15 jmp hex_loop hex_done: mov al, " " stosb xor r15, r15 ascii_loop: cmp r15, r14 jae ascii_done lea rsi, [buf] mov al, [rsi + r15] cmp al, 32 jb ascii_dot cmp al, 126 ja ascii_dot jmp ascii_put ascii_dot: mov al, "." ascii_put: stosb inc r15 jmp ascii_loop ascii_done: mov al, 10 stosb lea rsi, [line] mov rdx, rdi sub rdx, rsi mov rax, SYS_WRITE mov rdi, 1 syscall add r13, r14 jmp read_loop done: mov rax, SYS_CLOSE mov rdi, r12 syscall mov rax, SYS_EXIT xor rdi, rdi syscall failed: mov rax, SYS_EXIT mov rdi, 1 syscall usage: mov rax, SYS_EXIT mov rdi, 2 syscall
echo hello > sample.txt
nasm -f elf64 -o hexdump.o hexdump.asm
ld -o hexdump hexdump.o
./hexdump sample.txt
echo $?
00000000: 68 65 6c 6c 6f 0a hello.
0
Build steps
Build the dumper first: open, loop read-until-zero, format each 16-byte row, close. Then the patcher: validate the offset and the byte value (two hex digits), open O_RDWR, lseek, write, close. Test on a copy of a text file, then diff the patched copy against the original to confirm exactly one byte changed.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many bytes does one hexdump row show?
Answer
16
The dumper reads 16 bytes per row, the classic hexdump width.
-
What does the leftmost column of a hexdump show?
Answer
The offset of the first byte in the row
The eight hex digits give the position of the row in the file.
-
Which table turns a nibble into a hex character?
Answer
db "0123456789abcdef"
The byte value indexes the table, and the table holds the sixteen digits.
-
What does the dumper print for a non-printable byte in the ASCII column?
Answer
A dot
Bytes below 32 or above 126 would break the line, so a dot marks them.
-
Which syscall reads the file in chunks?
Answer
read (0)
read takes the descriptor, the buffer and the byte count.
-
What ends the read loop?
Answer
read returns 0
Zero bytes read means the file is at its end.
-
What does the sample file
hellofollowed by a newline print in the hex area?Answer
68 65 6c 6c 6f 0a
The five letters and the newline are six bytes, each shown as two hex digits.
-
What does the ASCII column show for that sample?
Answer
hello.
The letters are printable, and the newline byte becomes a dot.
-
Which syscall moves the file position for the patcher?
Answer
lseek (8)
lseek sets the offset before the one-byte write.
-
Why does the patcher open the file with O_RDWR?
Answer
It must read and write the same file
The patch reads the byte, writes a new one and the file stays open for both.
The hex dumper was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container. The hexpatch program is described in the build steps and is not shown as code; the output above is the dumper on sample.txt.
Article author: Arthur Isaev