Latest / CyberCode Academy / Course 33 - Static Analysis for Reverse Engineering | Episode 4: Static Analysis and Software Patching in x64dbg
Transcript
- 0:00Speaker 1: I want you to imagine, just for a second, having
- 0:02Speaker 1: the power to bend the rules of time, well, at
- 0:06Speaker 1: least digital time. Have you ever stalled the piece of software,
- 0:10Speaker 1: stared at that little window that pops up saying, you know,
- 0:13Speaker 1: thirty days remaining in your trial, and wondered how the
- 0:16Speaker 1: program actually knows that.
- 0:17Speaker 2: I mean, we always assume it's this impenetrable vault of logic.
- 0:20Speaker 1: Exactly, But what if you could look under the hood,
- 0:23Speaker 1: rewrite the logic, and just tell the application that you
- 0:26Speaker 1: actually have two hundred and forty five days left.
- 0:29Speaker 2: It's a huge shift in perspective. We're conditioned to treat
- 0:32Speaker 2: software as a black box, but the reality of compiled
- 0:35Speaker 2: code is well, it's far more mechanical. Wait, what looks
- 0:39Speaker 2: like an unyielding restriction on the UI is often just
- 0:42Speaker 2: a simple mathematical comparison, and it's totally vulnerable to modification
- 0:47Speaker 2: if you know how to read the processor's instructions.
- 0:50Speaker 1: Okay, let's unpack this. Our mission today is a deep
- 0:53Speaker 1: dive into the fascinating world of static analysis and file patching.
- 0:57Speaker 2: Yeah, a real step by step masterclass for you today.
- 1:00Speaker 1: Whether you are a cybersecurity student, a self taught reverse
- 1:04Speaker 1: engineer or just someone curious about malware analysis. We are
- 1:08Speaker 1: walking you through the exact technical workflow of reverse engineering
- 1:13Speaker 1: a trial period, and.
- 1:14Speaker 2: We are using a custom educational binary called crack me
- 1:18Speaker 2: four as our training ground.
- 1:19Speaker 1: Which is perfect right because it trips away all the
- 1:22Speaker 1: noise of a massive enterprise application exactly.
- 1:26Speaker 2: It isolates the core licensing mechanics. When you run it normally,
- 1:30Speaker 2: it just shows a standard thirty day trial countdown.
- 1:33Speaker 1: So the objective here is to analyze the binary, find
- 1:35Speaker 1: that enforcement mechanism, and just patch the file to permanently
- 1:38Speaker 1: extend the trial.
- 1:40Speaker 2: But before we can change the logic, we have to
- 1:42Speaker 2: catch the software in the act. We need to observe
- 1:44Speaker 2: the binary, right, which means.
- 1:45Speaker 1: Getting our environment ready. We are going to load crack
- 1:48Speaker 1: me for into a debugger called buy sixty four DBG.
- 1:52Speaker 1: But we don't just hit play.
- 1:55Speaker 2: Oh, definitely not. You can't just run it. You have
- 1:56Speaker 2: to configure the debugger first to handle any evasion techniques.
- 2:00Speaker 1: Because developers know their apps are being scrutinized, right.
- 2:03Speaker 2: So in by sixty four DBG, we go to the
- 2:05Speaker 2: options and enable the system breakpoint that pauses execution at
- 2:10Speaker 2: the very earliest operating system level.
- 2:13Speaker 1: And crucially, we also need to check the box for
- 2:15Speaker 1: TLS callbacks. Actually want to highlight that for a second.
- 2:18Speaker 2: Yeah, TLS callbacks are sneaky.
- 2:19Speaker 1: TLS stands for a thread local storage. Usually you expect
- 2:23Speaker 1: a program to start at the entry point or the
- 2:25Speaker 1: main function, but developers can use these callbacks to run
- 2:29Speaker 1: code before the main program even starts.
- 2:31Speaker 2: It's a classic anti debugging trap. A developer might put
- 2:35Speaker 2: a function in there that queries the Windows API, like
- 2:38Speaker 2: using is debugger.
- 2:39Speaker 1: Present, just to check if we're watching exactly, and.
- 2:42Speaker 2: If it sees by sixty four dbg, it silently terminates
- 2:45Speaker 2: before you even get to line one of the main code.
- 2:47Speaker 1: Wow. So by pausing on TLS callbacks, we intercept those
- 2:50Speaker 1: traps before they can even.
- 2:51Speaker 2: Spring, right, And we also need to configure our exception ranges.
- 2:55Speaker 2: We have to tell the debugger to ignore exceptions marked
- 2:58Speaker 2: as eight zeros and the range of eight fs.
- 3:01Speaker 1: Because the operating system is constantly throwing background exceptions, right,
- 3:05Speaker 1: just doing normal memory management.
- 3:06Speaker 2: Yeah, just background noise. If we don't filter those hex
- 3:09Speaker 2: ranges out.
- 3:10Speaker 1: Our debugger is just gonna pause every single microsecond exactly.
- 3:13Speaker 2: It makes analysis completely impossible. Filtering keeps our focus purely
- 3:17Speaker 2: on the applications logic.
- 3:18Speaker 1: Okay, so the environment is tuned. We let the program run.
- 3:22Speaker 1: The UI loads up, and we see our targeted message
- 3:24Speaker 1: that says twenty days trial period remaining.
- 3:27Speaker 2: We have our baseline, but now we're staring at the
- 3:30Speaker 2: raw disassembler view, which is just this massive wall of
- 3:35Speaker 2: hexadecimal addresses and assembling knemonics.
- 3:37Speaker 1: It's overwhelming. You definitely don't read it top to bottom.
- 3:40Speaker 2: No, you'd be there for years. Instead, we use a
- 3:43Speaker 2: reverse mapping technique.
- 3:44Speaker 1: Right because the software just printed a highly specific text
- 3:47Speaker 1: string to the screen, we use that user facing text
- 3:50Speaker 1: as our breadcrumb.
- 3:51Speaker 2: User facing text is honestly the ultimate vulnerability and software design.
- 3:56Speaker 2: No matter how complex the math is, it has to
- 3:59Speaker 2: talk to a human eventually.
- 4:00Speaker 1: So we take the word trial from that pop up
- 4:03Speaker 1: in the debugger. We write click and search for current
- 4:06Speaker 1: module string references.
- 4:07Speaker 2: And then the debugger scans the data sections of the
- 4:10Speaker 2: file where all the static text strings live in memory, and.
- 4:14Speaker 1: The code section has pointers to those memory addresses. So
- 4:17Speaker 1: the search finishes and we get our hits. We see
- 4:20Speaker 1: a reference to trial period expired and one for trial
- 4:24Speaker 1: period remaining.
- 4:25Speaker 2: Right, so we double click trial period remaining and boom,
- 4:28Speaker 2: the debugger instantly jumps us to the exact line of
- 4:30Speaker 2: code where that text is referenced.
- 4:32Speaker 1: It's amazing. We just bypassed thousands of lines of UI
- 4:35Speaker 1: rendering and OS calls, landing straight in the licensing function.
- 4:38Speaker 2: We found the front door. But we still need to
- 4:42Speaker 2: map out the architecture of the house because.
- 4:44Speaker 1: Raw symboly is dense. It's just vertical instructions. Move this,
- 4:48Speaker 1: push that, call this. It's hard to see the logic.
- 4:51Speaker 2: Yeah, so we need to visualize it. We use a
- 4:53Speaker 2: plug in in by sixty forty BG called x Analyzer.
- 4:56Speaker 1: And we run analyzed module exactly.
- 4:59Speaker 2: The plug in scans for patterns like function prologs. It
- 5:02Speaker 2: draws these thick black structural lines next to the code,
- 5:05Speaker 2: grouping the instructions into a distinct function.
- 5:08Speaker 1: And once we have those boundaries, we highlight the top
- 5:10Speaker 1: of it and press G on the keyboard. This is
- 5:13Speaker 1: where it gets fine.
- 5:13Speaker 2: Yeah, the graphical visualizer. It totally changes the game. It
- 5:16Speaker 2: takes that vertical list and turns it into a control
- 5:19Speaker 2: flow graph or CFG.
- 5:20Speaker 1: Basically a float chart. It breaks the function down into
- 5:23Speaker 1: blocks of instructions, and when there's a branch, it draws
- 5:26Speaker 1: lines between.
- 5:27Speaker 2: Them and you immediately see this critical decision point. One
- 5:31Speaker 2: block at the top splits into two paths.
- 5:34Speaker 1: Color coded two, a green path leading to our day's
- 5:38Speaker 1: remaining string and a red path going to the trial
- 5:41Speaker 1: expired string.
- 5:42Speaker 2: And sitting right at that fork in the road is
- 5:44Speaker 2: a conditional jump.
- 5:45Speaker 1: Instruction JG jump if greater.
- 5:47Speaker 2: Right in by eighty six architecture, the CPU makes decisions
- 5:51Speaker 2: using an internal set of flags. The instruction right before
- 5:54Speaker 2: the JG does a comparison, which updates those.
- 5:57Speaker 1: Flags, like the zero flag or the sign flag exactly.
- 5:59Speaker 2: The instruction just looks at those flags and says do
- 6:02Speaker 2: I jump or not?
- 6:03Speaker 1: In this specific block, it's looking at a register called ecx.
- 6:06Speaker 1: It compares ecx to zero yep.
- 6:09Speaker 2: If ecx is greater than zero, say it's fifteen or twenty,
- 6:12Speaker 2: it's evaluated as true. It takes the jump, goes down
- 6:15Speaker 2: the green path, and the software works.
- 6:17Speaker 1: But if it's zero or negative, the jump fails. It
- 6:21Speaker 1: falls through to the red path and you're locked out.
- 6:24Speaker 2: Wait, I have to pause here. Sure, looking at this
- 6:26Speaker 2: from a developer standpoint, if it's literally just a plaintext
- 6:30Speaker 2: greater than zero check at the end of a function.
- 6:33Speaker 2: Why not obfuscate that?
- 6:35Speaker 1: I mean they try to because it just.
- 6:36Speaker 2: Seems like leaving the front door totally unlocked. Why not
- 6:40Speaker 2: encrypt this single point of failure.
- 6:42Speaker 1: Oh, developers absolutely use commercial packers, virtual machines heavy obfuscation.
- 6:47Speaker 1: But here's the fundamental constraint of computer architecture.
- 6:50Speaker 2: The processor needs raw instructions.
- 6:52Speaker 1: Exactly to actually perform math. The CPU needs unencrypted instructions
- 6:57Speaker 1: in its registers. You can hide the logic in a
- 7:00Speaker 1: massive labyrinth, but at the moment.
- 7:02Speaker 2: Of execution, the minotaur at the center is always just
- 7:04Speaker 2: a basic binary comparison.
- 7:06Speaker 1: Yes, true or false, greater than or equal to, And
- 7:09Speaker 1: we caught the logic in its decrypted state inside the debugger.
- 7:12Speaker 2: That perfectly highlights the power of dynamic analysis. So if
- 7:16Speaker 2: keeping the program unlocked just means keeping ecx greater than zero,
- 7:19Speaker 2: we just need to trace it backward. Right, we scroll
- 7:22Speaker 2: up the flow chart to see how it calculates that number.
- 7:24Speaker 2: In the first place, we.
- 7:25Speaker 1: Look at the block right above the decision point, and
- 7:28Speaker 1: we find a move instruction. It moves a x value
- 7:31Speaker 1: into ecx. That value is.
- 7:33Speaker 2: One e and one e in hexadecimal translates to exactly
- 7:37Speaker 2: thirty in decimal.
- 7:38Speaker 1: The thirty day trial. They hard coded their life span
- 7:41Speaker 1: right into the function YEP.
- 7:42Speaker 2: It initializes ecx with thirty and right below that there's
- 7:46Speaker 2: a subtraction instruction.
- 7:48Speaker 1: It takes that one e and it subtracts the value
- 7:50Speaker 1: of a different register, EAX.
- 7:52Speaker 2: And by looking at the debugger state, we can deduce
- 7:55Speaker 2: what EAX is. It's the current date.
- 7:57Speaker 1: So if today is July tenth, eaxx ten.
- 8:00Speaker 2: So the math is elementary. Thirty minus ten is twenty and.
- 8:03Speaker 1: Twenty goes into ecx. It hits the JG instruction is
- 8:07Speaker 1: twenty greater than zero. Yes, green path twenty days remaining.
- 8:10Speaker 2: It's just simple arithmetic.
- 8:12Speaker 1: But quick question, why do programs use registers like eax
- 8:15Speaker 1: and ecx for this? Why not just read the variables
- 8:18Speaker 1: straight from the computer's RAM.
- 8:19Speaker 2: It comes down to hardware latency. Registers are physical storage
- 8:23Speaker 2: etched directly onto the CPUDE. They run at the processor's clock.
- 8:26Speaker 1: Speed, which is blisteringly fast.
- 8:28Speaker 2: Right RAM is off the die. Fetching data from RAM
- 8:31Speaker 2: takes hundreds of clock cycles, so to do.
- 8:34Speaker 1: The subtraction, you have to load it into the CPU first.
- 8:36Speaker 2: Exactly, you load the data into the arithmetic logic unit.
- 8:40Speaker 2: The assembly code is just a direct map of those
- 8:43Speaker 2: electrical transfers.
- 8:44Speaker 1: That makes total sense. Okay, so we've reverse engineered the formula.
- 8:47Speaker 1: We know it starts at one E, subtracts the date,
- 8:50Speaker 1: and checks if it's greater.
- 8:51Speaker 2: Than zero, which brings us to the actual heist the
- 8:55Speaker 2: file patching.
- 8:56Speaker 1: Yes, we're going to rewrite the variables. We want ecx
- 8:59Speaker 1: to stay positive for a very long time.
- 9:01Speaker 2: So we go to the dissembler, target that instruction moving
- 9:04Speaker 2: one E into ecx, and we double click it.
- 9:07Speaker 1: That opens the assembler prompt. We literally just delete the
- 9:09Speaker 1: one e and type in ff.
- 9:11Speaker 2: And ff and decimal is two fifty five. We just
- 9:14Speaker 2: changed the starting pool from thirty days to two hundred
- 9:16Speaker 2: and fifty five days.
- 9:17Speaker 1: Well, wait, we have to be super careful here about
- 9:19Speaker 1: opcode alignment, right, Oh.
- 9:20Speaker 2: Critically careful. Now you have to ensure that keep same
- 9:23Speaker 2: size option is checked.
- 9:24Speaker 1: Because machine code is like a Jenga tower, every instruction
- 9:27Speaker 1: takes up a specific number of bytes.
- 9:29Speaker 2: Exactly, if the original instruction is three bytes long, our
- 9:33Speaker 2: patched instruction has to be exactly three bytes.
- 9:35Speaker 1: If we made it five bytes, we'd overwrite the next instruction.
- 9:38Speaker 2: In memory, and the CPU would just try to read
- 9:40Speaker 2: fragments of data as if they were op codes. The
- 9:43Speaker 2: tower collapses and the app crashes instantly.
- 9:46Speaker 1: So by checking keep same size, we safely swap the bricks.
- 9:49Speaker 1: What if our new instruction is actually shorter than.
- 9:51Speaker 2: The old one, The debugger automatically pads the extra space
- 9:55Speaker 2: with ninety bytes which.
- 9:56Speaker 1: Are NOPs right, no operation instructions yep.
- 9:59Speaker 2: The CP you just reads them and does nothing, keeping
- 10:01Speaker 2: the alignment perfect. So we apply the patch. We export
- 10:05Speaker 2: the modifications to a new file, say crackmefour patch dot ex.
- 10:09Speaker 1: We completely close the debugger, severing the analysis environment, and
- 10:14Speaker 1: just run our new X directly in Windows.
- 10:16Speaker 2: And the moment it launches, the CPU executes our patch.
- 10:19Speaker 2: It loads two hundred and fifty five into.
- 10:21Speaker 1: ECS subtracts today's date ten two.
- 10:24Speaker 2: Hundred and fifty five minus ten is two hundred and
- 10:26Speaker 2: forty five.
- 10:26Speaker 1: It's the JG comparison. Two hundred and forty five is
- 10:29Speaker 1: greater than zero true green path, and.
- 10:31Speaker 2: The UI pops up two hundred and forty five days
- 10:34Speaker 2: trial period remaining the enforcement is permanently bypassed.
- 10:37Speaker 1: It is so cool to see how hardware literally executes
- 10:41Speaker 1: software logic like that.
- 10:42Speaker 2: It really is, though, I should mention one alternative technique
- 10:46Speaker 2: before we move on. Sure, we relied heavily on searching
- 10:48Speaker 2: for that plaintech string, but what if the developers encrypted
- 10:52Speaker 2: their strings in the data section, So.
- 10:55Speaker 1: We couldn't just search for trial period expired because it
- 10:57Speaker 1: wouldn't be there, right.
- 10:58Speaker 2: It would be invisible until the exact millisecond it renders.
- 11:02Speaker 2: In that case, we use the call stack method.
- 11:04Speaker 1: How does that work?
- 11:05Speaker 2: You just let the program run normally in the debugger
- 11:08Speaker 2: until that bad message pops up on the UI. The
- 11:10Speaker 2: second it appears, you pause execution.
- 11:13Speaker 1: And then check the call stack window.
- 11:14Speaker 2: Yep, the call stack is a dynamic record. Every time
- 11:18Speaker 2: a function is called, the CPU pushes the return address
- 11:21Speaker 2: onto the stack, so.
- 11:22Speaker 1: It's basically a breadcrumb trail where the execution just was exactly.
- 11:26Speaker 2: You trace it backward, find the address of the function
- 11:29Speaker 2: that called the UI renderer, double click it, and boom,
- 11:32Speaker 2: you're right back in the licensing function.
- 11:34Speaker 1: Wow. So you can still generate the graphical flow chart
- 11:37Speaker 1: without ever needing a string search yep.
- 11:39Speaker 2: It's a very rigorous scientific workflow.
- 11:42Speaker 1: You locate the mechanism via strings or stack tracing, map
- 11:46Speaker 1: the boundaries with X analyzer, visualize the logic with the graph,
- 11:51Speaker 1: and then carefully patch.
- 11:53Speaker 2: The math, breaking it down into those atomic components registers, flags,
- 11:57Speaker 2: binary comparisons. It completely removes the abstraction of software.
- 12:01Speaker 1: Hardware constraints rule everything. If you understand them, you control
- 12:05Speaker 1: the execution definitely well. Before we wrap up this deep dive,
- 12:08Speaker 1: let's do a quick mental exercise for you, the listener,
- 12:11Speaker 1: just to solidify the methodology.
- 12:13Speaker 2: Sounds good.
- 12:14Speaker 1: Imagine you're analyzing a totally new, heavily protected binary. You
- 12:18Speaker 1: run it and it immediately blocks you with a pop
- 12:20Speaker 1: up saying license expired. I mean the strings aren't encrypted.
- 12:24Speaker 1: What is your very first immediate action in the debugger?
- 12:27Speaker 2: You leverage the applications need to communicate. You go to
- 12:30Speaker 2: the string references window and search for the exact phrase
- 12:33Speaker 2: license expired.
- 12:34Speaker 1: That memory pointer acts as your inverse map.
- 12:37Speaker 2: Taking you straight from the UI output back to the
- 12:39Speaker 2: conditional jump. That string is always your front door.
- 12:42Speaker 1: We took this seemingly impenetrable system and proved it's literally
- 12:47Speaker 1: just doing elementary school arithmetic at the processor level.
- 12:50Speaker 2: It's a really powerful concept. And I want to leave
- 12:53Speaker 2: you with a final thought to moleover.
- 12:54Speaker 1: Yeah, go ahead.
- 12:56Speaker 2: Today our whole bypass relied on finding that one E
- 12:59Speaker 2: value you push to a register right out in the open,
- 13:02Speaker 2: just a few instructions above.
- 13:03Speaker 1: The jump very exposed. Right.
- 13:05Speaker 2: So, if you were a software developer and you knew
- 13:08Speaker 2: attackers could just read your control flow graph like a map,
- 13:11Speaker 2: how would you design your code to protect that one
- 13:13Speaker 2: E value?
- 13:14Speaker 1: Oh, that's a great question.
- 13:15Speaker 2: How might you separate the mathematical comparison from the UI
- 13:18Speaker 2: rendering so that searching the string doesn't lead the attacker
- 13:21Speaker 2: straight to your logic.
- 13:23Speaker 1: The dynamic between reverse engineers and developers really is a
- 13:26Speaker 1: perpetual arms race.
- 13:27Speaker 2: It truly is.
- 13:28Speaker 1: As analysis evolves, the defenses get crazier. But for today,
- 13:32Speaker 1: we've proven that digital countdowns are not absolute laws. They
- 13:36Speaker 1: are just instructions waiting to be rewritten. Absolutely, Keep analyzing
- 13:40Speaker 1: the structures around you, keep questioning how things execute at
- 13:43Speaker 1: the hardware level, and we will see you on the
- 13:45Speaker 1: next deep dive.