Latest / CyberCode Academy / Course 30 - Practical Malware Development - Beginner Level | Episode 3: Enhancing Agent Resilience and Establishing Remote Server
Transcript
- 0:00Speaker 1: Imagine you're writing a piece of code, right, like a
- 0:04Speaker 1: remote agent or a script that you deploy onto another machine,
- 0:08Speaker 1: and it has this one critical mission. It needs to
- 0:11Speaker 1: sit there quietly, receive your commands across a network, execute them,
- 0:15Speaker 1: and then send the data back.
- 0:17Speaker 2: Yeah, which sounds simple enough in.
- 0:19Speaker 1: Theory, right, But then imagine you're typing a command and
- 0:22Speaker 1: you make a tiny syntax error, like you just miss
- 0:25Speaker 1: a single space character, or maybe a router misroutes a packet,
- 0:29Speaker 1: and you get this momentary connection drop.
- 0:31Speaker 2: Well, if the architecture of that agent isn't specifically built
- 0:34Speaker 2: to handle those weird edge cases, it just panics, exactly.
- 0:37Speaker 1: It throws an unhandled exception, the whole process terminates, and
- 0:41Speaker 1: suddenly you know your remote access is just dead in
- 0:43Speaker 1: the water. You've literally lost your entire foothold over a typo.
- 0:47Speaker 2: And that is the textbook definition of brittle architecture. Yeah,
- 0:50Speaker 2: because in remote operations, the environment you're in is inherently hostile.
- 0:55Speaker 2: I mean, it's unpredictable.
- 0:56Speaker 1: Yeah, making things work when conditions are perfect is well,
- 0:59Speaker 1: it's pretty crivial.
- 1:00Speaker 2: It is the actual engineering challenge. The real test is
- 1:04Speaker 2: building localized fault tolerance. You have to create a system
- 1:07Speaker 2: that's designed from the ground up to absorb those failures,
- 1:11Speaker 2: heal itself, and crucially not draw any attention when things
- 1:14Speaker 2: inevitably go wrong.
- 1:15Speaker 1: And that is exactly the mission of our deep dive today.
- 1:19Speaker 1: We are going to step through the architecture of a
- 1:22Speaker 1: highly resilient, fault tolerant remote connection program written in C sharp.
- 1:27Speaker 2: Yeah, we're really getting under the hood today.
- 1:29Speaker 1: We are. We're going to look at how to bulletproof
- 1:31Speaker 1: the core logic, how to preempt those annoying operator errors,
- 1:36Speaker 1: and how to establish a persistent connection that basically knows
- 1:40Speaker 1: how to manage its own life cycle.
- 1:41Speaker 2: Right, So, before a program can even safely reach out
- 1:44Speaker 2: across a network, its internal logic has to be stabilized.
- 1:47Speaker 2: I mean every core file, interaction or system function, whether
- 1:50Speaker 2: you're like downloading a payload or just enumerting a working directory,
- 1:54Speaker 2: it has to be isolated within its own fault domain.
- 1:57Speaker 1: Okay, so how do we actually achieve that isolation.
- 2:00Speaker 2: Well, we do it by wrapping these high risk operations
- 2:02Speaker 2: entirely inside trying catch blocks.
- 2:04Speaker 1: Okay, let's unpack that for a second. Think of this
- 2:08Speaker 1: like having a safety net under a tightrope walker. You know,
- 2:12Speaker 1: you try the trick, but if you slip, the catch
- 2:14Speaker 1: block gracefully catches you instead of just resulting in this
- 2:18Speaker 1: catastrophic system crash.
- 2:20Speaker 2: That's a great way to look at it. Containment is
- 2:21Speaker 2: step one, but step two is actually getting actionable intelligence
- 2:25Speaker 2: out of that failure.
- 2:26Speaker 1: Right, because you need to know why you fell into
- 2:29Speaker 1: the net exactly.
- 2:30Speaker 2: So when the catch block intercepts the crash, it grabs
- 2:33Speaker 2: the active instance of the exception class let's just call
- 2:36Speaker 2: it E. The operator needs to know exactly why the
- 2:38Speaker 2: execution failed, so they can, you know, adjust their next move.
- 2:43Speaker 1: But you can't really just send raw exception objects back
- 2:46Speaker 1: over the network, can you.
- 2:48Speaker 2: No, no way. You can't serialize and transmit those objects
- 2:51Speaker 2: over an asyncretous HTTP polling channel without adding massive overhead.
- 2:56Speaker 2: Plus you risk causing even more parser errors on the
- 2:59Speaker 2: server side.
- 3:00Speaker 1: So what's the work around there.
- 3:01Speaker 2: You need a flat string. You take that exception instance,
- 3:04Speaker 2: you access the message property, and you just use the
- 3:07Speaker 2: toe string method.
- 3:08Speaker 1: Ah okay, so that strips away all a complex optic wrappers.
- 3:11Speaker 2: Yeah, it leaves you with a clean, readable text string,
- 3:15Speaker 2: something simple like access denied or file not found. And
- 3:18Speaker 2: because of this, you have to modify the return type
- 3:21Speaker 2: for all of these core functions so they return a string.
- 3:24Speaker 1: And I assume you apply this to successful operations as well,
- 3:27Speaker 1: Right Like, if a file downloads perfectly, the function returns
- 3:30Speaker 1: a string saying you know, file save to whatever.
- 3:33Speaker 2: Path, right exactly, Because in a remote shell scenario, you
- 3:36Speaker 2: are essentially operating entirely blind.
- 3:39Speaker 1: Yeah, you don't have standard output just popping up on
- 3:42Speaker 1: your screen like a local session.
- 3:43Speaker 2: Precisely, that string returned by the function is literally your
- 3:47Speaker 2: only telemetry. Without casting those outcomes to strings and pushing
- 3:51Speaker 2: them back, the operator has no idea if the command worked,
- 3:54Speaker 2: or if an antivirus blocked it or what.
- 3:56Speaker 1: So catching those runtime errors with their safety net is
- 3:59Speaker 1: obviously crucial, but preventing bad input from triggering an exception
- 4:03Speaker 1: in the first place is even better. Let's look at
- 4:06Speaker 1: a really common bug in basic command parsers.
- 4:09Speaker 2: Yeah, this is a classic issue. So the parser's job
- 4:11Speaker 2: is to take the rostering you type, say download tailo
- 4:15Speaker 2: dot xe, and split it based on the space character.
- 4:18Speaker 1: Right, So index zero becomes the command and index one
- 4:22Speaker 1: becomes the argument.
- 4:23Speaker 2: Exactly, and that logic works perfectly right up until the
- 4:26Speaker 2: operator types a single word command.
- 4:28Speaker 1: Oh right, like just typing l's or woo mean yeah.
- 4:33Speaker 2: If you pass a bear command like that and hit enter,
- 4:35Speaker 2: there is no space character anywhere in the string, but
- 4:39Speaker 2: the standard split function just executes blindly anyway. It creates
- 4:42Speaker 2: an array with the length of exactly one, and.
- 4:44Speaker 1: Then the code immediately tries to assign index one to
- 4:47Speaker 1: the argument variable.
- 4:48Speaker 2: Which doesn't exist, so it throws an index out of bounds. Exception,
- 4:52Speaker 2: the whole logic just shatters because it expected a delimiter
- 4:55Speaker 2: that simply wasn't there.
- 4:56Speaker 1: That's so frustrating. So how do we fix that without
- 4:58Speaker 1: just relying on the try catch blow to catch the crash?
- 5:01Speaker 2: Well, we bypass it entirely by injecting an Ifel's logic
- 5:04Speaker 2: tree right before the string manipulation even happens.
- 5:06Speaker 1: Okay, so we're evaluating the raw input.
- 5:08Speaker 2: First, right, The code checks the strength. If it detects
- 5:12Speaker 2: a space character, great, it proceeds with the standard split
- 5:15Speaker 2: populating the command and the argument. But if it doesn't
- 5:18Speaker 2: find a space meaning the condition returns false. It just
- 5:22Speaker 2: immediately initializes the command variable with the entire raw string itself,
- 5:26Speaker 2: and then it.
- 5:27Speaker 1: Just leaves the argument variable as an empty string exactly.
- 5:30Speaker 2: It anticipates the format constraint. You bypass the exception state completely.
- 5:34Speaker 2: You don't even need the circuit breaker to trip.
- 5:36Speaker 1: That is super elegant, and there's actually this huge quality
- 5:40Speaker 1: of life improvement that ties directly into dealing with these
- 5:43Speaker 1: empty arguments, like with the directory enumeration function, which acts
- 5:48Speaker 1: like your standard Doull's command.
- 5:49Speaker 2: Right, So when an operator wants a directory listing but
- 5:53Speaker 2: they don't specify a target path, that argument variable is empty. Now,
- 5:58Speaker 2: a brutal program would either just pass that no value
- 6:00Speaker 2: to the operating system and crash, or it would throw
- 6:03Speaker 2: an error yelling at you to provide a valid path.
- 6:06Speaker 1: Right, But we don't want it to do that.
- 6:08Speaker 2: No, Instead, we design the function to look for that
- 6:10Speaker 2: empty strength. If it sees one, it dynamically defaults to
- 6:14Speaker 2: the current working directory of the payload itself. It pulls
- 6:18Speaker 2: that active path from an instance of a general infoclass,
- 6:21Speaker 2: specifically a variable called sea directory.
- 6:24Speaker 1: Okay, here's where it gets really interesting. But I have
- 6:26Speaker 1: to push back on this a little bit. Why go
- 6:28Speaker 1: through the effort of building in these default behaviors for
- 6:32Speaker 1: empty arguments? What do you mean, well, why not just
- 6:34Speaker 1: force the user to always type out the full path,
- 6:37Speaker 1: Like if they want to see the desktop, make them
- 6:39Speaker 1: type ce colon slash users slash admin slash desktop. Wouldn't
- 6:44Speaker 1: strict syntax be I don't know, more predictable, sure.
- 6:47Speaker 2: I mean mathematically predictable. Yeah, but operationally it would be exhausting.
- 6:51Speaker 2: You really have to factor in operator fatigue.
- 6:53Speaker 1: Okay, fair point.
- 6:54Speaker 2: When you're navigating a file system remotely, you rely on
- 6:57Speaker 2: the same muscle memory you use on your your own
- 7:00Speaker 2: local machine. If you type drror, you naturally expect to
- 7:03Speaker 2: see the context of where you currently are.
- 7:05Speaker 1: Yeah, that makes sense. I would hate having to type
- 7:08Speaker 1: the absolute path every single time, exactly.
- 7:11Speaker 2: Forcing an operator to repeatedly query their path and manually
- 7:15Speaker 2: append it just introduces massive friction. It slows you down,
- 7:19Speaker 2: and honestly, it increases the likelihood of human error when
- 7:22Speaker 2: you're under pressure. Good code maps to natural human behavior.
- 7:25Speaker 2: A developer should anticipate lazy typing, right.
- 7:28Speaker 1: You want the tool to reduce your cognitive load, not
- 7:31Speaker 1: add to it. So okay, our internal architecture is solid.
- 7:35Speaker 1: The logic handles invalid syntax smoothly. The fault domains protect
- 7:39Speaker 1: the main thread. Basically, the agent is stable in a vacuum, right, But.
- 7:43Speaker 2: A remote agent isn't very useful in a vacuum.
- 7:45Speaker 1: True, So now we need to architect the outbound network routing.
- 7:49Speaker 1: It's time to make it phone home to the attacker
- 7:51Speaker 1: server or the command and control infrastructure.
- 7:54Speaker 2: Yeah, we have to move from a localized script to
- 7:56Speaker 2: an active agent. So first we find the server's IP addressed.
- 8:00Speaker 2: You can just use the terminal command ipay for that.
- 8:02Speaker 2: Then we construct the target.
- 8:03Speaker 1: URL something like http coll in slash slash the IP
- 8:07Speaker 1: address slash get underscore command dot php exactly.
- 8:11Speaker 2: And to handle the actual EGTB traffic and c sharp,
- 8:14Speaker 2: we use the system dot net namespace to create an
- 8:18Speaker 2: instance of the web client class.
- 8:20Speaker 1: Okay, and then we use the download string method to
- 8:22Speaker 1: actually send those http get requests to that URL. But
- 8:27Speaker 1: it can't just send one request and stop, right. It
- 8:30Speaker 1: needs to constantly check if the operator has a new
- 8:33Speaker 1: command waiting.
- 8:34Speaker 2: Right. It needs an active polling cycle, and to do
- 8:36Speaker 2: that you need an infinite wile loop.
- 8:38Speaker 1: But wait, if you run an infinite loop executing network
- 8:41Speaker 1: requests as fast as the processor can handle them, that's
- 8:44Speaker 1: going to be a disaster.
- 8:45Speaker 2: Oh, an absolute disaster. You will instantly cripple the local
- 8:49Speaker 2: network adapter. Your CPU will spike to one hundred percent,
- 8:52Speaker 2: and you'll generate this massive flood of traffic.
- 8:55Speaker 1: Which is going to trigger every single heuristic alert on
- 8:57Speaker 1: that subnet exactly.
- 8:59Speaker 2: So the polling cycle has to be throttled. To do that,
- 9:02Speaker 2: we import the system dot threading namespace and we use
- 9:05Speaker 2: the thread dot sleep method right inside that loop.
- 9:08Speaker 1: Okay, so that introduces a deliberate pause. If we pass
- 9:11Speaker 1: an argument of five thousand milliseconds, it forces the program
- 9:15Speaker 1: to just sleep for five seconds between every single GINTIA request.
- 9:19Speaker 2: Yeah, it creates a persistent cadence.
- 9:21Speaker 1: It's totally like a kid in the backseat of a
- 9:23Speaker 1: car asking are we there yet? Every five seconds. It's
- 9:27Speaker 1: this constant checking, but with a built in pause, so
- 9:30Speaker 1: it's not just this breathless blur of screaming requests.
- 9:33Speaker 2: That's a perfect analogy and finding that balance at the
- 9:37Speaker 2: cadence of that beacon. It's one of the most critical
- 9:40Speaker 2: variables you have to manage.
- 9:41Speaker 1: Why exactly five seconds though, Well.
- 9:43Speaker 2: Think about the extremes. If you set the sleep to say,
- 9:47Speaker 2: sixty seconds, your interactive shell becomes agonizingly slow. You type
- 9:52Speaker 2: a command and you might have to wait a full
- 9:54Speaker 2: minute to see the result. But if you drop it
- 9:56Speaker 2: to one hundred milliseconds to make it super fast, you
- 9:59Speaker 2: generate a massive amount of network noise.
- 10:01Speaker 1: Right, and network defenders are watching.
- 10:03Speaker 2: For that exactly. Endpoint detection and response tools or EDRs,
- 10:07Speaker 2: they monitor connection density. If a process suddenly fires hundreds
- 10:11Speaker 2: of requests to an external IP, it's getting quarantined instantly.
- 10:15Speaker 2: So five seconds is a tactical compromise.
- 10:17Speaker 1: It's frequent enough to keep control, but spaced out enough
- 10:20Speaker 1: to not overwhelm the network or spike the CPU.
- 10:23Speaker 2: It blends in, right, But introducing the network introduces the
- 10:27Speaker 2: biggest variable of all. What happens if that server suddenly disappears?
- 10:31Speaker 1: Oh, right, like if the c twoter infrastructure goes offline.
- 10:34Speaker 2: Yeah, the moment that webclient object tries a getcht request
- 10:38Speaker 2: and the endpoint is unreachable, it throws a network exception,
- 10:42Speaker 2: And if you haven't armored that loop, the agent crashes
- 10:44Speaker 2: on the first missed heartbeat.
- 10:46Speaker 1: So we have to apply our earlier lesson about exceptions
- 10:49Speaker 1: directly to this network loop.
- 10:50Speaker 2: Exactly. We wrap the HTTP get request and the sleep
- 10:54Speaker 2: function inside another tricatch block.
- 10:56Speaker 1: But wait, if it's an infinite loop that infinitely catches errors,
- 11:00Speaker 1: wouldn't it just keep failing forever? We need a kill switch.
- 11:03Speaker 2: We absolutely do, and the logic for that is pretty straightforward.
- 11:06Speaker 2: We create an integer variable and exception counter, and we
- 11:09Speaker 2: initialize it at zero.
- 11:11Speaker 1: Okay, So if an exception occurs, like the server is offline,
- 11:14Speaker 1: the catch block increments that counter by one. The thread
- 11:17Speaker 1: still sleeps for five seconds, and then the loop tries.
- 11:20Speaker 2: Again, right, and if the request finally succeeds in the triblock,
- 11:24Speaker 2: the counter just resets back to zero. But if the
- 11:27Speaker 2: counter gets higher than nineteen, which means twenty consecutive failures,
- 11:30Speaker 2: the script uses a brake command to exit the loop
- 11:33Speaker 2: and close the program entirely.
- 11:35Speaker 1: Okay, Wait, I need to question this logic for a second.
- 11:37Speaker 1: Let's do the math. Twenty consecutive errors at five seconds each.
- 11:42Speaker 1: That's exactly one hundred seconds correct, That's less than two
- 11:45Speaker 1: minutes of downtime. Why give up so quickly? Why not
- 11:48Speaker 1: keep trying forever? I mean, routers, reboot laptops, switch Wi
- 11:52Speaker 1: Fi networks all the time.
- 11:53Speaker 2: Because you have to avoid creating zombie processes. If the
- 11:57Speaker 2: command server is truly gone, you want that remote agent
- 12:00Speaker 2: to quietly shut down. You don't want it infinitely screaming
- 12:03Speaker 2: into the void.
- 12:04Speaker 1: Oh, because that leaves traces.
- 12:05Speaker 2: Massive traces, and infinite failing loop drains resources. It generates
- 12:10Speaker 2: event logs, fail DNS lookups, drop packets at the firewall.
- 12:13Speaker 2: If a thread hunter spot's a weird background process desperately
- 12:17Speaker 2: trying to reach a dead IP address for two days straight,
- 12:20Speaker 2: they will find it, reverse engineer it, and map your
- 12:22Speaker 2: whole infrastructure.
- 12:24Speaker 1: Wow. Okay, so stealth means knowing when to walk away.
- 12:28Speaker 1: It's better to lose the agent than to get caught exactly. So, Okay,
- 12:32Speaker 1: the code is written, it heals itself, it reaches out safely.
- 12:35Speaker 1: But step by step, how do we actually verify that
- 12:38Speaker 1: this connection is alive during deployment?
- 12:41Speaker 2: Well, you never trust that the client side code just works.
- 12:44Speaker 2: Client side validation is a trap. You have to verify
- 12:46Speaker 2: the result on the server side.
- 12:48Speaker 1: So we check the attacker server logs to ensure the
- 12:50Speaker 1: victim machine is actually connecting.
- 12:52Speaker 2: Right, you'd read the Apache service log file, which is
- 12:54Speaker 2: usually located at slash var slash log slash Apache two
- 12:58Speaker 2: slash access dot log and.
- 13:00Speaker 1: You just confirm that the get requests are logging successfully
- 13:03Speaker 1: from the victim machines.
- 13:04Speaker 2: IP. Yeah, if you see those requests hitting every five seconds,
- 13:08Speaker 2: you know it's working. And by the way, if you're
- 13:10Speaker 2: just testing this locally and you want to manually close
- 13:12Speaker 2: the application, you can just use the creed drawl C
- 13:15Speaker 2: keyboard combination in your terminal.
- 13:16Speaker 1: So what does this all mean. The ultimate proof of
- 13:20Speaker 1: a successful complex remote infiltration is literally just a quietly
- 13:25Speaker 1: updating line of text and a server's diary.
- 13:27Speaker 2: Pretty much. That's networking in a nutshell.
- 13:30Speaker 1: It's wild. Okay, we've covered a lot of ground today.
- 13:33Speaker 1: We move from brittle functions that crash over a missing
- 13:36Speaker 1: space to constructing a self healing remote agent that catches
- 13:40Speaker 1: its own errors. It anticipates lazy typing, and it safely
- 13:45Speaker 1: phones home every five seconds without turning into a noisy
- 13:48Speaker 1: zombie process.
- 13:49Speaker 2: It's all about choreographing exactly how the system behaves when
- 13:53Speaker 2: it encounters.
- 13:54Speaker 1: An error exactly. Now, to reinforce what you've learned today,
- 13:58Speaker 1: I want to talk directly to you the listener, and
- 14:00Speaker 1: try a short mental exercise.
- 14:02Speaker 2: Oh I like this.
- 14:03Speaker 1: Imagine you are adjusting this exact code. You want the
- 14:06Speaker 1: program to check in every ten seconds instead of five,
- 14:09Speaker 1: but you want it to tolerate exactly five minutes of
- 14:12Speaker 1: server downtime before the kill swip shuts it off.
- 14:14Speaker 2: Okay, so they need to adjust the sleep argument and the.
- 14:17Speaker 1: Exception counter right, how would you change the sleep duration argument?
- 14:22Speaker 1: And what number would you set for the exception counter limit.
- 14:24Speaker 1: I'll give you a second to think.
- 14:25Speaker 2: About the math. It's all about the relationship between the
- 14:27Speaker 2: thread timing and the loop iterations.
- 14:30Speaker 1: Right, Okay, here's the answer. For the sleep duration, the
- 14:33Speaker 1: argument is in milliseconds, so to get a ten second
- 14:37Speaker 1: pause you pass it ten thousand spot on.
- 14:40Speaker 2: And for the downtime constraint.
- 14:42Speaker 1: Well, five minutes is three hundred seconds. If the loop
- 14:44Speaker 1: cycle is every ten seconds, you divide three hundred by ten,
- 14:48Speaker 1: which means the agent must execute exactly thirty consecutive failed loops. Therefore,
- 14:53Speaker 1: the exception counter limit needs to be set to twenty
- 14:56Speaker 1: nine yep, so.
- 14:56Speaker 2: It triggers a brake command on that thirtieth failure. It
- 14:59Speaker 2: just shows how tightly coupled those operational variables really are.
- 15:03Speaker 1: It really does. It's a delicate balancing act. And I
- 15:06Speaker 1: want to leave you with a final thought to ponder
- 15:07Speaker 1: as we wrap up. If an educational, step by step
- 15:10Speaker 1: process can build a script this resilient to errors and
- 15:13Speaker 1: network drops using just basic name spaces and simple loop counters,
- 15:18Speaker 1: how incredibly hard do you think it is for cybersecurity
- 15:20Speaker 1: defenders to actually track down and stop advanced malicious code.
- 15:24Speaker 2: Oh it's incredibly difficult.
- 15:25Speaker 1: I mean we're talking about code that is actively designed
- 15:27Speaker 1: from the ground up to hide its own failures and
- 15:29Speaker 1: quietly weight in the dark. It's a massive challenge.
- 15:32Speaker 2: The defensive surface area is huge, and the code only
- 15:35Speaker 2: has to be right once exactly.
- 15:38Speaker 1: Well, that's all for today. Thanks for joining us on
- 15:39Speaker 1: this deep dive. Keep questioning the code and we'll catch
- 15:42Speaker 1: you next time.