Latest / CyberCode Academy / Course 30 - Practical Malware Development - Beginner Level | Episode 6: Developing a Command and Control (C2) System with PHP and MySQL
Transcript
- 0:00Speaker 1: So somewhere in the world right now, a single hacker
- 0:03Speaker 1: clicks a button on a web dashboard and ten thousand
- 0:06Speaker 1: infected laptops simultaneously start mining cryptocurrency.
- 0:11Speaker 2: Oh yeah.
- 0:11Speaker 1: Or on the flip side of that, an IT administrator
- 0:14Speaker 1: in some office building hits submit and three thousand corporate
- 0:19Speaker 1: laptops instantly update their security software.
- 0:23Speaker 2: Right, two completely different intents exactly.
- 0:25Speaker 1: And on the surface, both of these scenarios look like,
- 0:28Speaker 1: I don't know, black magic, But beneath that surface, it's
- 0:31Speaker 1: actually just a continuous loop of very basic, highly orchestrated
- 0:36Speaker 1: digital handshakes.
- 0:37Speaker 2: It really is a fascinating piece of hidden infrastructure. I mean,
- 0:41Speaker 2: we rely on centralized systems every single day, yet we
- 0:45Speaker 2: rarely stop to think about the mechanical reality of how
- 0:47Speaker 2: remote control actually works across the Internet.
- 0:49Speaker 1: Yeah, we just expect it to work, right, And.
- 0:51Speaker 2: Whether it's a malicious botnet or just a standard corporate network,
- 0:55Speaker 2: the underlying architecture, what we call a commanding control system,
- 0:58Speaker 2: is surprisingly similar, and that is.
- 1:01Speaker 1: Exactly what we are pulling apart today. Welcome to our
- 1:03Speaker 1: deep dive.
- 1:03Speaker 2: Glad to be here.
- 1:04Speaker 1: So we've got a stack of raw programming blueprints today
- 1:07Speaker 1: we're talking the actual server side scripts and architectural logic
- 1:11Speaker 1: that map out how to build a complete command control
- 1:14Speaker 1: system basically from scratch, from.
- 1:16Speaker 2: The ground up.
- 1:17Speaker 1: Yeah, and our mission for you today is to walk
- 1:20Speaker 1: through the logic of this back end loop step by step.
- 1:23Speaker 1: We're going to explore how remote devices actually check in,
- 1:27Speaker 1: how they receive their instructions, and how a human operator
- 1:32Speaker 1: orchestrates that entire dance through a custom dashboard.
- 1:35Speaker 2: And understanding this loop gives you a completely different perspective
- 1:39Speaker 2: on how the Internet operates, because we aren't just looking
- 1:41Speaker 2: at isolated lines of code today. We are looking at
- 1:43Speaker 2: the fundamental concepts of modern network architecture, oh for sure,
- 1:47Speaker 2: like data sanitization and state management. Once you grasp the
- 1:51Speaker 2: mechanics of how two machines securely talk to each other
- 1:54Speaker 2: and pass instructions without crashing, I mean you essentially understand
- 1:58Speaker 2: that the backbone of modern web development.
- 2:01Speaker 1: Okay, so let's look at the starting line. A device
- 2:03Speaker 1: needs to connect to the.
- 2:05Speaker 2: Central server, the initial handshake right in our blueprints.
- 2:08Speaker 1: This initial contact is handled by a registration script. It's
- 2:12Speaker 1: called register dot php. The remote device basically walks up
- 2:16Speaker 1: to the service front desk and has to hand over
- 2:18Speaker 1: an ID card exactly to do this, it sends what
- 2:21Speaker 1: is known in web protocols as a post request, and
- 2:25Speaker 1: this request contains three specific pieces of data about the
- 2:28Speaker 1: device its host name, its IP address, and its operating system.
- 2:33Speaker 2: And a post request is essentially the Internet's version of
- 2:37Speaker 2: a sealed envelope. It's the standard way to package up
- 2:40Speaker 2: data and deliver it to a web server without displaying
- 2:43Speaker 2: it out in the open like in the URL. But
- 2:45Speaker 2: the server is faced with a critical choice the moment
- 2:48Speaker 2: it receives that envelope, which is well, it can't just
- 2:50Speaker 2: blindly trust what's inside. It has to handle that incoming
- 2:53Speaker 2: data incredibly carefully before it stores it in the central database.
- 2:58Speaker 2: Our blueprints use a file called con pp to securely
- 3:01Speaker 2: manage that database connection.
- 3:03Speaker 1: Okay, let's unpack this because I'm stuck on that point. Wait,
- 3:06Speaker 1: why go through the hassle of a prepared statement or
- 3:08Speaker 1: secure connection. If I'm building this system, my instinct is
- 3:13Speaker 1: just to take the host name, the IP and immediately
- 3:17Speaker 1: drop it straight into the database to keep things moving.
- 3:20Speaker 1: Why do we need a security checkpoint for a simple registration?
- 3:24Speaker 2: Because trust and user input is basically the original sin
- 3:27Speaker 2: of web development.
- 3:28Speaker 1: Wow. Dramatic but true.
- 3:31Speaker 2: In the early days of the Internet, developers did exactly
- 3:34Speaker 2: what your instinct suggested. They took the incoming data and
- 3:37Speaker 2: dropped it straight into their database commands. This led to
- 3:40Speaker 2: a vulnerability known as sequel injection.
- 3:43Speaker 1: Right, I've heard of that.
- 3:44Speaker 2: It remains one of the most destructive flaws in Internet history.
- 3:48Speaker 1: So someone could send something that isn't a host name
- 3:50Speaker 1: at all, like a command.
- 3:52Speaker 2: Precisely, the database doesn't know the difference between a name
- 3:55Speaker 2: and an executable command unless you force it to. If
- 3:58Speaker 2: you just drop the data into the queer. Sorry, A
- 4:00Speaker 2: clever attacker could send a piece of malicious database code
- 4:03Speaker 2: instead of their actual computer name.
- 4:05Speaker 1: I see.
- 4:06Speaker 2: The database would read that code, assume the server authorized it,
- 4:09Speaker 2: and execute it. An attacker could literally tell your database
- 4:13Speaker 2: to delete the entire table of registered devices and the
- 4:16Speaker 2: database would just obey.
- 4:18Speaker 1: That is terrifying. Okay, So how do these blueprints solve that?
- 4:22Speaker 1: How do you accept the data without opening the door
- 4:24Speaker 1: to malicious code.
- 4:26Speaker 2: The architecture uses a technique called a prepared statement. So
- 4:30Speaker 2: instead of dynamically writing the database command with the incoming
- 4:33Speaker 2: data mixed in the script specifically prepares a rigid template first,
- 4:37Speaker 2: a template yeah, an insert into victims query. It tells
- 4:41Speaker 2: the database, I am going to insert a new row
- 4:43Speaker 2: into the table. Here's the exact structure, but I'm leaving
- 4:47Speaker 2: three empty boxes for the actual data and in the code,
- 4:50Speaker 2: those boxes are literally just three question marks like.
- 4:52Speaker 1: Drawing the outline of the data before it even arrives. Yes, yeh.
- 4:55Speaker 2: And those empty boxes are locked when the server finally
- 4:58Speaker 2: takes the host name, IP and operating system from that
- 5:01Speaker 2: PS request envelope. It uses a bind.
- 5:04Speaker 1: Parham method bind pair. Right.
- 5:07Speaker 2: It uses a string format specifically S, which stands for
- 5:10Speaker 2: three strings to safely lock those three pieces of data
- 5:14Speaker 2: into place before executing. It forces the database to view
- 5:17Speaker 2: the incoming information as a harmless string of characters.
- 5:21Speaker 1: Completely neutralizing any executable code hidden inside.
- 5:24Speaker 2: Exactly, it completely disarms it.
- 5:26Speaker 1: That is a brilliant way to sanitize the data. Now, obviously,
- 5:29Speaker 1: when a developer is building and testing this architecture, they
- 5:32Speaker 1: don't always have a physical secondary laptop sitting next to
- 5:36Speaker 1: them to act as the remote device.
- 5:38Speaker 2: Right, you need a way to simulate it.
- 5:39Speaker 1: Our source materials detail a really clever way to test
- 5:42Speaker 1: this practically without a real remote computer. Use a terminal
- 5:46Speaker 1: application called curl.
- 5:48Speaker 2: Oh, Curl's fantastic.
- 5:50Speaker 1: Yeah, you just install it like a pseudoapped to install
- 5:53Speaker 1: curl on a Linux machine and simulate the remote device
- 5:56Speaker 1: using the dashed option to attach that dummy data and
- 5:59Speaker 1: datchexot to send it to the registration.
- 6:01Speaker 2: Your tools like that are indispensable. It perfectly mimics the
- 6:05Speaker 2: behavior of a remote laptop checking in.
- 6:07Speaker 1: So you fired off, You check your database, and boom,
- 6:10Speaker 1: the test device is registered. It's on the books. But wait,
- 6:13Speaker 1: at this point, the device is just sitting in the database.
- 6:15Speaker 2: It's just a row of data.
- 6:17Speaker 1: Yeah, it's waiting in the lobby, so to speak. It's
- 6:20Speaker 1: completely useless unless the server actually gives it a job.
- 6:23Speaker 1: So how does the server push an instruction down the
- 6:26Speaker 1: wire to that specific machine. This leads us to our
- 6:33Speaker 1: next script to get command dot php.
- 6:35Speaker 2: Well, if that's actually a common misconception about how these
- 6:38Speaker 2: systems work, The server doesn't push the command down the
- 6:40Speaker 2: wire at all. The remote device is proactive. It has
- 6:43Speaker 2: to pull the command.
- 6:44Speaker 1: Wait, really, the device is the one initiating the conversation.
- 6:47Speaker 2: Always think about it. Firewalls are designed to block incoming
- 6:50Speaker 2: traffic from random servers. If the central server tried to
- 6:54Speaker 2: reach out and tap a corporate laptop on the shoulder,
- 6:56Speaker 2: the local network security would instantly block.
- 6:59Speaker 1: It because it's unsolicited in coming traffic.
- 7:02Speaker 2: That makes sense exactly, So the architecture flips the dynamic.
- 7:06Speaker 2: The remote device regularly sends another PST request to the server,
- 7:11Speaker 2: essentially knocking on the door and asking, Hey, do you
- 7:14Speaker 2: have any new orders for me?
- 7:15Speaker 1: I see. So the server receives that knock and it
- 7:18Speaker 1: has to look into the database to find out right.
- 7:20Speaker 2: It uses store result, bind result, and fetch functions to
- 7:23Speaker 2: pull a command from the database using a select query.
- 7:27Speaker 1: Okay, so let's say the server finds a command, maybe
- 7:30Speaker 1: an instruction to run a system diagnostic. It packages that
- 7:34Speaker 1: command up echoes it back to the remote device.
- 7:37Speaker 2: And then what's fascinating here is once the server echoes
- 7:40Speaker 2: the command back to the device, it immediately runs in
- 7:43Speaker 2: a UK.
- 7:44Speaker 1: Query an update query.
- 7:45Speaker 2: Yes, it immediately updates the database to clear that specific command.
- 7:49Speaker 2: It sets it to be completely empty.
- 7:51Speaker 1: I love the visual of this. It's exactly like a
- 7:53Speaker 1: secret agent eating the mission briefing immediately after eating it.
- 7:56Speaker 2: That is a perfect analogy.
- 7:57Speaker 1: But what's the architectural fallout if the server forgets to
- 8:01Speaker 1: wipe the command. Let's say the developer skips that update step.
- 8:05Speaker 2: Absolute chaos. Remember the remote device is on a loop.
- 8:09Speaker 2: It's going to chap in again, all right. If the
- 8:11Speaker 2: server doesn't wipe the database field. The next time that
- 8:14Speaker 2: device knocks and asks for orders, the server will see
- 8:17Speaker 2: the run a system diagnostic command still sitting there. It
- 8:21Speaker 2: will hand the command.
- 8:22Speaker 1: Out again, and the device runs it again.
- 8:24Speaker 2: You end up in an infinite loop, forcing the remote
- 8:26Speaker 2: machine to execute the exact same instruction over and over
- 8:30Speaker 2: until the system crashes. So that immediate wipe is the
- 8:33Speaker 2: only thing preventing a catastrophic duplicate execution.
- 8:37Speaker 1: Wow. Now, speaking of things going wrong, our blueprints bring
- 8:40Speaker 1: up a highly relatable programming reality right at this dispatcher face.
- 8:45Speaker 2: Oh, the debugging process theory is nice, but failure is
- 8:48Speaker 2: where you actually learn.
- 8:50Speaker 1: So true. So, while testing the CURL request to get
- 8:53Speaker 1: a test command, nothing was returning. The developer fired up
- 8:56Speaker 1: their digital stand in, knocked on the door, and the terminal.
- 8:59Speaker 2: Just state the dreaded silent failure. An error message is
- 9:03Speaker 2: actually a gift because it tells you what broke. A
- 9:06Speaker 2: silent failure means you have to play detective, So.
- 9:08Speaker 1: The developer walks us through the debugging logic. First step
- 9:12Speaker 1: check the Apache server logs, and the logs returned an
- 9:15Speaker 1: HTDP status code of two hundred OK.
- 9:18Speaker 2: And that status code is a perfect illustration of how
- 9:21Speaker 2: literal machines are. A four h four means not found,
- 9:24Speaker 2: a five hundred means the server code crashed, but a
- 9:28Speaker 2: two hundred OK means the connection was flawless.
- 9:31Speaker 1: But if the server didn't crash, why does the test
- 9:34Speaker 1: command come back?
- 9:35Speaker 2: Because a two hundred OK just means the server executed
- 9:38Speaker 2: the script from top to bottom without throwing a fatal error,
- 9:41Speaker 2: it doesn't mean the human logic.
- 9:42Speaker 1: Was correct, right, And sure enough, the culprits were simple
- 9:44Speaker 1: human error.
- 9:45Speaker 2: Two separate typos it's always typos, yep.
- 9:48Speaker 1: First, when simulating the remote device's knock, there was a
- 9:51Speaker 1: typo in the post parameter for the operating system. And second,
- 9:55Speaker 1: inside the back end script itself, the sqel query asking
- 9:58Speaker 1: the database for the command was misspelled. They typed the
- 10:01Speaker 1: column name as command comma N.
- 10:04Speaker 2: They drop the d it grounds as complex tech and
- 10:07Speaker 2: such a relatable experience. The database is entirely literal. It
- 10:11Speaker 2: looks at command, realizes it doesn't have a column with
- 10:13Speaker 2: that exact spelling, and simply returns nothing. No crash, just
- 10:16Speaker 2: no data.
- 10:17Speaker 1: It doesn't try to get your intentions. Yeah, but once
- 10:19Speaker 1: those typos were fixed, the simulated device received the test
- 10:23Speaker 1: command perfectly excellent. So the dispatcher phase is solid. The
- 10:27Speaker 1: device has its orders, it executes the command locally, but
- 10:31Speaker 1: now it has to report back. How does the server
- 10:33Speaker 1: catch that data?
- 10:34Speaker 2: Well, the system has to be a two way street.
- 10:36Speaker 2: Issuing the order is only half the.
- 10:38Speaker 1: Battle, which brings us to the mailroom phase. Yeah. The
- 10:40Speaker 1: script here is get results dot php. The device sends
- 10:45Speaker 1: another PST request, this time with its host name IP
- 10:49Speaker 1: and the result payload.
- 10:50Speaker 2: And the server receives this and uses another up day
- 10:53Speaker 2: query to set the command result field in the database.
- 10:56Speaker 1: But there's a wonderful little code trap highlighted in our
- 10:59Speaker 1: source maerials here that perfectly illustrates how rigid data mapping is.
- 11:04Speaker 1: When binding the parameters those locked boxes we talked about earlier,
- 11:07Speaker 1: the programmer accidentally put the host name in the first slot. Ah. Yeah,
- 11:10Speaker 1: but the sequel query was asking for the result first.
- 11:13Speaker 2: This touches on the absolute blindness of back end architecture.
- 11:18Speaker 2: A database doesn't possess artificial intelligence. It doesn't look at
- 11:22Speaker 2: a string of text and say, ah, server zero one,
- 11:24Speaker 2: that looks like a host name, I'll put it in
- 11:26Speaker 2: the host name column.
- 11:27Speaker 1: It's like sorting mail, but the postal worker only looks
- 11:30Speaker 1: at the shape of the envelope, not the address, and
- 11:32Speaker 1: just shoves it into the very first open slot they
- 11:36Speaker 1: see on the wall.
- 11:36Speaker 2: That's exactly it. Strict attention to parameter order is critical.
- 11:41Speaker 2: The database relies entirely on the developer mapping the exact sequence.
- 11:45Speaker 2: Once they fix that, a quick Curl test successfully injected
- 11:49Speaker 2: a test result into the database.
- 11:51Speaker 1: So at this point the core machine to machine loop
- 11:53Speaker 1: is fully functional. Devices register, they ask for work, execute it,
- 11:57Speaker 1: and post results. But up to this point, the server
- 12:00Speaker 1: and the device have just been talking to each other.
- 12:02Speaker 1: How does the human safely manage this without manually typing
- 12:05Speaker 1: database code in a dark room? Right?
- 12:07Speaker 2: You need a visual interface, a control panel, and.
- 12:10Speaker 1: Our blueprints detail exactly how to build this management UI
- 12:13Speaker 1: with two final pages manage dot php and show results
- 12:18Speaker 1: dot php. But because these pages literally hold the keys
- 12:21Speaker 1: to the kingdom, they introduce a vital new.
- 12:24Speaker 2: Security leader, session authentication.
- 12:26Speaker 1: Exactly, Session dot php ensures only authenticated users can access them.
- 12:31Speaker 1: If you aren't logged in, the server drops you.
- 12:34Speaker 2: Security is paramount.
- 12:35Speaker 1: Now I have a question about the HTML form where
- 12:38Speaker 1: the admin actually types the command. There's a visible text
- 12:40Speaker 1: box and a submit button, But looking at the code,
- 12:43Speaker 1: there's also a hidden input field holding a B parameter.
- 12:47Speaker 1: Why do we need an invisible box on our web page?
- 12:49Speaker 2: That invisible box is the glue for the entire targeted system.
- 12:53Speaker 2: Because HTTP is a stateless protocol, it has no memory
- 12:56Speaker 2: of what you just clicked. That B parameter securely tracks
- 12:59Speaker 2: the specific target's host name in the background.
- 13:01Speaker 1: So if an admin clicks on laptop forty two, the
- 13:04Speaker 1: URL secretly passes laptop forty two into that invisible.
- 13:07Speaker 2: Box exactly when submitted. An eeptet query assigns the command
- 13:10Speaker 2: specifically to that target. It guarantees precision so you don't
- 13:14Speaker 2: accidentally send a reboot command to the entire global network.
- 13:17Speaker 1: Okay, that makes total sense. Now let's look at the
- 13:19Speaker 1: text area below that form, which displays the results. This
- 13:23Speaker 1: is where the blueprints introduce some real asynchronous JavaScript magic.
- 13:27Speaker 2: This is what makes a modern web page feel.
- 13:29Speaker 1: Live, right. Instead of making the admin manually click refresh
- 13:33Speaker 1: over and over to see if a result has arrived,
- 13:35Speaker 1: JavaScript is used to silently check the database in the background.
- 13:38Speaker 2: It uses something called an XML HTTP request. It continuously
- 13:44Speaker 2: sends a geet request to that show results dot php
- 13:47Speaker 2: page we mentioned.
- 13:48Speaker 1: And the pacing of that is controlled right, Yes.
- 13:50Speaker 2: Yeah, the technical specifics are great. Here, a set timeout
- 13:53Speaker 2: function is used to fire this request every two thousand milliseconds,
- 13:57Speaker 2: so every two seconds, like clockwork. And if the response
- 14:01Speaker 2: text from the server is longer than two characters meaning
- 14:03Speaker 2: a real result came back. JavaScript uses document dot get
- 14:07Speaker 2: element by id to magically update the result area on
- 14:11Speaker 2: the administrator's screen.
- 14:12Speaker 1: The admin doesn't lift the finger, they hit submit, and
- 14:15Speaker 1: a few seconds later, the result just paints itself onto
- 14:18Speaker 1: their screen in real time.
- 14:20Speaker 2: It completely masks the complex database stands happening underneath.
- 14:24Speaker 1: It's an incredibly elegant illusion. So let's summarize this entire
- 14:28Speaker 1: architectural loop. Remote devices securely check in using prepared statements.
- 14:33Speaker 1: They constantly pull the server for instructions. The server delivers
- 14:37Speaker 1: the command and immediately wipes it. The devices report the
- 14:40Speaker 1: results back, and finally, an administrator manages the whole dance
- 14:45Speaker 1: through a dynamic auto refreshing UI.
- 14:48Speaker 2: It is a beautifully logical, self contained ecosystem.
- 14:52Speaker 1: It really is. Now, before we wrap up today's deep dive,
- 14:55Speaker 1: we want to make sure the core mechanics really stick
- 14:57Speaker 1: with you. So here's a quick review question for you
- 14:59Speaker 1: to think back to the dispatcher phase. If you want
- 15:04Speaker 1: a remote device to execute a command only once, what
- 15:07Speaker 1: critical step must your back end script take immediately after
- 15:10Speaker 1: echoing that command?
- 15:11Speaker 2: Right, we talked about the secret agent. You must run
- 15:13Speaker 2: and you PD eight query to wipe the command from
- 15:15Speaker 2: the database entirely. If you skip that step, you create
- 15:18Speaker 2: an infinite loop.
- 15:19Speaker 1: Exactly. It's such a vital mechanism, it is.
- 15:22Speaker 2: And as you think about these mechanics, I want to
- 15:23Speaker 2: leave you with a final thought to mull over.
- 15:25Speaker 1: Oh, let's hear it.
- 15:26Speaker 2: In our blueprints, the management page checks for new results
- 15:28Speaker 2: every two seconds. The remote devices are doing the same thing.
- 15:31Speaker 2: If you have one device polling a server every two seconds,
- 15:34Speaker 2: that's fine, it's nothing valuablyph right, But imagine scaling this.
- 15:38Speaker 2: What happens to your server load If you have one
- 15:40Speaker 2: hundred thousand devices constantly knocking on the door every two seconds.
- 15:45Speaker 2: How would you redesign this architecture to handle that massive scale?
- 15:49Speaker 1: Oh man, that is a fantastic engineering puzzle to leave
- 15:53Speaker 1: off on. We started today talking about how essentilized server
- 15:57Speaker 1: manages a massive fleet, and now you have the foundational
- 16:00Speaker 1: blueprints to understand exactly how it works. Thank you so
- 16:03Speaker 1: much for joining us on this deep dive. Keep questioning
- 16:05Speaker 1: the systems you interact with and we'll catch you on
- 16:07Speaker 1: the next one.