Latest / CyberCode Academy / Course 20 - Malware Analysis: Identifying and Defeating Code Obfuscation | Episode 3: Analyzing and Defeating String Obfuscation in Native
Transcript
- 0:00Speaker 1: Imagine for a moment that you walk into a vast
- 0:03Speaker 1: old library. You know, it smells like dust and paper.
- 0:07Speaker 1: You pull a heavy, leather bound book off the shelf.
- 0:10Speaker 1: Maybe it's a thriller you've been dying to read. You
- 0:13Speaker 1: feel the weight of it in your hands. You open
- 0:15Speaker 1: the cover, expecting to see words, sentences, a plot unfolding. Right,
- 0:20Speaker 1: but the pages are plank, completely white.
- 0:24Speaker 2: Wow. Okay, that is an incredibly unsettling image.
- 0:27Speaker 1: It is, right, but here's the catch. The book weighs
- 0:30Speaker 1: the same as a normal book. It has the same
- 0:32Speaker 1: number of pages. You know, there is a story inside, instructions, characters,
- 0:37Speaker 1: a plot, but the text is literally invisible.
- 0:40Speaker 2: That is a terrifyingly accurate metaphor for what security analysts
- 0:44Speaker 2: face every single day. Really, yeah, you know there is
- 0:46Speaker 2: malicious intent inside a file. You know it's designed to
- 0:49Speaker 2: do damage, but when you look at it with standard tools,
- 0:51Speaker 2: it looks like nothing, just empty space.
- 0:55Speaker 1: And that is exactly what we are digging into today.
- 0:57Speaker 1: We are looking at the invisible script. We're going to
- 0:59Speaker 1: talk about how militia programmers hide their code, specifically the
- 1:02Speaker 1: tech strings to evade detection.
- 1:04Speaker 2: And crucially, we're going to look at the detective tools
- 1:08Speaker 2: and the mindset analysts use to make that invisible.
- 1:11Speaker 1: Ink a here, it sounds like a game of cat
- 1:13Speaker 1: and mouse.
- 1:13Speaker 2: Oh, it really is spy versus spy. The material we're
- 1:17Speaker 2: breaking down today. It covers everything from the basic differences
- 1:21Speaker 2: in how code is built. These some truly devious techniques.
- 1:26Speaker 2: We're talking stack strings, precomputed checksums, and.
- 1:29Speaker 1: How to catch them in the act exactly. I love
- 1:32Speaker 1: that spycraft angle. But before we get into the heavy stuff,
- 1:35Speaker 1: we probably need to set the stage. The research makes
- 1:37Speaker 1: a big distinction between interpreted code and native code. I
- 1:42Speaker 1: feel like this is the foundation of the whole thing, right.
- 1:44Speaker 2: It absolutely is. I mean, if you don't get this distinction,
- 1:46Speaker 2: the rest of the magic tricks they just won't make sense.
- 1:49Speaker 2: Think of it like cooking interpreted code languages people might know,
- 1:52Speaker 2: like Python or JavaScript, is like cooking while reading a
- 1:57Speaker 2: recipe card.
- 1:58Speaker 1: Okay, so the recipe is the code.
- 1:59Speaker 2: Right, you have the source code the recipe sitting right there.
- 2:02Speaker 2: The computer reads a line, executes it, reads the next line,
- 2:05Speaker 2: executes it. Yeah, if you're an analyst, you can usually
- 2:07Speaker 2: just walk into the kitchen, pick up the recipe and
- 2:10Speaker 2: read it.
- 2:10Speaker 1: You can see if they're adding poison to the soup.
- 2:13Speaker 2: Because the instructions are plain text. It says download virus
- 2:17Speaker 2: or delete files. It's visible.
- 2:19Speaker 1: That seems pretty easy to catch. But native code is.
- 2:22Speaker 2: Different, completely different, native code like C or C plus plus.
- 2:25Speaker 2: But that's more like a pre cooked frozen meal. Okay,
- 2:28Speaker 2: the chef rote a recipe, sure, but then they put
- 2:31Speaker 2: it through a process called compiling. The compiler translates that
- 2:35Speaker 2: human readable code into machine code, into binary data that
- 2:39Speaker 2: the CPU executes directly. It gets packaged up, usually in
- 2:43Speaker 2: what's called a pe file format on Windows.
- 2:46Speaker 1: So you can't just read the recipe anymore. It's already
- 2:48Speaker 1: been cooked into this binary brick exactly.
- 2:51Speaker 2: It's just a block of ones and zeros for the processor.
- 2:53Speaker 1: Yeah.
- 2:54Speaker 2: But and here's the key. Even in a frozen meal,
- 2:57Speaker 2: you can usually identify the ingredients if you look closely.
- 3:01Speaker 2: In a normal native program, if it wants to say
- 3:03Speaker 2: Hello world or connect to the internet, those words, those
- 3:07Speaker 2: strings of text, they usually sit in a specific designated
- 3:11Speaker 2: section of the binary file.
- 3:13Speaker 1: This is usually called the dot data section right, or
- 3:15Speaker 1: dot r data.
- 3:16Speaker 2: YEP or dot our data for read only data. The
- 3:18Speaker 2: point is the text is stored as text. So if
- 3:22Speaker 2: you use a basic tool like the standard strings utility,
- 3:26Speaker 2: it just scans the file and says, hey, I found
- 3:28Speaker 2: the word password or attack sitting right here.
- 3:31Speaker 1: It's like reading the ingredient's label on the back of
- 3:33Speaker 1: the box contains tnuts sugar malicious payload precisely.
- 3:38Speaker 2: And that's the problem for malware authors. They know the
- 3:40Speaker 2: very first thing a security analyst does is run that
- 3:43Speaker 2: strings tool. It's the low hanging fruit totally. If your
- 3:46Speaker 2: malware says download ransomware in plain text inside the file,
- 3:50Speaker 2: you're caught in five seconds. You don't even have to
- 3:52Speaker 2: run the program to know what's bad.
- 3:53Speaker 1: So if I'm a malware author, I have a problem.
- 3:56Speaker 1: I need to tell the computer to do things like
- 3:58Speaker 1: visit a website url which wire's text, but I can't
- 4:01Speaker 1: write that text down.
- 4:03Speaker 2: That's the paradox. How do you use a word without
- 4:05Speaker 2: writing it down?
- 4:06Speaker 1: Which brings us to our first major magic trick, the
- 4:10Speaker 1: stack string. This sounds kind of architectural. What's happening here.
- 4:15Speaker 2: It's a very clever way of defeating that static scan
- 4:17Speaker 2: we just talked about. Okay, let's stick with a crime analogy.
- 4:21Speaker 2: Imagine you want to send a ransom note. But you
- 4:23Speaker 2: know the police are scanning every letter for the phrase
- 4:26Speaker 2: I have your cat.
- 4:27Speaker 1: Right, classic villain stuff.
- 4:29Speaker 2: If you write I have your cat on a piece
- 4:31Speaker 2: of paper and mail it, the scanner catch of it
- 4:33Speaker 2: because the letters are next to each other. But what
- 4:36Speaker 2: if you cut letters out of a magazine one by one.
- 4:39Speaker 2: She's an I put it in your pocket, then an H,
- 4:42Speaker 2: then an A.
- 4:43Speaker 1: So the phrase never exists as a whole unit.
- 4:46Speaker 2: Not until the very last second when you glue them down.
- 4:49Speaker 2: In the code, this relies on the MOV instruction short
- 4:52Speaker 2: for move. The program tells the CPU move the number
- 4:55Speaker 2: for the letter H into this spot and memory. Now
- 4:58Speaker 2: move the number for E into the next pot.
- 5:00Speaker 1: So it's building the string directly on the stack, this
- 5:02Speaker 1: temporary memory space.
- 5:04Speaker 2: Bye bye bye.
- 5:05Speaker 1: That is sneaky. So if I run that strings tool
- 5:07Speaker 1: on the file beforehand, I don't find download virus. I
- 5:11Speaker 1: just find well, a bunch of instructions.
- 5:14Speaker 2: You just find move, move Move. The strings tool is
- 5:17Speaker 2: looking for a sentence. It's not looking for the instructions
- 5:19Speaker 2: to build a sentence. To a human. It just looks
- 5:22Speaker 2: incredibly tedious.
- 5:23Speaker 1: It breaks the pattern recognition.
- 5:25Speaker 2: Completely and This is really common in something called shell code.
- 5:28Speaker 1: I see that term a lot. Shell code is the
- 5:30Speaker 1: code that often doesn't even have a proper file format.
- 5:33Speaker 1: It just floats in memory.
- 5:34Speaker 2: That's the one shell code is raw independent instructions. It
- 5:38Speaker 2: doesn't have the luxury of a nice data section to
- 5:41Speaker 2: store its text, so it has to build its strings
- 5:43Speaker 2: on the fly.
- 5:44Speaker 1: And the sources pointed out a specific string that shows
- 5:46Speaker 1: up a lot in show code LDR load dl.
- 5:49Speaker 2: Yeah, that's a command used to load libraries. If the
- 5:51Speaker 2: shell code wants to do anything like talk to the network,
- 5:54Speaker 2: it needs to load the Windows libraries first. But here's
- 5:56Speaker 2: a little detail that trips people up. What's that in memory?
- 5:59Speaker 2: Because because of how Intel computers store numbers, it's called
- 6:02Speaker 2: little Indian format. Yeah, you often see that string backward.
- 6:06Speaker 1: Backward, like reading it in a mirror.
- 6:08Speaker 2: Yeah, pretty much, instead of reading left to right. The
- 6:11Speaker 2: bytes are stored least significant first, So you might be
- 6:14Speaker 2: reading led dale RDL in your memory dump.
- 6:18Speaker 1: So not only is it built letter by letter, but
- 6:20Speaker 1: once it's built, it might be written backward just because
- 6:23Speaker 1: of the architecture exactly.
- 6:24Speaker 2: It's not malice, just mechanics. Yeah, but it adds to
- 6:27Speaker 2: the confusion if you aren't expecting it.
- 6:28Speaker 1: Okay, so stack strings are a pain, but for every
- 6:32Speaker 1: lock there's a key. The sources mentioned a specific tool
- 6:35Speaker 1: designed to catch.
- 6:36Speaker 2: This flass floss, the fire Eye Labs obviouscated string.
- 6:40Speaker 1: Solver, best acronym in the industry. It sounds like dental
- 6:43Speaker 1: hygiene for your code.
- 6:45Speaker 2: It basically is. It cleans up the gunk flaws. Is
- 6:47Speaker 2: designed to be smarter than your average string scanner house.
- 6:50Speaker 2: So the standard strings tool is static. It just looks
- 6:53Speaker 2: at the file on the disc. Floss actually emulates the.
- 6:55Speaker 1: Code emulate so it runs it sort of.
- 6:59Speaker 2: It mimics the CP you in a safe, simulated way.
- 7:02Speaker 2: It goes through the code and says, okay, if I
- 7:04Speaker 2: were the CPU, I would move h here, then e here.
- 7:08Speaker 2: It watches the malware glue the ransom note together in
- 7:11Speaker 2: virtual memory, snaps a picture of the finished text and
- 7:14Speaker 2: hands it to you.
- 7:15Speaker 1: That's fantastic, So it bates the malware into revealing itself
- 7:19Speaker 1: without any risk.
- 7:20Speaker 2: Right. It's a huge time saver. I mean, without it,
- 7:23Speaker 2: an analyst has to manually reverse engineer those stack strings,
- 7:27Speaker 2: which takes forever.
- 7:28Speaker 1: So stack strings are level one. But the research points
- 7:31Speaker 1: to something much darker, the ghost method.
- 7:35Speaker 2: Yeah, stack strings are just camouflage. If we want to
- 7:38Speaker 2: talk about true invisibility, we have to talk about pre
- 7:40Speaker 2: computed checksums.
- 7:41Speaker 1: This part really blew my mind. We're moving from hiding
- 7:44Speaker 1: the text to not using text at all. Right, but
- 7:47Speaker 1: walk me through that. How do you tell the computer
- 7:49Speaker 1: to open Internet explore? If you don't use those words,
- 7:51Speaker 1: you can't just point and grunt at the processor.
- 7:54Speaker 2: You use math. This technique is formally called dynamic input
- 7:58Speaker 2: resolution using hashing. It's elegant in a really terrifying way.
- 8:03Speaker 1: Okay, break it down for us.
- 8:04Speaker 2: So malware needs to find a specific system tool. Let's
- 8:07Speaker 2: say it needs a library called wine at dot dll
- 8:10Speaker 2: to connect to the Internet. A normal program would ask
- 8:13Speaker 2: for it by name, Please give me wy at dot dll,
- 8:16Speaker 2: But that leaves a paper trail that string it searchable,
- 8:18Speaker 2: got it? So instead, the malware author takes the name
- 8:21Speaker 2: wy at dot dl beforehand on their own computer and
- 8:24Speaker 2: runs it through a specific mathematical formula hashing algorithm. Let's
- 8:27Speaker 2: just say that formula turns the letters wine at dot
- 8:30Speaker 2: dl into the number five five sixty six.
- 8:33Speaker 1: Okay, so the malware just remembers the number five five
- 8:36Speaker 1: sixty six.
- 8:37Speaker 2: Yes, the malware carries at number five five sixty six
- 8:39Speaker 2: hard coded inside it. It deletes the name. It just
- 8:42Speaker 2: knows it needs number five five sixty six.
- 8:44Speaker 1: So what happens when it runs on my computer?
- 8:46Speaker 2: It starts picking up every tool that finds in your memory,
- 8:49Speaker 2: one by one. It picks up tool A, runs the
- 8:52Speaker 2: math on its name. Does it equal five to five
- 8:54Speaker 2: to sixty six? No, okay, move on, pick up tool
- 8:56Speaker 2: B runs the math and no. It picks up wine
- 8:58Speaker 2: at dot DL runs the math and bingo, iticals five
- 9:02Speaker 2: past sixty six.
- 9:03Speaker 1: Wow, it's like a secret handshake. The malaware doesn't know
- 9:06Speaker 1: the person's name, it just knows that if they perform
- 9:08Speaker 1: the secret handshake quickly.
- 9:09Speaker 2: That's the one that is the perfect analogy. And if
- 9:12Speaker 2: you look at the code, you see absolutely nothing readable.
- 9:15Speaker 2: You just see a loop doing math. To a static scanner,
- 9:18Speaker 2: it looks like random noise.
- 9:20Speaker 1: The source material actually walks through a specific algorithm. It
- 9:23Speaker 1: seemed really specific. Why not just you know, add the
- 9:26Speaker 1: values of the letters up.
- 9:28Speaker 2: You'd get too many collisions, too many different names would
- 9:30Speaker 2: add up to the same number. So the algorithm does
- 9:33Speaker 2: a few things. First, it handles capitalization right.
- 9:35Speaker 1: Because DLL and DEAL are the same to Windows, but
- 9:38Speaker 1: different numbers.
- 9:39Speaker 2: Exactly, so they often use an OAR operation with zero
- 9:42Speaker 2: by twenty to force everything to lower case. Then it
- 9:46Speaker 2: rotates the bits of the value.
- 9:48Speaker 1: Rotate the bits like moving a dial.
- 9:50Speaker 2: Kind of imagine a row of soldiers the bits. You
- 9:53Speaker 2: take the last soldier and move them to the front.
- 9:55Speaker 2: It scrambles the number in a predictable way. Finally, they
- 9:58Speaker 2: often XO R the result with another number.
- 10:01Speaker 1: It sounds like a blender.
- 10:02Speaker 2: It is a blender, but it's a mathematical blender where
- 10:05Speaker 2: if you put the same fruit in you always get
- 10:07Speaker 2: the exact same smoothie.
- 10:08Speaker 1: So, as an analyst looking at this file, I'm blind.
- 10:12Speaker 1: How do I figure out what it's doing? Do I
- 10:13Speaker 1: just give up?
- 10:14Speaker 2: Never, if we can't read the script, we have to
- 10:17Speaker 2: watch the movie. We shift from static analysis to dynamic analysis.
- 10:22Speaker 1: This is where we put on the lab coat and safety.
- 10:24Speaker 2: Goggles or the bomb discosal suit. We use a tool
- 10:27Speaker 2: called a debugger like Windy Bay or by thirty two DBA.
- 10:30Speaker 2: We load the malware into a safe, isolated environment. We're
- 10:34Speaker 2: not going to double click it and let it run wild?
- 10:36Speaker 2: Who want to watch it? Step by step?
- 10:37Speaker 1: So what are we looking at inside a debugger?
- 10:40Speaker 2: Imagine a control panel with different quadrants. One shows the
- 10:43Speaker 2: raw code, one shows the assembly instructions, another shows memory,
- 10:47Speaker 2: and a really important one shows the registers the CPU's
- 10:51Speaker 2: current state. It kind of looks like the matrix.
- 10:53Speaker 1: And the sources mention a specific tripwire command I INT three.
- 10:58Speaker 1: What is that?
- 10:58Speaker 2: It? Three is a soft for a breakpoint. It's an
- 11:01Speaker 2: instruction you can insert that tells the CPU stop freeze
- 11:05Speaker 2: right here, like.
- 11:06Speaker 1: Hitting the pause button on a movie.
- 11:07Speaker 2: Exactly. Yeah, So let's go back to that ghost method.
- 11:10Speaker 2: We see the code doing all this weird math. We
- 11:12Speaker 2: don't know what it's looking for, but we know that eventually,
- 11:15Speaker 2: once the math matches, the malware is going to store
- 11:17Speaker 2: the address of that function in a register.
- 11:20Speaker 1: Okay, a register like EAX or ebx. You said, that's
- 11:23Speaker 1: like the hands of the CPO.
- 11:24Speaker 2: Exactly, that's what it's holding right now. So the analyst
- 11:27Speaker 2: sets a break point. Right after the math loop finishes,
- 11:30Speaker 2: they run the program. It freezes. Then they look at
- 11:33Speaker 2: the CPU's hands. They look at the EAX.
- 11:35Speaker 1: Register, and that's the uhuh moment.
- 11:37Speaker 2: That's the magic trick revealed. Suddenly, that meaningless hash has
- 11:41Speaker 2: been resolved into a real memory address. The debugger will
- 11:44Speaker 2: translate that address and tell you, hey, EAX is now
- 11:47Speaker 2: pointing to kernel thirty two dot DLL.
- 11:49Speaker 1: The ghost takes off its mask.
- 11:51Speaker 2: It has to to use the function, It has to
- 11:53Speaker 2: find the address. You just wait for the malware to
- 11:55Speaker 2: solve it for you. Then you peek over its shoulder.
- 11:58Speaker 1: That is incredibly satisfying.
- 11:59Speaker 2: It is, But there is one little trap mentioned in
- 12:02Speaker 2: the sources that I think is worth noting. It trips
- 12:04Speaker 2: up beginners constantly. The difference between ASCI and Unicode strengths.
- 12:08Speaker 1: Oh right, the null byte issue. Why does this matter
- 12:12Speaker 1: so much?
- 12:12Speaker 2: So? In the old days we used ASKI one byte
- 12:15Speaker 2: equals one letter, simple, But to support all the world's
- 12:19Speaker 2: languages we move to Unicode in Windows. That often means
- 12:22Speaker 2: using two bytes for every letter.
- 12:24Speaker 1: Okay, so a gets bigger.
- 12:26Speaker 2: In ASCI, A is just forty one. In Unicode, a
- 12:30Speaker 2: looks like forty one zero. That zero zero is a
- 12:33Speaker 2: null byte is zero? It's padding.
- 12:35Speaker 1: And why does the zero matter?
- 12:37Speaker 2: Because most old school tools assume a string ends when
- 12:40Speaker 2: it hits a zero. It's the period of the end
- 12:42Speaker 2: of the sentence. If you try to view a Unicode
- 12:45Speaker 2: string with an a sc tool like the day command
- 12:47Speaker 2: in a debugger, it sees the first letter, then sees
- 12:50Speaker 2: the zero zero and thinks oh, end of string.
- 12:52Speaker 1: So if the password is attack, you only see.
- 12:55Speaker 2: A exactly you see A and think it's just garbage data.
- 12:58Speaker 2: You might miss the entire halo just because you're using
- 13:01Speaker 2: the wrong command. You have to switch to Unicode mode
- 13:03Speaker 2: the do command to see the full text.
- 13:06Speaker 1: It's those tiny details that make all the difference.
- 13:08Speaker 2: It really is. Reverse engineering is ninety percent knowing where
- 13:11Speaker 2: to look and ten percent knowing how to read what
- 13:13Speaker 2: you find.
- 13:13Speaker 1: Okay, so let's do a quick review before we give
- 13:15Speaker 1: our listeners a challenge. We learned that malware authors are
- 13:18Speaker 1: constantly trying to hide their intent right.
- 13:21Speaker 2: And we saw three main levels of hiding. First, stack strings,
- 13:25Speaker 2: building a sentence character by character to fool static scanners.
- 13:29Speaker 1: The ransom note method.
- 13:30Speaker 2: Then there's encryption, which we only touched on, and third,
- 13:33Speaker 2: the most advanced one. We discussed checksums, removing text entirely
- 13:39Speaker 2: and using math to find functions.
- 13:41Speaker 1: And the way to beat them is either with smarter
- 13:43Speaker 1: tools like flock laws, or by getting your hands dirty
- 13:47Speaker 1: with dynamic analysis and a debugger.
- 13:49Speaker 2: Exactly the malware can run, but it can't hide if
- 13:52Speaker 2: you know where to look.
- 13:53Speaker 1: Okay, here is the challenge for you listening at home.
- 13:56Speaker 1: Visualize this. You're analyzing a suspicious file. You run the
- 14:00Speaker 1: basic strings tool and you find nothing.
- 14:03Speaker 2: Absolute silence, the worst feeling.
- 14:05Speaker 1: Right, no imports, nothing, but the file size is large.
- 14:08Speaker 1: It definitely has code in it. Based on what we
- 14:10Speaker 1: talked about today, what technique is likely being used, and
- 14:12Speaker 1: what is your very next step.
- 14:15Speaker 2: That's a classic scenario. If you're seeing nothing, not even
- 14:18Speaker 2: the basic imports that every program needs, it's highly likely
- 14:21Speaker 2: using free computed checksums, it's finding its own tools using math.
- 14:25Speaker 2: And the next step you can't just read it, you
- 14:27Speaker 2: need to open it in a disassembler or a debugger.
- 14:30Speaker 2: You'd be looking for what we call indirect calls. Instead
- 14:32Speaker 2: of the code saying call function X, it says call
- 14:34Speaker 2: whatever's in register.
- 14:35Speaker 1: EAX, and you trace backer.
- 14:38Speaker 2: You trace backward to see how eax got filled. That's
- 14:41Speaker 2: where you'll find the math loop.
- 14:42Speaker 1: Excellent. It's like finding a secret passage by looking for
- 14:45Speaker 1: the worn out carpet leading to a blank wall.
- 14:47Speaker 2: That's a very poetic way to describe reverse engineering.
- 14:50Speaker 1: I try, you know, looking at all of this, it
- 14:52Speaker 1: really feels like an arms race.
- 14:53Speaker 2: It is. As soon as a new hiding technique is found,
- 14:57Speaker 2: analysts build tools like floss to find them. Then the
- 15:01Speaker 2: malware authors invent a new obfuscation. The invisible script is
- 15:06Speaker 2: only invisible until you know.
- 15:07Speaker 1: How to look, and that's the takeaway. The code can run,
- 15:10Speaker 1: but it can't hide forever.
- 15:12Speaker 2: Absolutely, the truth is always in the execution.
- 15:15Speaker 1: Well said. Thanks for joining us on this deep dive
- 15:18Speaker 1: into the invisible world of obfuscated code. To everyone listening,
- 15:21Speaker 1: stay curious and look beneath the surface.
- 15:24Speaker 2: Until next time, keep digging.