Latest / CyberCode Academy / Course 43 - Practical Malware Development | Episode 4: System Navigation and Command Execution
Transcript
- 0:00Speaker 1: Imagine you have a digital ghost living inside your computer.
- 0:03Speaker 2: Ooh, I mean, that sounds a little spooky. Right.
- 0:07Speaker 1: But seriously, think about a program that can silently navigate
- 0:10Speaker 1: through your private folders, check your system's identity, and run
- 0:14Speaker 1: these incredibly powerful commands.
- 0:16Speaker 2: And it does all of this entirely in the shadows.
- 0:19Speaker 1: Exactly. There's no icon on your taskbar, no window popping
- 0:22Speaker 1: up on your screen. It is just an invisible presence
- 0:26Speaker 1: quietly executing instructions.
- 0:28Speaker 2: So today we are basically dissecting the anatomy of that ghost.
- 0:32Speaker 2: We're doing a deep dive into the architecture of a
- 0:34Speaker 2: system control client. Yeah.
- 0:36Speaker 1: And usually, you know, this kind of architecture is discussed
- 0:39Speaker 1: in the context of malware, like attackers building remote access
- 0:43Speaker 1: Trojans to establish command and control channels over a compromised machine.
- 0:47Speaker 2: But we are totally flipping the script today. We're looking
- 0:50Speaker 2: at this purely as an educational teardown because knowing exactly
- 0:53Speaker 2: how the lock is picked is really the only way
- 0:55Speaker 2: to build a better door.
- 0:56Speaker 1: Well said. So We're treating this as an objective analysis
- 0:59Speaker 1: of the code logic. No screen needed today. We're just
- 1:03Speaker 1: going to walk you through how it's built piece by piece.
- 1:06Speaker 2: Right, because if we want to understand how these tools
- 1:08Speaker 2: operate under the hood, we have to start from the
- 1:10Speaker 2: very foundation.
- 1:11Speaker 1: The basics.
- 1:12Speaker 2: Yeah. Before a program can execute a malicious payload or
- 1:15Speaker 2: exfiltrate data, it needs the ability to perceive its environment.
- 1:20Speaker 2: It has to see its surroundings and move around the
- 1:22Speaker 2: file system.
- 1:23Speaker 1: And if it's going to move around, it obviously needs instructions.
- 1:26Speaker 2: Yeah.
- 1:26Speaker 1: Which brings us to the command parser.
- 1:28Speaker 2: The parser, yes.
- 1:29Speaker 1: I kind of look at this like a digital switchboard.
- 1:32Speaker 1: When the program receives a specific string of text from
- 1:35Speaker 1: a remote operator, say CD for change directory or Ls
- 1:41Speaker 1: to list files, the parser routes that action to a
- 1:45Speaker 1: specific internal function.
- 1:46Speaker 2: That's a good analogy. The parser functions as a series
- 1:49Speaker 2: of logical gates. It checks the incoming string against a
- 1:52Speaker 2: hard-coded list of known commands.
- 1:54Speaker 1: So when the gate for CD opens, what happens?
- 1:56Speaker 2: It triggers a function that relies on the setCurrentDirectory method.
- 2:00Speaker 2: This is pulled from the programming environment's directory class, which
- 2:02Speaker 2: is basically a library of pre-built tools for file management.
- 2:05Speaker 1: Okay, got it.
- 2:06Speaker 2: So calling that method immediately updates the program's working location
- 2:11Speaker 2: on the hard drive to whatever path the operator provided.
- 2:14Speaker 1: So it steps into a new room. But stepping into
- 2:17Speaker 1: a room is kind of useless if the lights are off, right?
- 2:19Speaker 2: Exactly.
- 2:20Speaker 1: If it receives that L's command, it has to look
- 2:23Speaker 1: around and report back what it sees in that specific folder.
- 2:27Speaker 2: Right. And the directory class gives us two specific methods
- 2:30Speaker 2: for this enumerate directories and enumerate files.
- 2:33Speaker 1: Sounds straightforward enough.
- 2:35Speaker 2: You'd think so. But when a program calls these methods,
- 2:38Speaker 2: the operating system doesn't just hand over a neatly formatted
- 2:41Speaker 2: text document.
- 2:42Speaker 1: Oh, it doesn't?
- 2:43Speaker 2: No, it returns a raw collection of data objects that
- 2:46Speaker 2: are just residing in memory. The real challenge for the
- 2:48Speaker 2: programmer is converting those raw system objects into a readable
- 2:52Speaker 2: format to send back over the network.
- 2:55Speaker 1: Let's talk about that memory aspect for a second, because
- 2:57Speaker 1: this is where I see a lot of beginner code
- 2:59Speaker 1: just totally fall apart. Oh, absolutely. In languages like C Sharp,
- 3:03Speaker 1: standard strings of text are immutable. You cannot just take
- 3:06Speaker 1: an existing string of text in the computer's memory and
- 3:09Speaker 1: dynamically tack new words onto the end of it.
- 3:11Speaker 2: Right. The computer actually has to destroy the old string,
- 3:14Speaker 2: allocate a brand new block of memory, and build a
- 3:17Speaker 2: completely new string containing the updated text.
- 3:20Speaker 1: Okay, let's unpack this. If I have a folder with
- 3:23Speaker 1: like 10,000 files in it, And the program tries to
- 3:26Speaker 1: build a list by appending one file name at a
- 3:29Speaker 1: time to a basic string.
- 3:31Speaker 2: It's going to be a mess. Yeah.
- 3:32Speaker 1: The system isn't just holding one long string. It created
- 3:35Speaker 1: and threw away 9,999 slightly smaller strings along the way.
- 3:41Speaker 1: That abandoned memory has to be cleaned up, doesn't it?
- 3:44Speaker 2: It does, which triggers aggressive garbage collection in the RAM.
- 3:48Speaker 1: And that's bad for stealth. Really bad.
- 3:50Speaker 2: The CPU is forced into this massive cycle of causing
- 3:53Speaker 2: the application. sweeping the memory heap to delete the discarded
- 3:57Speaker 2: strings and reallocating space. Doing this 10,000 times spikes the
- 4:01Speaker 2: CPU usage dramatically.
- 4:03Speaker 1: So for a program designed to be a digital ghost,
- 4:06Speaker 1: maxing out the processor just to read a folder completely
- 4:09Speaker 1: ruins its cover.
- 4:10Speaker 2: Exactly. To avoid setting off alarms with massive CPU spikes,
- 4:15Speaker 2: the architecture relies on a string builder class instead.
- 4:18Speaker 1: A string builder. Okay, using a string builder is like
- 4:21Speaker 1: stacking sticky notes.
- 4:23Speaker 2: I like that.
- 4:23Speaker 1: Instead of trying to memorize and recite every file in
- 4:26Speaker 1: a folder one by one, the program just keeps slapping
- 4:28Speaker 1: a new sticky note on a stack.
- 4:30Speaker 2: Right, it adds a new line break after each one.
- 4:32Speaker 1: Yeah, using environment.newline.
- 4:34Speaker 2: Yeah.
- 4:34Speaker 1: And when it's done, it hands you the whole neat
- 4:36Speaker 1: stack at once. It only does the heavy computational lifting
- 4:39Speaker 1: at the very end, right before transmitting the data back
- 4:42Speaker 1: to the server.
- 4:43Speaker 2: And, you know, the developer also made a very deliberate
- 4:45Speaker 2: choice regarding how they feed data into that string builder.
- 4:48Speaker 1: That's him.
- 4:49Speaker 2: They structure the code with a loop to iterate through
- 4:51Speaker 2: the collection of directories first, appending each folder name to
- 4:55Speaker 2: the object. Then they use that exact same StringBuilder instance
- 4:59Speaker 2: to loop through and append.
- 5:00Speaker 1: All the files. But wait, why go through the trouble
- 5:03Speaker 1: of separating the directory enumeration and file enumeration? Like, why
- 5:08Speaker 1: not just grab everything at once?
- 5:09Speaker 2: Because if you just ask the operating system to dump
- 5:12Speaker 2: everything into the list at once, you run into a
- 5:14Speaker 2: massive sorting problem on the back end.
- 5:16Speaker 1: Oh, I see. You'd just end up with a chaotic
- 5:18Speaker 1: jumble of Files and folders mixed together.
- 5:21Speaker 2: Exactly. By separating the enumeration, the output is naturally sorted.
- 5:26Speaker 2: The operator receives a clean list with all the folders
- 5:29Speaker 2: grouped at the top, followed by the files.
- 5:31Speaker 1: All while the program computationally reuses the exact same memory footprint.
- 5:36Speaker 1: That's super efficient. It is.
- 5:38Speaker 2: So now we've built a program that can silently map
- 5:40Speaker 2: out every room in the file system without triggering CPU alarms.
- 5:44Speaker 1: But mapping a house is useless if the operator doesn't
- 5:47Speaker 1: know whose house they broke into. The program has to
- 5:50Speaker 1: figure out its own identity and its clearance level.
- 5:52Speaker 2: Right. And this intelligence gathering operates on the same switchboard
- 5:56Speaker 2: logic from the parser we talked about.
- 5:58Speaker 1: So incoming keywords are mapped to system queries. Like, a
- 6:01Speaker 1: command like hostname triggers a function to fetch the computer's
- 6:05Speaker 1: network name. Yep.
- 6:07Speaker 2: And pwd print working directory returns the program's current location
- 6:10Speaker 2: on the disk.
- 6:11Speaker 1: There is also an element of data consolidation happening here, right?
- 6:14Speaker 2: There is. If the operator requests process info... The program
- 6:19Speaker 2: grabs both the process name and the unique numerical process ID.
- 6:23Speaker 2: It formats them together with a space in between just
- 6:27Speaker 2: to save network bandwidth during transmission.
- 6:29Speaker 1: Here's where it gets really interesting to me. It's like
- 6:31Speaker 1: a spy checking their ID badge, figuring out their cover story,
- 6:36Speaker 1: and checking if they have the VIP pass, which in
- 6:38Speaker 1: this case is administrator privileges.
- 6:40Speaker 2: That's a great way to put it. To establish its
- 6:43Speaker 2: location in the world, the program gathers its IPv4 address,
- 6:46Speaker 2: which gives the operator geographical and network context.
- 6:50Speaker 1: And it checks its own executable path, so it knows
- 6:52Speaker 1: exactly where its binary file is hiding on the hard drive.
- 6:55Speaker 2: Right. And like you mentioned with the VIP pass, it
- 6:57Speaker 2: evaluates the current user privileges to determine if it landed
- 7:01Speaker 2: in a restricted guest account or if it has full
- 7:03Speaker 2: admin rights.
- 7:04Speaker 1: And the system returns those privileges as a Boolean value, right?
- 7:08Speaker 1: Like a simple true or false.
- 7:10Speaker 2: Yes, but transmitting a raw Boolean over a basic tech
- 7:13Speaker 2: socket can be really unpredictable depending on the receiving server.
- 7:17Speaker 1: Ah, so the program converts that true or false into
- 7:20Speaker 1: a literal text string before sending it out.
- 7:22Speaker 2: Exactly. All of this identity data, the host name, the
- 7:26Speaker 2: IP address, the admin privileges, it's all packaged into a
- 7:29Speaker 2: custom structure called a general info class.
- 7:32Speaker 1: So the program instantiates this class. and gathers all this
- 7:36Speaker 1: data the moment it boots up.
- 7:37Speaker 2: Meaning it is caching the data in its own memory heap.
- 7:41Speaker 1: Let me challenge that design for a second. Why build
- 7:43Speaker 1: a custom general info class to hold all this data?
- 7:46Speaker 1: Why not just ask the operating system for the information
- 7:49Speaker 1: fresh every single time a command is sent?
- 7:51Speaker 2: Well, it comes down to efficiency and stealth. Operating systems
- 7:54Speaker 2: are divided into user space, where applications run, and kernel space,
- 7:58Speaker 2: where the core system operates and interfaces with the hardware.
- 8:01Speaker 1: And user space is highly restricted.
- 8:03Speaker 2: Very restricted. Every time a program asks for core system data,
- 8:07Speaker 2: the CPU has to perform what's called a context switch.
- 8:10Speaker 1: So it pauses the user application, switches over to kernel
- 8:13Speaker 1: mode privileges, executes the system call to retrieve the data,
- 8:17Speaker 1: and then switches back to user mode.
- 8:18Speaker 2: You got it. So the CPU literally stops running our program,
- 8:22Speaker 2: saves where it was, changes its security context, does the work,
- 8:26Speaker 2: and returns.
- 8:27Speaker 1: Wow. So if an attacker is running an automated script
- 8:30Speaker 1: that rapidly fires dozens of commands asking for system info,
- 8:34Speaker 1: forcing that context switch over and over would generate a
- 8:38Speaker 1: massive amount of noisy traffic.
- 8:39Speaker 2: Exactly. Between the program and the OS kernel, it gets
- 8:42Speaker 2: very loud. And modern heuristic antivirus software monitors the frequency
- 8:47Speaker 2: of system calls.
- 8:48Speaker 1: So a sudden flurry of kernel requests from an unknown
- 8:50Speaker 1: background process is going to be a massive red flag.
- 8:53Speaker 2: A huge red flag. By querying the information once it's
- 8:56Speaker 2: startup and storing it inside the instantiated general info class,
- 9:00Speaker 2: the data is just readily available in the program's own
- 9:03Speaker 2: local memory.
- 9:04Speaker 1: So when the operator asks for the host name, the
- 9:06Speaker 1: program bypasses the kernel entirely and just reads its own
- 9:09Speaker 1: memory cache.
- 9:10Speaker 2: Right, which drastically reduces its behavioral footprint. The only exception
- 9:14Speaker 2: to this caching rule is data that is inherently dynamic.
- 9:18Speaker 1: Like the current working directory.
- 9:19Speaker 2: Exactly. That has to be updated actively as the program
- 9:21Speaker 2: navigates the host system.
- 9:23Speaker 1: Okay, so... Hard coding commands like hostname or cd is
- 9:28Speaker 1: great for basic intelligence gathering, but a truly powerful program
- 9:32Speaker 1: needs to adapt.
- 9:33Speaker 2: It does. It creates a ceiling on what the program
- 9:35Speaker 2: can do if it can only understand five or six words.
- 9:38Speaker 1: Right. So what happens when it receives a complex custom
- 9:41Speaker 1: command that the parser doesn't recognize? It builds a secret
- 9:45Speaker 1: trapdoor directly to the computer's brain.
- 9:47Speaker 2: The architecture solves this limitation by building a fallback mechanism
- 9:51Speaker 2: to the computer's underlying command line interface. When a command
- 9:54Speaker 2: bypasses the known keyword list, the program turns to the system.diagnostics
- 9:59Speaker 2: name Which is.
- 10:00Speaker 1: A library of pre-built classes designed for interacting with system processes, right?
- 10:05Speaker 2: Yep. Event logs, performance counters, all of that. It utilizes
- 10:09Speaker 2: the process class to spawn a new hidden pipeline directly
- 10:12Speaker 2: to cmd.exil, the Windows command prompt.
- 10:15Speaker 1: So it just feeds the unrecognized command directly into that prompt.
- 10:19Speaker 1: but it has to pass a very specific argument first,
- 10:21Speaker 1: the slash C flag.
- 10:22Speaker 2: Yes. The slash me flag is actually a legacy command
- 10:26Speaker 2: carrying over from the early days of MS-DOS before Windows
- 10:30Speaker 2: even had a graphical user interface. Oh, wow.
- 10:32Speaker 1: That's old school.
- 10:33Speaker 2: It is. Command dot XX is a command interpreter. Passing
- 10:37Speaker 2: the slash C flag tells the interpreter to carry out
- 10:40Speaker 2: the string of text that follows and then immediately terminate
- 10:43Speaker 2: the command prompt session.
- 10:45Speaker 1: Because without that termination flag, Every single custom command sent
- 10:49Speaker 1: by the attacker would open a new permanent instance of cmd.xx
- 10:54Speaker 1: in the background.
- 10:55Speaker 2: Right, and you'd end up with dozens of zombie processes
- 10:58Speaker 2: draining the CPU and RAM.
- 11:00Speaker 1: The user would open their task manager and see 50
- 11:02Speaker 1: command prompts running silently, which immediately alerts them that the
- 11:06Speaker 1: machine is compromised.
- 11:08Speaker 2: Exactly. The program has to manage the lifecycle of its
- 11:10Speaker 2: own background processes meticulously.
- 11:13Speaker 1: But even with the slash C flag terminating the process
- 11:15Speaker 1: after execution... Doesn't invoking cmd.exec normally cause a black console
- 11:20Speaker 1: window to briefly flash onto the screen?
- 11:23Speaker 2: It does, and a black box flashing on the desktop
- 11:25Speaker 2: is the fastest way to get caught.
- 11:26Speaker 1: Yeah, that's a dead giveaway.
- 11:28Speaker 2: To achieve total stealth, the programmer has to subvert the
- 11:31Speaker 2: operating system's default behavior. They do this by setting two
- 11:35Speaker 2: critical properties in the process configuration object before it launches.
- 11:40Speaker 1: This is the stealth factor.
- 11:41Speaker 2: Exactly. First, they set useShellExecute to false. The shell is
- 11:46Speaker 2: the graphical interface of the operating system, the desktop manager,
- 11:49Speaker 2: the taskbar, the visual windows.
- 11:52Speaker 1: So setting useShellExecute to false explicitly instructs the Windows subsystem
- 11:57Speaker 1: not to wrap the new process in a graphical interface.
- 12:00Speaker 2: Right. The program bypasses the shell entirely and communicates directly
- 12:04Speaker 2: with the executable binary. And the second property is even
- 12:07Speaker 2: more direct. CreateNoWindow is set to true.
- 12:10Speaker 1: It's like a ventriloquist act.
- 12:12Speaker 2: A ventriloquist act.
- 12:13Speaker 1: Yeah, the program is making the computer speak and execute
- 12:15Speaker 1: arbitrary commands, but his lips aren't moving. There's no window
- 12:19Speaker 1: drawn on the screen, no visual flash, no icon on
- 12:22Speaker 1: the taskbar.
- 12:23Speaker 2: That's spot on. The user sitting at the keyboard typing
- 12:26Speaker 2: an email sees absolutely nothing.
- 12:27Speaker 1: But creating a windowless process introduces a pretty severe engineering
- 12:31Speaker 1: problem regarding data retrieval, doesn't it?
- 12:33Speaker 2: It does. In POSIX standards and Standard C programming, processes
- 12:37Speaker 2: rely on three default streams— standard input, standard output, and
- 12:42Speaker 2: standard error.
- 12:43Speaker 1: And the Windows command prompt was designed to display its
- 12:46Speaker 1: standard output visually.
- 12:48Speaker 2: Right. If you silently execute ipconfig slash all to dump
- 12:52Speaker 2: the system's network configuration, the command prompt attempts to print
- 12:56Speaker 2: the results to a console screen that just does not exist.
- 12:59Speaker 1: The standard output just disappears into the void. So to
- 13:03Speaker 1: actually read the stolen data, the program has to intercept
- 13:06Speaker 1: those data streams at the operating system pipe level before
- 13:09Speaker 1: they reach the non-existent screen.
- 13:11Speaker 2: And it does this by setting a property called redirectStandardOutput
- 13:14Speaker 2: to true. This intercepts the text that normally would have
- 13:17Speaker 2: been rendered visually.
- 13:18Speaker 1: And then it uses a built-in method called readTone.
- 13:21Speaker 2: Yes. It relies on readTone to gather all of that
- 13:24Speaker 2: intercepted text out of the buffer, bundles it into a
- 13:27Speaker 2: single string variable, and transmits it back to the remote server.
- 13:31Speaker 1: Okay, so capturing the standard output makes sense. We want
- 13:33Speaker 1: the answer to our command. But why go out of
- 13:37Speaker 1: our way to capture the standard error as well? Because
- 13:39Speaker 1: the architecture deliberately sets up a second redirection for that, right?
- 13:43Speaker 2: It does. It sets redirect standard error to true and
- 13:46Speaker 2: uses retune again to append any error messages directly to
- 13:50Speaker 2: the successful output payload.
- 13:52Speaker 1: But why?
- 13:53Speaker 2: Because blindness is the worst enemy of a remote operator.
- 13:56Speaker 2: If an attacker sends a command and receives absolute silence
- 13:59Speaker 2: in return, they have zero context for what went wrong.
- 14:02Speaker 1: Oh, I see. The network connection might have dropped or
- 14:04Speaker 1: the command syntax might be flawed.
- 14:06Speaker 2: Or the system might have actively blocked the execution due
- 14:09Speaker 2: to insufficient administrative privileges. By capturing the standard error stream,
- 14:15Speaker 2: the operator gets the exact failure message generated by the OS.
- 14:18Speaker 1: They know immediately if they typed a directory name wrong
- 14:22Speaker 1: or if they triggered a permission denial.
- 14:24Speaker 2: Exactly. It allows them to troubleshoot their own malware and
- 14:27Speaker 2: adjust their tactics in real time, all without ever alerting
- 14:30Speaker 2: the actual user whose machine is acting as the host.
- 14:33Speaker 1: It's wild. The tools designed for benign system administration are
- 14:38Speaker 1: simply being repurposed for covert control. Like, the process class
- 14:42Speaker 1: and the directory class are standard tools for developers.
- 14:45Speaker 2: They are. But when orchestrated with this specific logic, they
- 14:49Speaker 2: operate completely below the surface layer of user interaction.
- 14:53Speaker 1: It is a perfectly closed loop. So we have logically
- 14:56Speaker 1: stepped through how a shadow program navigates a system, utilizing
- 15:00Speaker 1: a string builder to neatly stack its findings without causing
- 15:04Speaker 1: massive garbage collection spikes.
- 15:06Speaker 2: We explored how it maps its environment, caching its identity
- 15:10Speaker 2: to avoid noisy context switching.
- 15:12Speaker 1: And finally, we uncovered how it opens an invisible portal
- 15:15Speaker 1: to execute arbitrary commands by bypassing the graphical shell entirely
- 15:19Speaker 1: and redirecting the standard output and error streams.
- 15:22Speaker 2: Quite the journey.
- 15:23Speaker 1: Truly. Before we go, a quick review question for you
- 15:26Speaker 1: to test what you've learned today. Think back to our
- 15:28Speaker 1: secret trapdoor. If you are coding a process and you
- 15:31Speaker 1: want to ensure the command prompt remains completely invisible to
- 15:34Speaker 1: the user, what are the two specific properties you need
- 15:37Speaker 1: to set in the code?
- 15:39Speaker 2: No looking back at your notes.
- 15:40Speaker 1: I'll give you a second to think about it. If
- 15:42Speaker 1: you said setting... You shall execute to false and create
- 15:46Speaker 1: no window to true. You nailed it. Those are the
- 15:49Speaker 1: technical keys to the ventriloquist act.
- 15:51Speaker 2: We've just seen how easy it is to write code
- 15:54Speaker 2: that runs entirely in the shadows, executing commands and gathering
- 15:57Speaker 2: data without triggering a single visual cue on the screen.
- 16:02Speaker 2: So if a program can do all of this silently,
- 16:04Speaker 2: how would you go about detecting it on your own machine?
- 16:07Speaker 1: Ooh.
- 16:08Speaker 2: Yeah, it's something to think about the next time you
- 16:10Speaker 2: open your task manager and look at the background processes.
- 16:12Speaker 1: A slightly terrifying but incredibly important thought to leave on. Remember,
- 16:18Speaker 1: just because you can't see the ghost doesn't mean it's
- 16:20Speaker 1: not looking around. Catch you on the next deep dive.