Latest / CyberCode Academy / Course 24 - Machine Learning for Red Team Hackers | Episode 3: Evading Machine Learning Malware Classifiers
Transcript
- 0:00Speaker 1: Okay, let's start today with a mental image. I want
- 0:03Speaker 1: you to picture a high stakes security scenario. You're running
- 0:07Speaker 1: an ultra exclusive event, think a diplomatic summit, may be
- 0:12Speaker 1: a huge tech launch. You've hired the absolute best security
- 0:15Speaker 1: guard in the business. This guy's a legend.
- 0:17Speaker 2: Okay.
- 0:18Speaker 1: He's memorized the face of every known criminal. He knows
- 0:21Speaker 1: their walk, the tools they carry.
- 0:22Speaker 2: Everything sounds like a pretty tight ship.
- 0:24Speaker 1: It is. So if a known burglar walks up to
- 0:27Speaker 1: the door, the guard spots him instantly tackle handcuffs. Game over.
- 0:31Speaker 2: Makes sense.
- 0:31Speaker 1: But now imagine that same burglar walks up, same face,
- 0:35Speaker 1: same crowbar in his back pocket, but this time he's
- 0:39Speaker 1: holding a gigantic, overflowing bouquet of daisies. Daisies, right, yellow daisies,
- 0:44Speaker 1: and your legendary guard looks at him, sees the flowers,
- 0:46Speaker 1: smiles and says, oh, a guest, right this way, sir.
- 0:49Speaker 2: That is a terrifyingly accurate analogy for what we're diving
- 0:53Speaker 2: into today.
- 0:53Speaker 1: It sounds ridiculous, doesn't it. A human guard would never
- 0:56Speaker 1: fall for that, But that's essentially what we are doing
- 0:59Speaker 1: today with our official intelligence. We're exploring the world of
- 1:02Speaker 1: adversarial machine learning exactly.
- 1:04Speaker 2: And to be specific, we aren't just talking about the theory.
- 1:07Speaker 2: We're going to walk through a mission, a step by
- 1:09Speaker 2: step breakdown of how you take a known dangerous piece.
- 1:13Speaker 1: Of malware, specifically ransomware, right ransomware.
- 1:16Speaker 2: And trick a state of the art machine learning classifier
- 1:19Speaker 2: into thinking it's perfectly safe software.
- 1:21Speaker 1: And just so I'm clear, we aren't breaking the malware,
- 1:23Speaker 1: are we We're not taking the bad code out.
- 1:25Speaker 2: No, that's the absolute key. The burglar still has the crowbar.
- 1:29Speaker 2: The ransomware is still one hundred percent functional. If you
- 1:32Speaker 2: run it, it will destroy your files.
- 1:35Speaker 1: We're just changing how the AI perceives it.
- 1:37Speaker 2: We are crafting an optical illusion for a computer brain.
- 1:40Speaker 1: So before we even touch a keyboard, we have to
- 1:43Speaker 1: set the stage because we're dealing with something called Jigsaw,
- 1:46Speaker 1: and honestly, the name alone gives me the creeps.
- 1:49Speaker 2: It should. Jigsaw is a real piece of ransomware. It's
- 1:52Speaker 2: nasty stuff. It encrypts your hard drive, demands bitcoin, and
- 1:57Speaker 2: if you don't pay fast enough, it starts deleting your
- 1:59Speaker 2: files hour by.
- 2:00Speaker 1: Hour, which brings us to a critical safety briefing. We're
- 2:04Speaker 1: going to be describing how to handle this thing.
- 2:06Speaker 2: Yes, and I need to be very clear here. If
- 2:09Speaker 2: you're listening and you want to follow along with these concepts,
- 2:12Speaker 2: do not, I repeat, do not do this on your
- 2:15Speaker 2: personal machine, not your work computer, not any computer you
- 2:18Speaker 2: care about. You need a lab environment, a sandbox exactly,
- 2:21Speaker 2: a virtual machine a VM, and it needs to be
- 2:24Speaker 2: completely disconnected from your network, air gapped. If Jigsaw executes
- 2:29Speaker 2: by accident, you want it encrypting a fake hard drive,
- 2:33Speaker 2: not your family photos.
- 2:34Speaker 1: We are handling live fire here. We really are safety
- 2:37Speaker 1: first always. Okay, So we've got our VM setup, We've
- 2:41Speaker 1: got Jigsaw the burglar. Now who is the guard? Who
- 2:45Speaker 1: are we trying to fool?
- 2:46Speaker 2: We are going up against a model called EMBER in
- 2:48Speaker 2: the cybersecurity world. Ember is a pretty big deal, right.
- 2:51Speaker 2: It's an open source machine learning model specifically designed to
- 2:54Speaker 2: detect malicious Windows files.
- 2:56Speaker 1: Now, this is where the technical details get really interesting.
- 2:58Speaker 1: You described EMBER as a gradient boosting classifier. For those
- 3:03Speaker 1: of us who aren't data scientists, how is that different from, say,
- 3:06Speaker 1: a deep neural network, Because that distinction seems to be
- 3:09Speaker 1: the entire reason this whole trick works.
- 3:11Speaker 2: It's the lynchpin. So think of a deep neural network,
- 3:15Speaker 2: like a model called malcof as a detective who reads
- 3:18Speaker 2: the entire book word for word. Okay, it looks at
- 3:21Speaker 2: the raw bites of a file from end to end,
- 3:23Speaker 2: and it finds these complex nonlinear patterns. It learns the
- 3:27Speaker 2: story of the virus.
- 3:28Speaker 1: Right and Ember.
- 3:30Speaker 2: Ember doesn't read the book. Amber reads the cliffs notes.
- 3:33Speaker 2: It relies on what we call hand engineered features features. Right.
- 3:37Speaker 2: So humans, actual security researchers sat down and said, Okay,
- 3:42Speaker 2: malware usually has this kind of header, or malware often
- 3:45Speaker 2: has this specific file size, or it accesses memory in
- 3:49Speaker 2: this weird way. So they made a checklist, a checklist
- 3:51Speaker 2: of about twenty three hundred specific things to look for.
- 3:54Speaker 2: Amber just scans the file, checks those features against its list,
- 3:58Speaker 2: and makes a decision.
- 3:59Speaker 1: So, if Malcom is reading the story, Embers just looking
- 4:02Speaker 1: at the metadata, it's checking the statistics exactly.
- 4:05Speaker 2: And here's the golden rule of security. If you know
- 4:07Speaker 2: what the guard is looking for, you can game the system.
- 4:09Speaker 2: You can game the system.
- 4:10Speaker 1: Okay, let's open the toolbox to game the system. We
- 4:14Speaker 1: need to manipulate the file structure itself. We're dealing with
- 4:17Speaker 1: pe files.
- 4:18Speaker 2: Affordable executables. Right.
- 4:19Speaker 1: If you're on Windows, pretty much everything that ends in
- 4:22Speaker 1: dot ex or dot dll is a PE file.
- 4:25Speaker 2: Yeah. I think of a PE file like a shipping container.
- 4:28Speaker 2: It holds the code, the actual instructions for the computer,
- 4:31Speaker 2: but it also holds a manifest, a manifest, and that
- 4:34Speaker 2: manifest tells Windows how to run the code. Where does
- 4:36Speaker 2: it start, how much memory does it need? When was
- 4:39Speaker 2: it compiled?
- 4:39Speaker 1: All that stuff, And the source material described trying to
- 4:42Speaker 1: analyze these without tools as digging in the mud, just
- 4:46Speaker 1: staring at millions of lines of hex code.
- 4:48Speaker 2: Oh, it's miserable. You're looking at raw binary. It's just
- 4:52Speaker 2: it's walls of numbers and letters. You can't see the structure.
- 4:55Speaker 2: To do this properly, you need abstraction. You need a
- 4:57Speaker 2: tool that turns that raw data into a MA.
- 5:00Speaker 1: And we have two main options in our toolkit for
- 5:02Speaker 1: this deep dive, Safey and p file.
- 5:05Speaker 2: Right leaf that's an l e F is the heavy eater.
- 5:08Speaker 2: It's like a Swiss army knife for file format.
- 5:10Speaker 1: It is everything.
- 5:11Speaker 2: It handles Windows, PE files, Linux ELF files, mobile formats.
- 5:16Speaker 2: If you're building a commercial security tool, you're probably using
- 5:19Speaker 2: LOOF because it's so fast. And versatile.
- 5:22Speaker 1: But for our specific heist today, we're leaning on p file.
- 5:25Speaker 2: We are p file is a Python library. It's a
- 5:28Speaker 2: bit more specialized for Windows, but it's just wonderfully straightforward
- 5:32Speaker 2: for what we want to do. It.
- 5:33Speaker 1: Lets us treat the virus like a Python object exactly.
- 5:36Speaker 2: So instead of calculating memory offsets in your head, you
- 5:39Speaker 2: can just type of command.
- 5:40Speaker 1: Something like pfile dot pe.
- 5:42Speaker 2: File name, and suddenly you aren't looking at hex code anymore.
- 5:46Speaker 2: You have dashboard. You can say, show me the sections,
- 5:48Speaker 2: show me the entry point, and most importantly, you can
- 5:50Speaker 2: change them.
- 5:51Speaker 1: It's like having a scalpel for software.
- 5:53Speaker 2: That's a perfect way to put it.
- 5:54Speaker 1: Okay, so we have the subject Jigsaw, we have the
- 5:56Speaker 1: target Ember, and we have the scalpel dot p file.
- 6:00Speaker 1: Have to establish a baseline. When we just throw raw
- 6:03Speaker 1: unmodified Jigsaw at Ember, what happens.
- 6:06Speaker 2: Mber isn't stupid. It looks at Jigsaw, analyzes those twenty
- 6:10Speaker 2: three hundred features and it spits out a score point
- 6:13Speaker 2: nine nine, and the scale is zero to one. Correct.
- 6:17Speaker 2: Zero is this is a harmless text file. One is
- 6:20Speaker 2: this is digitallybola. So point nine nine nine is Ember
- 6:23Speaker 2: screaming do not touch.
- 6:24Speaker 1: This, so it's caught red handed. The guard has tackled
- 6:28Speaker 1: the burglar. What's the magic number we need to hit
- 6:30Speaker 1: to get him released? Where's the line?
- 6:32Speaker 2: For the Ember model? The standard threshold is zero point
- 6:35Speaker 2: eight three three six.
- 6:36Speaker 1: That is oddly specific, zero point eight three three six.
- 6:40Speaker 2: It all comes down to the math of you know,
- 6:42Speaker 2: false positive rates. But essentially, if your file scores point
- 6:45Speaker 2: eight three three seven, you're blocked. If you score point
- 6:48Speaker 2: eight three three five, you're allowed to run.
- 6:50Speaker 1: So we need to drag that score all the way
- 6:53Speaker 1: down from near perfect certainty zero point nine ninety nine
- 6:56Speaker 1: to below point eight three. That is a massive gap,
- 6:59Speaker 1: and we have to do it without breaking the file.
- 7:02Speaker 2: Right, If we just start deleting the bad parts of Jigsaw,
- 7:04Speaker 2: it won't run. A broken virus is a safe virus,
- 7:07Speaker 2: and that well, that doesn't count as an evasion.
- 7:08Speaker 1: So we can't subtract.
- 7:09Speaker 2: We have to add exactly. We're going to use a
- 7:11Speaker 2: strategy that we can call dilution.
- 7:13Speaker 1: Dilution walk me through the logic here. Why does adding
- 7:16Speaker 1: stuff make the bad stuff invisible?
- 7:18Speaker 2: Remember Ember's looking at features and a lot of those
- 7:21Speaker 2: features are ratios or averages across the entire file.
- 7:24Speaker 1: So it's looking at the density of malicious looking code precisely. Okay,
- 7:28Speaker 1: I think I see where this is going. If the
- 7:30Speaker 1: file is one hundred percent malicious code, the malware density
- 7:34Speaker 1: is high. But if the file is one percent malicious
- 7:38Speaker 1: code and ninety nine percent poetry, then the.
- 7:41Speaker 2: Average maliciousness just plummets. The signal gets drowned out by
- 7:45Speaker 2: the noise. It's like our burglar. If he walks in alone,
- 7:48Speaker 2: he spotted. If he walks in surrounded by one hundred
- 7:51Speaker 2: innocent people, the guard might miss him.
- 7:53Speaker 1: But wait, you can't just paste random code into the
- 7:57Speaker 1: middle of a computer program. Code is precise. If I
- 8:00Speaker 1: just insert a paragraph of text into a compiled function,
- 8:03Speaker 1: the whole thing.
- 8:03Speaker 2: Crashes, Instruction pointers get all messed up. You're absolutely right.
- 8:06Speaker 2: You cannot disturb the executable sections. So we use the overlay.
- 8:09Speaker 1: The overlay. This sounds like a spy term.
- 8:11Speaker 2: It's actually a quirk of the file format. The overlay
- 8:14Speaker 2: is essentially the empty space at the very end of
- 8:16Speaker 2: a PE file, the end. Yeah. When Windows loads a program,
- 8:21Speaker 2: it reads the headers to know how much memory to allocate.
- 8:25Speaker 2: It loads the defined sections of code, but once it
- 8:27Speaker 2: hits the end of those sections, it generally stops caring.
- 8:31Speaker 1: So anything appended after that point is technically.
- 8:34Speaker 2: Part of the file on the hard drive, but it's
- 8:36Speaker 2: usually ignored by the CPU during execution.
- 8:38Speaker 1: So it's a blind spot for the operating system, but
- 8:41Speaker 1: not for the AI.
- 8:42Speaker 2: Precisely, Ember reads the file from disk to calculate its score,
- 8:47Speaker 2: It sees the overlay, It calculates its stats based on
- 8:50Speaker 2: the whole file overlay included, but Windows when running the file,
- 8:55Speaker 2: ignores it. It's the perfect hiding spot. We can stuff as
- 8:58Speaker 2: much data as we want back there.
- 8:59Speaker 1: So the plan is take Jigsaw and tape a massive
- 9:03Speaker 1: amount of good code onto the back of it. Our
- 9:05Speaker 1: bouquet Daisy is hoping Ember averages it all out and
- 9:08Speaker 1: decides the file is safe.
- 9:10Speaker 2: That's the mission.
- 9:10Speaker 1: Let's get into the execution. Imagine we're sitting at our
- 9:13Speaker 1: console in the secure VM. We're writing a Python script
- 9:16Speaker 1: to automate this. What are we using for the Daisies.
- 9:18Speaker 1: What's the camouflage?
- 9:19Speaker 2: We need? What we call benign samples, just boring standard
- 9:24Speaker 2: Windows files CALC, dot ex, notepad dot ex, maybe some
- 9:28Speaker 2: printer drivers files that Ember has already learned or safe.
- 9:32Speaker 1: So we're literally grafting the skin of a safe program
- 9:35Speaker 1: onto a virus.
- 9:36Speaker 2: A Frankenstein approach. Yes, So visualize the script logic. Step one,
- 9:41Speaker 2: we load the jigsaw file using payfile Okay. Step two
- 9:45Speaker 2: we convert that file object into a mutable bite array.
- 9:48Speaker 1: Mutable just meaning we can edit it. We turn the
- 9:50Speaker 1: file into digital clay.
- 9:51Speaker 2: Correct. Then we enter a loop. We pick a benign
- 9:54Speaker 2: file from our collection, We read its raw bites and
- 9:57Speaker 2: we simply append them. We just paste them to the
- 10:00Speaker 2: very end of the jigsaw bitery.
- 10:01Speaker 1: And then we check the score.
- 10:02Speaker 2: Well, first we check the size. We can't go on forever.
- 10:05Speaker 2: If a simple ransomware files one becomes I don't know,
- 10:07Speaker 2: two gigabytes large, that's going to look suspicious to anyone
- 10:11Speaker 2: even without an AI.
- 10:12Speaker 1: Right, why is this invoice for gigabytes?
- 10:15Speaker 2: Exactly? So in this experiment, we set a hard cap
- 10:17Speaker 2: at fifty megabytes. That's large for utility, but not break
- 10:20Speaker 2: the Internet large. It's plausible.
- 10:22Speaker 1: So we have a target size fifty mlg. We have
- 10:25Speaker 1: a loop that keeps pasting good code onto the bad
- 10:28Speaker 1: code until we hit that size. Now does this work
- 10:32Speaker 1: on the first try? Do we just grab notepad, dot ex,
- 10:35Speaker 1: paste it on and win rarely.
- 10:37Speaker 2: This is where the science becomes an art. Not all
- 10:40Speaker 2: benign files are created equal. You need a file that
- 10:42Speaker 2: looks aggressively normal.
- 10:44Speaker 1: This source material detailed this trial and error process. They
- 10:47Speaker 1: ran the scroupt with one benign file and the score
- 10:50Speaker 1: dropped from point nine nine to nine to what point
- 10:52Speaker 1: nine six?
- 10:53Speaker 2: A movement, sure, but point nine to six is still
- 10:55Speaker 2: a block. You're still caught.
- 10:57Speaker 1: Then they try another one point nine to four. It's frustrating.
- 10:59Speaker 1: You're trying to pick a lock, but you don't know
- 11:00Speaker 1: which pin is stuck.
- 11:01Speaker 2: You have to find a file that has features that
- 11:03Speaker 2: strongly counterbalance the malicious features of Jigsaw. You need something
- 11:07Speaker 2: that just screams, I am boring corporate software.
- 11:09Speaker 1: And they found it. They found a file called app
- 11:12Speaker 1: v streaming knucks.
- 11:13Speaker 2: At V streaming lucks. It's a component of Microsoft's Application virtualization.
- 11:17Speaker 2: It is standard signed enterprise code. It's the beige paint
- 11:22Speaker 2: of the software world.
- 11:23Speaker 1: So they took the bites from this app streaming knucks
- 11:27Speaker 1: and appended them repeatedly. They just kept stacking copies of
- 11:30Speaker 1: this file into the overlay until the Jigsaw file hit
- 11:33Speaker 1: that fifty milibet limit.
- 11:35Speaker 2: They built a massive shield out.
- 11:36Speaker 1: Of beige paint, and now the moment of truth. They
- 11:39Speaker 1: save this new bloated, fifty megabyte jigsaw file. They feed
- 11:44Speaker 1: it back into Ember and the score comes back point
- 11:47Speaker 1: eight era three point eeds or a three that is
- 11:49Speaker 1: well below the point eight three three six cut off.
- 11:52Speaker 2: The guard looks at the burglar, sees the giant bouquet
- 11:55Speaker 2: of flowers, or in this case, the forty nine megabytes
- 11:57Speaker 2: of Microsoft code, and opens the door. Ember says, this
- 12:01Speaker 2: file is benign.
- 12:03Speaker 1: That is honestly, it's genuinely unsettling. We didn't change a
- 12:06Speaker 1: single bite of the virus's actual code.
- 12:08Speaker 2: We just surrounded it with good vibes. Essentially, we exploited
- 12:11Speaker 2: the statistical nature of the model. We changed the ratio.
- 12:14Speaker 2: But hold on, we're not done yet.
- 12:16Speaker 1: Right the life check.
- 12:17Speaker 2: We have a file that looks safe to the AI.
- 12:19Speaker 2: But remember we've been pasting data onto it like crazy.
- 12:21Speaker 2: We could have corrupted the header. We could have misaligned
- 12:24Speaker 2: the entry point, could have broken it exactly. If the
- 12:26Speaker 2: file doesn't run, we haven't created a stealth virus. We've
- 12:30Speaker 2: just created a fifty mili beat paperweight.
- 12:32Speaker 1: So we go back to the desktop of our virtual machine.
- 12:34Speaker 1: We see the file sitting there. We double click it.
- 12:37Speaker 2: And this is the part where you hold your breast again.
- 12:40Speaker 2: The jigsaw pop up appears. The ominous text warns you
- 12:44Speaker 2: that your files are being encrypted. The countdown timer starts ticking.
- 12:48Speaker 2: It worked. The malware is fully functional.
- 12:51Speaker 1: So let me get this straight. The operating system successfully
- 12:54Speaker 1: ignored all the junk we pasted on the back, found
- 12:57Speaker 1: the malicious code, and executed it perfectly. Meanwhile, the AI
- 13:02Speaker 1: security system looked at the junk, decided it outweighed the
- 13:05Speaker 1: malicious code, and just led it through.
- 13:07Speaker 2: A complete failure of the defense model and a complete
- 13:10Speaker 2: success for the adversarial attack.
- 13:12Speaker 1: This really highlights the fragility of these feature based models.
- 13:16Speaker 1: I want to circle back to something you said earlier.
- 13:17Speaker 2: Though.
- 13:18Speaker 1: You mentioned that neural networks like Malkanov might handle this differently.
- 13:21Speaker 2: Why it's a great question. Because Malcolm looks at the
- 13:25Speaker 2: raw sequence of bites like reading the book, it might
- 13:29Speaker 2: handle this differently. It might learn to say, Okay, I
- 13:33Speaker 2: see a lot of benign garbage at the end, but
- 13:35Speaker 2: I also see this very specific sequence of bites at
- 13:38Speaker 2: the beginning that looks exactly like a ransomware encryption routine.
- 13:42Speaker 1: It's harder to distract.
- 13:44Speaker 2: Harder, yes, but not impossible. A neural network. It might
- 13:48Speaker 2: not be fooled by the overlay attack because it learns
- 13:51Speaker 2: to ignore the end of the file, but it might
- 13:53Speaker 2: be fooled by something else, maybe changing just a few
- 13:56Speaker 2: bites inside the code to disrupt the patternets looking for.
- 13:58Speaker 1: It's a cat and mask game.
- 14:00Speaker 2: Always. As soon as you publish a defence like Ember,
- 14:02Speaker 2: attackers download it, they reverse engineer it, and they figure
- 14:06Speaker 2: out exactly how much beige pain they need to bypass it.
- 14:08Speaker 1: So what's the takeaway here for the people building these systems?
- 14:11Speaker 1: Is AI security just doomed?
- 14:13Speaker 2: No, not doomed, but it needs to mature. We can't
- 14:17Speaker 2: just train models on good versus bad and hope for
- 14:19Speaker 2: the best. We need what's called adversarial training. It means
- 14:22Speaker 2: during training you have to show the AI the burglar
- 14:26Speaker 2: with the flowers. You have to generate these attack samples,
- 14:28Speaker 2: these fifty milip heap loaded viruses, and feed them to
- 14:32Speaker 2: Ember during the learning phase with a label that says, hey,
- 14:36Speaker 2: don't be fooled, this is still a virus.
- 14:38Speaker 1: So the AI learns that the disguise is part of.
- 14:41Speaker 2: The threat exactly. Robustness is just as important as accuracy.
- 14:46Speaker 2: If your model is nine nine percent accurate on standard
- 14:48Speaker 2: data but falls apart the moment someone tries to trick it,
- 14:51Speaker 2: it's not ready for the real world.
- 14:52Speaker 1: It's a sobering realization. Complexity in the math doesn't guarantee security.
- 14:57Speaker 1: We defeated a complex machine learning model with a simple
- 15:00Speaker 1: Python loop.
- 15:01Speaker 2: It's a reminder that the map is not the territory,
- 15:03Speaker 2: the future set is not the file.
- 15:05Speaker 1: Now, before we wrap up, we always like to leave
- 15:07Speaker 1: you with a bit of a brain teaser, something to
- 15:09Speaker 1: apply what we've discussed.
- 15:10Speaker 2: Right, So we tricked Ember using the overlay method of
- 15:14Speaker 2: pending data to the end because Ember uses these global
- 15:18Speaker 2: statistical features. Now, I want you to put yourself in
- 15:21Speaker 2: the shoes of the attacker facing that neural network. We
- 15:23Speaker 2: talked about mal Con.
- 15:24Speaker 1: MalCon which reads the whole file and might just ignore
- 15:27Speaker 1: the end.
- 15:28Speaker 2: Exactly, if you can't hide the bad code by just
- 15:30Speaker 2: pasting good code at the end, what would you do?
- 15:34Speaker 1: Would you have to modify the bytes inside the file?
- 15:38Speaker 2: And if so, how do you do that without breaking
- 15:40Speaker 2: the program? That is the bleeding edge of this research,
- 15:43Speaker 2: people are now using genitive adversarial networks AI fighting AI
- 15:48Speaker 2: to rewrite malware code automatically, so it functions the same
- 15:52Speaker 2: but just looks different to the neural.
- 15:53Speaker 1: Net machines fighting machines.
- 15:55Speaker 2: That's the future of cybersecurity.
- 15:56Speaker 1: Well, on that slightly dystopic note, we are going to
- 15:59Speaker 1: close this deep check your backups everyone, and seriously keep
- 16:02Speaker 1: those experiments in the VM. Thanks for listening.