Latest / CyberCode Academy / Course 20 - Malware Analysis: Identifying and Defeating Code Obfuscation | Episode 1: Defeating Malware Obfuscation: Fundamentals, Impact
Transcript
- 0:00Speaker 1: I want you to just picture a scenario for a second.
- 0:03Speaker 1: Imagine you're a security analyst. It's I don't know, two
- 0:06Speaker 1: in the morning. A critical alert just fires the worst time, always,
- 0:11Speaker 1: something slip past the firewall at say a major financial institution.
- 0:16Speaker 1: You've got the file, and your job is well, simple
- 0:20Speaker 1: on paper, figure out what this thing does and how
- 0:24Speaker 1: to stop it.
- 0:25Speaker 2: Simple on paper, but in reality it's usually a complete nightmare, right.
- 0:29Speaker 1: Because you open this file and you're expecting code, you know, logic, instructions,
- 0:33Speaker 1: a roadmap, but instead you're just staring at chaos. It's
- 0:36Speaker 1: like someone took a dictionary through it in a blender
- 0:39Speaker 1: and just pasted the result on your screen.
- 0:41Speaker 2: And that chaos is entirely by design. What you're looking
- 0:44Speaker 2: at as the result of obfuscation.
- 0:46Speaker 1: Obfuscation. That's our topic for today. It sounds, I don't know,
- 0:49Speaker 1: a bit academic, but really what we're talking about is
- 0:51Speaker 1: digital camouflage.
- 0:51Speaker 2: Isn't it essentially yes, if you strip it down to
- 0:54Speaker 2: the basic definition, to obfuscate is to make something obscure, unclear,
- 0:58Speaker 2: or unintelligible. In our world, in malware analysis, it's the
- 1:02Speaker 2: art of writing code that works perfectly for the computer.
- 1:05Speaker 1: But is an absolute headache for the human trying to
- 1:07Speaker 1: read it exactly. So the mission for this deep dive
- 1:10Speaker 1: is to really understand that headache. We want to look
- 1:13Speaker 1: at how bad actors use this technique to hide their weapons,
- 1:17Speaker 1: and not just.
- 1:18Speaker 2: Hide them, but the psychological tricks.
- 1:20Speaker 1: Too, Yes, the tricks they use to burn people out,
- 1:24Speaker 1: and well, the mindset you need to actually crack that code.
- 1:28Speaker 2: It's a real battle of attrition.
- 1:30Speaker 1: Before we get into the weeds, though, I think we
- 1:32Speaker 1: should clear something up that I hear all the time.
- 1:33Speaker 1: People use obfuscation and encryption almost as if they're the
- 1:38Speaker 1: same thing. They're not, are they.
- 1:40Speaker 2: They're related like cousins, but they are not twins. Encryption
- 1:43Speaker 2: is about locking the data away. If I encryptophile, it's
- 1:47Speaker 2: mathematically unreadable. Without a key, you cannot access the content.
- 1:51Speaker 2: Obfuscation is different. The code isn't locked, it's just really
- 1:54Speaker 2: really messy.
- 1:55Speaker 1: So encryption is a safe that needs a combination. Obfuscation
- 1:58Speaker 1: is more like a note written in invisible inc but
- 2:02Speaker 1: upside down in another language.
- 2:04Speaker 2: That's actually a pretty good way to look at it.
- 2:06Speaker 2: The information is all there, it's accessible, but the effort
- 2:10Speaker 2: you need to put in to interpret it is just massive.
- 2:13Speaker 2: The goal isn't to be unbreakable, it's just to slow
- 2:16Speaker 2: you down.
- 2:17Speaker 1: So it's about the economics of the attack down entirely.
- 2:19Speaker 2: Think about it from the attacker's point of view. They
- 2:22Speaker 2: know that given enough time, a skilled analyst will figure
- 2:25Speaker 2: it out. They can't stop that from happening, but if
- 2:27Speaker 2: they can make it take say three days instead of
- 2:30Speaker 2: three hours, that's a huge win for them. That's three
- 2:34Speaker 2: extra days the malwares on the network, stealing data, spreading,
- 2:38Speaker 2: and it.
- 2:38Speaker 1: Just burns out the analyst. I mean, if I have
- 2:40Speaker 1: to spend ten hours trying to untangle one function, I'm
- 2:43Speaker 1: going to get tired. I'll make mistakes.
- 2:45Speaker 2: Exactly. They are weaponizing your own fatigue against you. They
- 2:49Speaker 2: want to hide the really obvious stuff like strings, which
- 2:52Speaker 2: are just the text bits in the code, or the
- 2:54Speaker 2: command and control addresses that phone home.
- 2:58Speaker 1: Now, we usually frame this as this whole, you know,
- 3:00Speaker 1: bad guys versus good guys thing, But this isn't exclusively
- 3:03Speaker 1: a criminal tactic, right.
- 3:05Speaker 2: No, not at all. It's just a tool. It's neutral.
- 3:08Speaker 2: Legitimate software companies use obfuscation all the time. If you
- 3:12Speaker 2: spend millions developing some proprietary algorithm. You don't want a
- 3:16Speaker 2: competitor to just open up your file and copy your homework, so.
- 3:19Speaker 1: They scramble it to protect their intellectual property.
- 3:22Speaker 2: It works just as well for that, but you know,
- 3:24Speaker 2: regardless of who uses it, there's a fundamental weakness, a
- 3:27Speaker 2: flaw that can never.
- 3:28Speaker 1: Really be fixed, the achilles heel.
- 3:30Speaker 2: The achilles heel, no matter how complex, no matter how
- 3:33Speaker 2: scramble the log it gets, at the end of the day,
- 3:36Speaker 2: the code has to.
- 3:37Speaker 1: Run because the computer doesn't do riddles.
- 3:40Speaker 2: The CPU is painfully literal. It can't execute confusion. It
- 3:44Speaker 2: needs precise ones and zero's instructions. So at the very
- 3:48Speaker 2: moment of execution, that malware has to deoppuscate itself. It
- 3:53Speaker 2: has to show its true form to the processor.
- 3:55Speaker 1: And that's the analyst's window.
- 3:57Speaker 2: That is the catch. The attacker has to balance how
- 3:59Speaker 2: well they have the code against the fact that eventually
- 4:02Speaker 2: they have to show their hand to the machine.
- 4:04Speaker 1: Okay, so let's talk about the battlefield, because catching them
- 4:07Speaker 1: in that moment means you have to know what kind
- 4:09Speaker 1: of code you're even looking at. We've got two main
- 4:12Speaker 1: categories here, native and interpreted code, and.
- 4:15Speaker 2: This distinction is just critical. It dictates your whole approach.
- 4:19Speaker 2: Let's start with native code.
- 4:20Speaker 1: Native meaning it speaks the language of the hardware directly exactly.
- 4:24Speaker 2: This is code written in languages like CCLUS plus a assembly.
- 4:29Speaker 2: When it's finished, the developer compiles it. That process translates
- 4:33Speaker 2: the human readable text into raw machine code that the
- 4:36Speaker 2: CPU understands directly.
- 4:38Speaker 1: So when I, as an analyst open a native file
- 4:42Speaker 1: like in dot ex on Windows, what am I even
- 4:45Speaker 1: looking at?
- 4:46Speaker 2: You're looking at a disassembly. You don't get the original
- 4:48Speaker 2: source code back. You don't see helpful variable names or comments.
- 4:52Speaker 2: The compilation process strips all that humanity away.
- 4:55Speaker 1: It's like trying to read a book after all the
- 4:57Speaker 1: words have been replaced by their dictionary definitions. The meaning
- 5:01Speaker 1: is there somewhere, but the flow is just gone.
- 5:03Speaker 2: And to even read that you need some heavy duty tools.
- 5:08Speaker 2: You're probably using a disassembler. The industry standard has been
- 5:11Speaker 2: iapro for years, but now there's also GUIDRA, which the
- 5:15Speaker 2: NSA released.
- 5:16Speaker 1: What's that like to use a tool like idapro. What
- 5:19Speaker 1: do I see on the screen?
- 5:20Speaker 2: Uh, you're usually looking at what's called a graph view,
- 5:23Speaker 2: So imagine a flow chart. You have these little blocks
- 5:26Speaker 2: of code instructions connected by arrows. A green arrow might
- 5:29Speaker 2: mean if yes, go this way, a red arrow means
- 5:32Speaker 2: if no, go that way. You're navigating this huge map.
- 5:36Speaker 1: Of logic, and I'm guessing a native code obsuscation just
- 5:39Speaker 1: ruins that map.
- 5:40Speaker 2: It turns the map into a bowl of spaghetti. But
- 5:42Speaker 2: let's contrast that with the other battleground, interpreted code.
- 5:45Speaker 1: So things like Java, Python, PowerShell, JavaScript, Right.
- 5:49Speaker 2: These languages don't run directly on the hardware. They run
- 5:51Speaker 2: through an intermediary like a runtime or an interpreter, so
- 5:54Speaker 2: they aren't compiled down to raw machine code in the
- 5:57Speaker 2: same way.
- 5:57Speaker 1: Which means they're easier to reverse generally.
- 6:00Speaker 2: Yeah, with interpreted code, we can use what are called decompilers.
- 6:05Speaker 2: A tool like dn spy can take the file and
- 6:08Speaker 2: basically reconstruct something that looks almost exactly like the original
- 6:11Speaker 2: source code.
- 6:12Speaker 1: So, if native code is like decoding an alien signal,
- 6:15Speaker 1: interpreted code is more like just trying to read someone's
- 6:18Speaker 1: really messy handwriting.
- 6:19Speaker 2: That's a fair comparison. Yeah, you can often see the
- 6:22Speaker 2: original variable names, the structure almost as the author wrote it.
- 6:26Speaker 1: Which means the malware authors have to work a lot
- 6:28Speaker 1: harder to hide their intent. Then if a tool can
- 6:31Speaker 1: just undo their work, they have to add other layers
- 6:35Speaker 1: of confusion, and.
- 6:36Speaker 2: That brings us to their bag of tricks. Since they
- 6:38Speaker 2: know we have these powerful tools, they have to find
- 6:41Speaker 2: ways to break the tools, or well break the analysts
- 6:44Speaker 2: using them.
- 6:45Speaker 1: All right, let's open that bag. What's the first trick?
- 6:47Speaker 1: The first thing that just screams obfuscation to you.
- 6:50Speaker 2: The most immediate sign, especially in interpreted code, is just
- 6:54Speaker 2: nonsensical naming.
- 6:55Speaker 1: Walk me through that.
- 6:56Speaker 2: Okay. So, in a normal program, a developer wants clarity,
- 7:00Speaker 2: they'll name a variable bank balance, or something obvious. It
- 7:03Speaker 2: helps with debugging. A malware author wants the exact opposite,
- 7:07Speaker 2: so they'll run a tool that renames every single variable
- 7:10Speaker 2: and function to random junk.
- 7:13Speaker 1: So bank balance becomes by seventy nine Q two.
- 7:15Speaker 2: Z's exactly, and the function that sends the money overseas
- 7:19Speaker 2: becomes function B one. Now imagine trying to follow a
- 7:23Speaker 2: script where every key term has been replaced by a
- 7:25Speaker 2: license plate number. If by seventy nine is greater than
- 7:28Speaker 2: B two, then run z ninety nine. You just can't
- 7:31Speaker 2: hold the story in your head.
- 7:32Speaker 1: You have to manually trace everything just to figure out
- 7:35Speaker 1: what's what.
- 7:35Speaker 2: It strips all the context away, and once they've done that,
- 7:38Speaker 2: they'll start attacking your patients with something called dead code.
- 7:41Speaker 1: Dead code is this the busy work tactic.
- 7:43Speaker 2: It's the art of wasting time. The attacker inserts blocks
- 7:46Speaker 2: of perfectly valid code that do absolutely nothing relevant to
- 7:48Speaker 2: the actual attack.
- 7:49Speaker 1: Give me an example of what that looks like.
- 7:51Speaker 2: So you're tracing the code, you hit a function that
- 7:54Speaker 2: looks important. It's taking a couple variables, doing some complex math,
- 7:58Speaker 2: maybe running a loose five thousand times. You think, ah,
- 8:02Speaker 2: this must be it. This is the decryption routine.
- 8:04Speaker 1: So you spend two hours mapping it all out.
- 8:07Speaker 2: You map it out, you calculate the values, you stare
- 8:09Speaker 2: at it until your eyes are burning, and then you
- 8:12Speaker 2: get to the end of the function and realize the
- 8:16Speaker 2: result just gets thrown away. It's never used.
- 8:19Speaker 1: It was a road to know.
- 8:20Speaker 2: It a total decoy. It's a psychological trap designed to
- 8:23Speaker 2: make you waste your morning on nothing.
- 8:25Speaker 1: That's incredibly frustrating. It's a puzzle where half the pieces
- 8:29Speaker 1: are from a different box.
- 8:31Speaker 2: And while you're busy with that, you might miss the
- 8:33Speaker 2: controlled flow, flattening the fake roads control flow.
- 8:36Speaker 1: That's the map we talked about, the.
- 8:37Speaker 2: Arrows yes, and malware authors love to corrupt that map.
- 8:41Speaker 2: They'll insert something called an opaque predicate. It's a fancy
- 8:45Speaker 2: term for an if statement where the outcome is already known,
- 8:48Speaker 2: but the tool doesn't know that.
- 8:50Speaker 1: So the code says, if X is true, go left.
- 8:52Speaker 1: If not, go right.
- 8:54Speaker 2: But X is built in a way that it is
- 8:56Speaker 2: always true. The code will never go right. But your
- 8:59Speaker 2: analysis tool sees both paths as possible, so it draws
- 9:03Speaker 2: both arrows. You do this hundreds of times, and that
- 9:07Speaker 2: nice clean flow chart just becomes an unreadable mess of
- 9:10Speaker 2: crossing lines.
- 9:11Speaker 1: There's cluttering the maps, you can't see the.
- 9:12Speaker 2: Highway precisely, visual noise. And then there's the data itself,
- 9:18Speaker 2: the secrets. If I'm writing malware, I need to tell
- 9:20Speaker 2: it where to phone home. I need a URL or
- 9:23Speaker 2: an IP address.
- 9:24Speaker 1: Strings.
- 9:24Speaker 2: Strings They're usually the first thing an analyst looks for.
- 9:28Speaker 2: You run a simple strings command and hope to see
- 9:30Speaker 2: something obvious. The authors know this, so they use string manipulation.
- 9:34Speaker 1: Hell, can you high text that eventually has to be read?
- 9:37Speaker 2: You break it into pieces. A common trick is stack
- 9:40Speaker 2: strings instead of writing a word download. The code might
- 9:43Speaker 2: say take the letter D, now, put an O next
- 9:45Speaker 2: to it. Now w. It builds the word character by
- 9:48Speaker 2: character right when it needs it.
- 9:50Speaker 1: So if you search the file for the word download,
- 9:53Speaker 1: you find nothing.
- 9:54Speaker 2: Nothing because the word doesn't actually exist until the program
- 9:58Speaker 2: is running. Or they'll just encrypt all the strings, storing
- 10:01Speaker 2: them as jibberish and only decrypting them for the split
- 10:03Speaker 2: second they're needed.
- 10:04Speaker 1: It really is a game of hiding in plain sight.
- 10:06Speaker 1: But you said earlier it takes a specific mindset to
- 10:10Speaker 1: beat this. It's not just about knowing the tricks right.
- 10:12Speaker 1: It's a whole workflow it is.
- 10:14Speaker 2: You can't just dive in. You need a structured approach.
- 10:17Speaker 2: We usually break it down into three phases, identify, defeat,
- 10:21Speaker 2: and analyze.
- 10:22Speaker 1: And identify is what we've been talking about. Spotting the
- 10:26Speaker 1: gibberish names, the weird loops.
- 10:28Speaker 2: Right, recognizing that you're being lied to. Once you see it,
- 10:31Speaker 2: you move to the defeat phase. And this is where
- 10:34Speaker 2: you have to make a choice between static and dynamic analysis, the.
- 10:38Speaker 1: Choice between studying the bomb and carefully detonating the bomb.
- 10:43Speaker 2: That's a vivid way to put it, but yes, static
- 10:46Speaker 2: analysis is looking at the code without running it. It's safer.
- 10:49Speaker 2: But as we've seen, it can be incredibly hard.
- 10:51Speaker 1: So when do you switch to dynamic analysis.
- 10:54Speaker 2: When the obfuscation is just too thick or you're on
- 10:56Speaker 2: the clock. Dynamic analysis means running the malware sounds insane.
- 11:00Speaker 1: Let me just run this virus on my computer.
- 11:02Speaker 2: Well, you never do it on your own machine. You
- 11:04Speaker 2: use a sandbox or a virtual machine, a contained environment
- 11:08Speaker 2: where the malware thinks it's on a real PC, but
- 11:11Speaker 2: it's actually in a cage.
- 11:14Speaker 1: And the benefit there goes right back to the Achilles
- 11:16Speaker 1: Heel exactly.
- 11:18Speaker 2: Since the code must deobcuscate itself to run, we just
- 11:22Speaker 2: let it do the hard work for us. We run
- 11:24Speaker 2: the malware in the sandbox, we let it unpack its
- 11:26Speaker 2: payload decrypted strings, and then at that exact moment, we
- 11:30Speaker 2: freeze time. You take a snapshot of the memory, we
- 11:33Speaker 2: dump the process memory, and suddenly we're not looking at
- 11:36Speaker 2: the scrambled version anymore. We have the clean, unpacked code
- 11:40Speaker 2: sitting right there in ram.
- 11:41Speaker 1: So you let the malware author defeat themselves. They had
- 11:45Speaker 1: to build the key to unlock their own code, and
- 11:47Speaker 1: you just waited for them to use it.
- 11:49Speaker 2: It's the most satisfying moment in analysis. You can bypass
- 11:52Speaker 2: days of static work just by letting the program run
- 11:55Speaker 2: for three seconds. But you do have to be careful.
- 11:57Speaker 2: Some malware is VM aware. If it's in a cage
- 12:00Speaker 2: before it unlocked, it's.
- 12:01Speaker 1: A constant arms race. They hide, we find, we build better.
- 12:05Speaker 2: Tools, They build better camouflage. It's endless And for anyone
- 12:09Speaker 2: listening who wants to try this, please please set up
- 12:12Speaker 2: a lab. Don't download samples to your laptop. Get a
- 12:15Speaker 2: Linux VM. Look at something like Remnicks. It comes preloaded
- 12:18Speaker 2: with all these tools.
- 12:19Speaker 1: Curious but safe, that's the motto always. Okay, before we
- 12:23Speaker 1: wrap up, I want to quiz our listeners a bit.
- 12:25Speaker 1: We've gone over a lot. Let's do a quick mental
- 12:27Speaker 1: exercise to lock it in.
- 12:29Speaker 2: Okay, here's the scenario. You're looking at a script. You
- 12:32Speaker 2: see a variable name A one B two. This variable
- 12:35Speaker 2: goes into a loop that runs five thousand times. Inside
- 12:39Speaker 2: the loop, all it's doing is simple addition over and over.
- 12:42Speaker 2: But and this is the key, the result is never
- 12:46Speaker 2: used anywhere else.
- 12:47Speaker 1: So we have a nonsense name running a math marathon
- 12:50Speaker 1: and the result gets thrown in the trash.
- 12:52Speaker 2: The question for you is what techniques are we seeing
- 12:54Speaker 2: and why did the author do that?
- 12:56Speaker 1: Okay, based on what we've said, that nonsense name A
- 12:58Speaker 1: one B two that's the nonsense naming to remove the
- 13:01Speaker 1: context direct and the loop. That's got to be dead code.
- 13:04Speaker 1: It's the busy work spot on.
- 13:06Speaker 2: And the why is twofold. One yes, to waste your
- 13:10Speaker 2: time as a human analyst, but two, it's also to
- 13:13Speaker 2: trick automated sandboxes. Some of those tools only run a
- 13:17Speaker 2: program for a few seconds. If the malware spends those
- 13:20Speaker 2: first few seconds just doing pointless math, the sandbox might
- 13:24Speaker 2: time out and mark the file as safe.
- 13:27Speaker 1: That is devious. It's just stulling the clock.
- 13:30Speaker 2: It's very effective, and recognizing that is the first step
- 13:33Speaker 2: to bypassing it.
- 13:34Speaker 1: This has been a huge download of information. I think
- 13:36Speaker 1: the biggest takeaway for me is that obfuscation is really
- 13:39Speaker 1: just a rapper. It feels so intimidating, all that complexity,
- 13:43Speaker 1: but it's just the packaging.
- 13:44Speaker 2: It is just a rapper. It's designed to stop you
- 13:46Speaker 2: from seeing the truth immediately. But if you have patience
- 13:49Speaker 2: and you trust the fact that code wants to run,
- 13:51Speaker 2: you can always peel back those layers.
- 13:53Speaker 1: I want to leave everyone with a final sort of
- 13:55Speaker 1: provocative thought. We've been talking about humans writing this code
- 13:58Speaker 1: and humans analyzing it, but we're entering a new era now,
- 14:01Speaker 1: the age of AI. Exactly as AI starts writing code
- 14:05Speaker 1: and inevitably starts writing malware, you have to wonder will
- 14:09Speaker 1: obcuscation become so complex, so mathematically perfect, that a human
- 14:14Speaker 1: just can't analyze it manually anymore.
- 14:16Speaker 2: That's the big question in our field right now. We
- 14:18Speaker 2: could be heading for a future where we have to
- 14:20Speaker 2: rely entirely on AI defenders to catch AI attackers. The
- 14:25Speaker 2: cat and mouse game becomes robot cat versus robot mouse, and.
- 14:28Speaker 1: We're just watching the blinking lights hoping our robot wins.
- 14:32Speaker 2: Or you know, we double down on the fundamentals, because
- 14:35Speaker 2: no matter who or what rates the code, the physics
- 14:37Speaker 2: of the CPU, they don't change.
- 14:40Speaker 1: The Achilles heel remains.
- 14:41Speaker 2: It always does.
- 14:42Speaker 1: A comforting thought to end on, Thanks for guiding us
- 14:44Speaker 1: through that maze today, my pleasure, and to you listening.
- 14:48Speaker 1: Keep peeling back those layers, stay curious, stay safe, and
- 14:51Speaker 1: we'll catch you on the next deep dive.