mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-08-16 08:00:26 +02:00
Updated WEEK02
This commit is contained in:
+9
-16
@@ -9,40 +9,33 @@ Introduction and Overview of Embedded Reverse Engineering: Ethics, Scoping, and
|
||||
#### Answers
|
||||
|
||||
##### Question 1: What does the function return?
|
||||
`stdio_init_all()` returns `_Bool`. The function signature shows `_Bool stdio_init_all(void)`.
|
||||
`stdio_init_all()` returns `_Bool`. The function signature is `_Bool stdio_init_all(void)`.
|
||||
|
||||
##### Question 2: What parameters does it take?
|
||||
**None** - the function signature shows `(void)` in parentheses, meaning zero parameters.
|
||||
None. The signature uses `(void)`, which means zero parameters.
|
||||
|
||||
##### Question 3: What functions does it call?
|
||||
`stdio_init_all()` calls initialization functions for:
|
||||
- **UART** initialization (serial pin communication) `stdio_uart_init()`
|
||||
|
||||
These set up the standard I/O subsystem so that `printf()` can output data.
|
||||
In this build, it calls `stdio_uart_init()` to initialize serial output.
|
||||
|
||||
##### Question 4: What's the purpose?
|
||||
`stdio_init_all()` initializes **Standard Input/Output** for the Pico 2:
|
||||
- **std** = Standard
|
||||
- **io** = Input/Output
|
||||
|
||||
It sets up UART communication channels, which allows `printf()` to send output through the serial connection.
|
||||
Its purpose is to initialize standard I/O so `printf()`/`puts()` output can be transmitted over UART.
|
||||
|
||||
##### Expected Output
|
||||
|
||||
```
|
||||
stdio_init_all() returns: void (_Bool)
|
||||
stdio_init_all() returns: _Bool
|
||||
It takes 0 parameters
|
||||
It calls the following functions: UART init
|
||||
Based on these calls, I believe it initializes: Standard I/O for USB and UART serial communication
|
||||
Based on these calls, I believe it initializes: Standard I/O for UART serial communication
|
||||
```
|
||||
|
||||
#### Reflection Answers
|
||||
|
||||
1. **Why would we need to initialize standard I/O before using `printf()`?**
|
||||
Without initialization, there is no communication channel configured. `printf()` needs a destination (USB, in other cases, or UART) to send its output. Without `stdio_init_all()`, output has nowhere to go.
|
||||
Without initialization, there is no configured output path. `printf()` needs a destination (UART in this exercise) to transmit characters.
|
||||
|
||||
2. **Can you find other functions in the Symbol Tree that might be related to I/O?**
|
||||
Yes - functions like `stdio_usb_init`, in other cases, and `stdio_uart_init`, `__wrap_puts`, and other I/O-related functions appear in the Symbol Tree.
|
||||
Yes - `stdio_uart_init`, `__wrap_puts`, and related low-level serial/output helpers are I/O-related.
|
||||
|
||||
3. **How does this function support the `printf("hello, world\r\n")` call in main?**
|
||||
It configures the USB, in other cases, and UART hardware so that when `printf()` (optimized to `__wrap_puts`) executes, the characters are transmitted over the serial connection to the host computer.
|
||||
It configures the UART output path so when `printf()` (optimized to `__wrap_puts`) runs, the string is sent over serial.
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ Once you've navigated to `stdio_init_all`:
|
||||
Based on what you see in the decompiled code, answer the following:
|
||||
|
||||
###### Question 1: What does the function return?
|
||||
Look at the return type at the top of the function. Is it `void`, `int`, `bool`, or something else?
|
||||
Look at the return type at the top of the function. Is it `void`, `int`, `_Bool`, or something else?
|
||||
|
||||
###### Question 2: What parameters does it take?
|
||||
Look at the function signature. Does it take any parameters? (Hint: Look for anything inside the parentheses)
|
||||
|
||||
@@ -25,7 +25,7 @@ The string `"hello, world\r\n"` is located at address **`0x100019CC`** in **Flas
|
||||
- 12 printable characters: `h`, `e`, `l`, `l`, `o`, `,`, ` `, `w`, `o`, `r`, `l`, `d`
|
||||
- 2 special characters: `\r` (carriage return, 0x0D) and `\n` (newline, 0x0A)
|
||||
|
||||
##### Question 3: How many times and which functions reference it?
|
||||
##### Question 3: How many times is it referenced, and by which function(s)?
|
||||
The string is referenced **1 time**, only in the **`main()`** function. The `ldr` instruction at `0x1000023a` loads the string address into register `r0`, which is then passed to `__wrap_puts`.
|
||||
|
||||
##### Question 4: How is the string encoded?
|
||||
|
||||
+7
-8
@@ -82,19 +82,19 @@ To see where this string is **used**:
|
||||
|
||||
Based on what you found:
|
||||
|
||||
###### Question 1: Memory Location
|
||||
###### Question 1: What is the address, and is it Flash or RAM?
|
||||
- What is the address of the "hello, world" string? __________
|
||||
- Is it in Flash memory (starts with `0x100...`) or RAM (starts with `0x200...`)? __________
|
||||
|
||||
###### Question 2: String Storage
|
||||
###### Question 2: How many bytes does the string take?
|
||||
- How many bytes does the string take in memory? __________
|
||||
- Can you count the characters? (h-e-l-l-o-,-space-w-o-r-l-d-\r-\n)
|
||||
|
||||
###### Question 3: References
|
||||
###### Question 3: How many times is it referenced, and by which function(s)?
|
||||
- How many times is this string referenced in the code? __________
|
||||
- Which function(s) reference it? (Hint: Look at the cross-references)
|
||||
|
||||
###### Question 4: ASCII Encoding
|
||||
###### Question 4: How is the string encoded?
|
||||
- How is the string encoded in memory?
|
||||
- Is each character one byte or more? __________
|
||||
- What does `\r` and `\n` represent? (Hint: `\r` = carriage return, `\n` = newline)
|
||||
@@ -133,10 +133,9 @@ Used in: [How the program uses it]
|
||||
|
||||
#### Questions for Reflection
|
||||
|
||||
1. **Why is the string stored in Flash memory?** Why not in RAM?
|
||||
2. **How does `printf()` know where to find the string?** (Hint: The address is loaded into `r0`)
|
||||
3. **What would happen if we didn't have the `\r\n` at the end?** How would the output look?
|
||||
4. **Could we modify this string at runtime?** Why or why not?
|
||||
1. **Why is the string stored in Flash instead of RAM?**
|
||||
2. **What would happen if you tried to modify this string at runtime?**
|
||||
3. **How does the Listing view help you understand string storage?**
|
||||
|
||||
#### Tips and Hints
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ It is a **READ** operation. The `ldr` (Load Register) instruction reads the poin
|
||||
##### Question 4: What happens next after the `ldr`?
|
||||
After the `ldr r0, [DAT_10000244]` instruction loads the string address into `r0`, the next instruction is **`bl 0x100015fc <__wrap_puts>`** which calls the `puts` function with `r0` as its argument (the string pointer).
|
||||
|
||||
##### Question 5: Complete Data Flow Chain
|
||||
##### Complete the Data Flow Chain
|
||||
|
||||
```
|
||||
String "hello, world\r\n" stored at 0x100019CC (Flash)
|
||||
|
||||
+8
-22
@@ -90,30 +90,30 @@ This means:
|
||||
|
||||
##### Step 7: Answer These Questions
|
||||
|
||||
###### Question 1: Data Address
|
||||
###### Question 1: What is the address of the data reference?
|
||||
- What is the address of the data reference you found? (e.g., `DAT_10000244`)
|
||||
- __________
|
||||
|
||||
###### Question 2: Referenced By
|
||||
###### Question 2: How many places reference this data?
|
||||
- How many places reference this data?
|
||||
- __________
|
||||
- Which function(s) use it?
|
||||
- __________
|
||||
|
||||
###### Question 3: Reference Type
|
||||
###### Question 3: Is it a read or write operation? Why?
|
||||
- Is it a read or write operation?
|
||||
- __________
|
||||
- Why? (What's the program doing with this data?)
|
||||
- __________
|
||||
|
||||
###### Question 4: The Chain
|
||||
###### Question 4: What happens next after the `ldr`?
|
||||
- The `ldr` instruction loads an address into `r0`
|
||||
- What happens next? (Hint: Look at the next instruction after the `ldr`)
|
||||
- __________
|
||||
- Is there a function call? If so, which one?
|
||||
- __________
|
||||
|
||||
###### Question 5: Understanding the Flow
|
||||
###### Complete the Data Flow Chain
|
||||
- **`DAT_10000244`** contains the address of the "hello, world" string
|
||||
- The `ldr` loads that address into `r0`
|
||||
- Then a function (probably `printf` or `puts`) is called with `r0` as the argument
|
||||
@@ -146,23 +146,9 @@ Read backwards: `10 00 19 CC` = `0x100019CC`
|
||||
|
||||
#### Questions for Reflection
|
||||
|
||||
1. **Why does the code need to load an address from memory?**
|
||||
- Why can't it just use the address directly?
|
||||
- Hint: Position-independent code and memory protection
|
||||
|
||||
2. **What's the relationship between `DAT_10000244` and the "hello, world" string?**
|
||||
- They're at different addresses - why?
|
||||
- Which is in Flash and which points to where it's stored?
|
||||
|
||||
3. **If we wanted to change what gets printed, where would we modify the code?**
|
||||
- Could we just change the string at address `0x100019CC`?
|
||||
- Or would we need to change `DAT_10000244`?
|
||||
- Or both?
|
||||
|
||||
4. **How does this relate to memory layout?**
|
||||
- Code section (Flash memory starting at `0x10000000`)
|
||||
- Data section (constants/strings)
|
||||
- Is everything at different addresses for a reason?
|
||||
1. **Why does the compiler use an indirect pointer reference here?**
|
||||
2. **What is a literal pool?**
|
||||
3. **How does cross-referencing help in reverse engineering?**
|
||||
|
||||
#### Tips and Hints
|
||||
|
||||
|
||||
@@ -8,23 +8,22 @@ Introduction and Overview of Embedded Reverse Engineering: Ethics, Scoping, and
|
||||
|
||||
#### Answers
|
||||
|
||||
##### Step 1-2 Verification
|
||||
|
||||
- **Was GDB able to connect to OpenOCD?** Yes, via `target extended-remote localhost:3333`.
|
||||
- **Did the program stop at the `main` breakpoint?** Yes, at `Breakpoint 1, main () at ../0x0001_hello-world.c:4`.
|
||||
|
||||
##### Step 3: Answer Exactly
|
||||
|
||||
- **Was GDB able to connect to OpenOCD?**
|
||||
- Yes, via `target extended-remote localhost:3333`.
|
||||
- **Did the program stop at the `main` breakpoint?**
|
||||
- Yes, at `Breakpoint 1, main () at ../0x0001_hello-world.c:4`.
|
||||
- **What is the address of `main`'s first instruction, and is it Flash or RAM?**
|
||||
- `0x10000234`, and it is in **Flash** (`0x100...` XIP region).
|
||||
- **What is the `sp` value at `main`, and is it Flash or RAM?**
|
||||
- `x/s $sp`, the value is, `0x20082000`, and it is in **RAM** (`0x200...` SRAM region).
|
||||
- `0x20082000`, and it is in **RAM** (`0x200...` SRAM region).
|
||||
- **What is the first instruction in `main`, and what does it do?**
|
||||
- `push {r3, lr}`; it saves `r3` and `lr` on the stack and keeps 8-byte stack alignment for ABI-compliant calls.
|
||||
- **Does GDB match what Ghidra shows?**
|
||||
- Yes. The disassembly and flow match the Ghidra listing.
|
||||
|
||||
##### Step 4: Capture Register Values (`pc`, `sp`, `lr`, `r0-r3`, by using `x/x $XX`)
|
||||
##### Step 4: Capture Register Values (`pc`, `sp`, `lr`, `r0-r3`)
|
||||
|
||||
| Register | Value | Description |
|
||||
|----------|-------|-------------|
|
||||
|
||||
+8
-19
@@ -228,30 +228,30 @@ The `pc` should now be at `0x10000236`, which is the next instruction.
|
||||
|
||||
Based on what you've observed:
|
||||
|
||||
###### Question 1: GDB Connection
|
||||
###### Question 1: Was GDB able to connect to OpenOCD?
|
||||
- Was GDB able to connect to OpenOCD? (Yes/No)
|
||||
- Did the program stop at your breakpoint? (Yes/No)
|
||||
- __________
|
||||
|
||||
###### Question 2: Breakpoint Address
|
||||
###### Question 2: What is the address of `main`'s first instruction, and is it Flash or RAM?
|
||||
- What is the memory address of the `main` function's first instruction?
|
||||
- __________
|
||||
- Is this in Flash memory (0x100...) or RAM (0x200...)?
|
||||
- __________
|
||||
|
||||
###### Question 3: Stack Pointer
|
||||
###### Question 3: What is the `sp` value at `main`, and is it Flash or RAM?
|
||||
- What is the value of the Stack Pointer (sp) when you're at `main`?
|
||||
- __________
|
||||
- Is this in Flash or RAM?
|
||||
- __________
|
||||
|
||||
###### Question 4: First Instruction
|
||||
###### Question 4: What is the first instruction in `main`, and what does it do?
|
||||
- What is the first instruction in `main`?
|
||||
- __________
|
||||
- What does it do? (Hint: `push` = save to stack)
|
||||
- __________
|
||||
|
||||
###### Question 5: Disassembly Comparison
|
||||
###### Question 5: Does GDB match what Ghidra shows?
|
||||
- Look at the disassembly from GDB (Step 7)
|
||||
- Compare it to the disassembly from Ghidra (Exercise 1)
|
||||
- Are they the same?
|
||||
@@ -289,20 +289,9 @@ This is useful when you want to break on a condition rather than every time.
|
||||
|
||||
#### Questions for Reflection
|
||||
|
||||
1. **Why does GDB show both the C source line AND the assembly?**
|
||||
- This is because the .elf file contains debug symbols
|
||||
- What would happen if we used a stripped binary?
|
||||
|
||||
2. **How does GDB know the assembly for each instruction?**
|
||||
- It disassembles the binary on-the-fly based on the architecture
|
||||
|
||||
3. **Why is the Stack Pointer so high (0x20082000)?**
|
||||
- It's at the top of RAM and grows downward
|
||||
- Can you calculate how much RAM this Pico 2 has?
|
||||
|
||||
4. **What's the difference between `si` (step into) and `ni` (next instruction)?**
|
||||
- `si` steps into function calls
|
||||
- `ni` executes entire functions without stopping inside them
|
||||
1. **Why does the stack pointer start where it does?**
|
||||
2. **Why does `push {r3, lr}` include `r3`?**
|
||||
3. **How does the infinite loop work in assembly?**
|
||||
|
||||
#### Important GDB Commands Reference
|
||||
|
||||
|
||||
+7
-5
@@ -616,11 +616,13 @@ Ghidra shows you two views of the code:
|
||||
|
||||
**Decompile View (Right Panel)** - The reconstructed C code:
|
||||
```c
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
do {
|
||||
__wrap_puts("hello, world");
|
||||
} while (true);
|
||||
int main(void)
|
||||
|
||||
{
|
||||
stdio_init_all();
|
||||
do {
|
||||
__wrap_puts("hello, world\r");
|
||||
} while( true );
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user