Latest / CyberCode Academy / Course 15 - Write an Android Trojan from scratch | Episode 3: Building a Reverse Connection Trojan: Programmatic Netcat Execution
Transcript
- 0:00Speaker 1: We spend so much for our lives on our phones. Right,
- 0:02Speaker 1: we have all these apps running, data is flowing, and
- 0:04Speaker 1: you just sort of assume that the code inside it
- 0:07Speaker 1: app is self contained. It's meant to manage your photos
- 0:11Speaker 1: or maybe process some text, show you a map. But
- 0:13Speaker 1: what happens when that code is specifically designed to breach
- 0:18Speaker 1: its own walls and talk directly to the operating system.
- 0:20Speaker 2: And that's really the wall, right between a benign utility
- 0:25Speaker 2: and a truly powerful system tool. When an application can
- 0:28Speaker 2: programmatically step outside its own little sandbox, it can run
- 0:32Speaker 2: commands just as if you were sitting there typing into
- 0:34Speaker 2: a terminal, WHOA, the whole scope of what it can
- 0:37Speaker 2: do just it shifts entirely, and often the user would
- 0:41Speaker 2: never even notice.
- 0:42Speaker 1: Right, So, things like firewalls, the stuff we rely on
- 0:45Speaker 1: to block incoming connections, they suddenly become almost irrelevant. We
- 0:49Speaker 1: need to understand how an app can be forced to
- 0:52Speaker 1: cooperate and run these external commands.
- 0:55Speaker 2: And that's our mission for this deep dive exactly.
- 0:58Speaker 1: We're going straight into the engineering required to install and
- 1:01Speaker 1: launch an external utility, a tool like netcat on a
- 1:05Speaker 1: mobile device to set up what's known as a reverse shell.
- 1:08Speaker 2: Yeah, and we're looking at three critical programming hurdles that
- 1:13Speaker 2: have to be cleared in sequence. If you miss one,
- 1:16Speaker 2: the whole thing.
- 1:17Speaker 1: Just some re stage rocket.
- 1:19Speaker 2: That's a good way to put it. First, you have
- 1:21Speaker 2: to get the binary file the tool onto the device
- 1:24Speaker 2: and into the right spot. Second, you have to change
- 1:26Speaker 2: its permissions so the OS actually lets it run. And
- 1:29Speaker 2: then third you execute the specific command that makes the
- 1:32Speaker 2: network connection happen.
- 1:34Speaker 1: Okay, so let's start with stage one packaging, getting that
- 1:37Speaker 1: utility into a usable location. Why can't the app just
- 1:41Speaker 1: use its own internal code for this? Why bring in
- 1:43Speaker 1: an external tool?
- 1:44Speaker 2: Because you need specialized tools for specialized tasks. I mean,
- 1:48Speaker 2: netcat is the perfect utility for handling the raw transfer
- 1:52Speaker 2: of a system shell over a network socket. The appsone
- 1:55Speaker 2: code environment just isn't built for that kind of low
- 1:57Speaker 2: level network stuff. But there's a really important point here.
- 2:00Speaker 2: For mobile, the standard netcat you'd find on a Linux desktop,
- 2:05Speaker 2: it won't work out of the box.
- 2:06Speaker 1: You said it needs to be adapted for mobile. What
- 2:08Speaker 1: does that mean? Technically?
- 2:10Speaker 2: It means you need a netcat binary that's been crossed,
- 2:13Speaker 2: compiled for the device's specific architecture. For phones. That's usually ARM,
- 2:19Speaker 2: not the by eighty six you'd find on a laptop.
- 2:21Speaker 2: So this special utility, let's just call it ANG for short.
- 2:25Speaker 2: It gets packaged inside the application structure, where exactly typically
- 2:29Speaker 2: you'd place it in the assets directory before the app
- 2:32Speaker 2: is even installed.
- 2:33Speaker 1: So the assets directory is like the app's suitcase, full
- 2:37Speaker 1: of files it brings with it exactly.
- 2:38Speaker 2: It's installed along with the app. But this is the key.
- 2:42Speaker 2: It's not an executable location. So the very first thing
- 2:44Speaker 2: the code has to do is move that NC file
- 2:46Speaker 2: from the suitcase to let's say a private desk where
- 2:50Speaker 2: the app can actually.
- 2:51Speaker 1: Use it, a private ratable directory, precisely.
- 2:53Speaker 2: The directory that only that one app can access. The
- 2:55Speaker 2: path usually looks something like data data then the package
- 2:59Speaker 2: name than app files.
- 3:00Speaker 1: And you mentioned a best practice here.
- 3:02Speaker 2: Yes, the mark of really robust code is that you
- 3:05Speaker 2: don't hardcode that full path. You don't type out the
- 3:08Speaker 2: package name.
- 3:09Speaker 1: Why not? What's the risk there?
- 3:11Speaker 2: Well, what if the app's name changes, or you're trying
- 3:14Speaker 2: to build something that works across slightly different OS versions,
- 3:17Speaker 2: A hard coded path will just break instantly.
- 3:20Speaker 1: Ah, So it's brittle, very The.
- 3:22Speaker 2: Much better way is to get the package name dynamically.
- 3:25Speaker 2: At runtime, the code can just ask the system, hey,
- 3:28Speaker 2: what's my package name? Using something like this dot get
- 3:31Speaker 2: package name. Then it builds the full path string on
- 3:33Speaker 2: the fly.
- 3:34Speaker 1: That makes a lot of sense. It adapts, it ensures.
- 3:36Speaker 2: The binary lands exactly where it needs to go every
- 3:38Speaker 2: single time.
- 3:39Speaker 1: Okay, so the ink binary is now successfully copied. Where
- 3:42Speaker 1: it's stage two and we hit what you called the
- 3:44Speaker 1: permission's wall. Having the file isn't enough.
- 3:47Speaker 2: Not even close. By default, that copied file has standard
- 3:51Speaker 2: permissions maybe read and write. But the one thing it's
- 3:54Speaker 2: missing is the.
- 3:55Speaker 1: Execution bit, the permission to actually run.
- 3:58Speaker 2: YEP. If the app tries to run the file at
- 4:00Speaker 2: this point, the operating system says nope, it throws up
- 4:03Speaker 2: a big rejection flag. We need to use a system
- 4:06Speaker 2: command to explicitly flip that switch.
- 4:08Speaker 1: And this is where the classic schmode seven five to
- 4:10Speaker 1: five comes in. Let's let's break that down for anyone
- 4:12Speaker 1: who's seen it but maybe doesn't know what the numbers mean.
- 4:14Speaker 2: Sure, so each mode just means change mode. The numbers
- 4:17Speaker 2: seven fifty five. That's what sets the permissions. You read
- 4:20Speaker 2: it digit by digit.
- 4:21Speaker 1: The first digit is for the owner.
- 4:24Speaker 2: Right, the owner is the app itself. The number seven
- 4:26Speaker 2: is a sum four for read, two for write, and
- 4:29Speaker 2: one for execute. Add them up you get seven full control.
- 4:32Speaker 1: And the two fives.
- 4:34Speaker 2: The next five is for the group the file belongs to,
- 4:36Speaker 2: and the last five is for everyone else on the device.
- 4:39Speaker 2: Five means read which is four, and execute which is
- 4:42Speaker 2: one no right permission. So that one for execute is
- 4:46Speaker 2: the magic ingredient here.
- 4:47Speaker 1: So how does the app running in its high level
- 4:50Speaker 1: Android environment talk to the low level operating system to
- 4:54Speaker 1: run the schmud command.
- 4:55Speaker 2: That's where you use the runtime dot get, run time,
- 4:58Speaker 2: dot ezec method. It's basically the bridge between your app
- 5:03Speaker 2: and the underlying Linux system. But you can't just pass
- 5:06Speaker 2: the command to it as a simple string.
- 5:07Speaker 1: This is where that really specific structure comes in the
- 5:10Speaker 1: command array. I know this is easy to get wrong.
- 5:13Speaker 2: Oh absolutely, it's very meticulous. You have to build the
- 5:15Speaker 2: instruction as an array of strings. Let's call it cmd.
- 5:18Speaker 2: It needs three parts, okay, part ring. Part one has
- 5:21Speaker 2: to be the path to the device's shell interpreter system
- 5:25Speaker 2: FISH and part two the flag dash sheet. This is
- 5:28Speaker 2: so crucial. It tells the shell Hey, the very next
- 5:30Speaker 2: string I give you. That's the complete command I want
- 5:33Speaker 2: you to run.
- 5:34Speaker 1: And then the third element in the array is the
- 5:35Speaker 1: command itself, Yeah, chibud seven fifty five, followed by the
- 5:39Speaker 1: full path to our innkeat file exactly.
- 5:42Speaker 2: And that sequence is non negotiable. If you try to
- 5:44Speaker 2: mash it all into one string, it's just not going
- 5:46Speaker 2: to work. The shell won't understand it.
- 5:48Speaker 1: And speaking of things not working, this seems like a
- 5:50Speaker 1: place where a tiny mistake could cause a huge.
- 5:53Speaker 2: Headache, the absolute worst. It's the silent killer of system programming,
- 5:58Speaker 2: the missing slash, the typo, it's just it's terrible. You
- 6:02Speaker 2: hear stories of researchers spending hours debugging why their connection
- 6:06Speaker 2: isn't working, and it's because the permission's never changed due
- 6:09Speaker 2: to a typo in the path, like.
- 6:10Speaker 1: Missing the slash between data data and the package name.
- 6:13Speaker 2: Or forgetting a slash before or after app files. The
- 6:16Speaker 2: cham mode command runs but on a path that doesn't exist,
- 6:20Speaker 2: so the system doesn't find the file, the permissions don't change,
- 6:22Speaker 2: and you're left with a completely useless binary.
- 6:25Speaker 1: That attention to detail is everything. So let's say we
- 6:28Speaker 1: got the path right. Runtime dot exec runs and now
- 6:31Speaker 1: we finally have an executable netcat binary. We are ready
- 6:35Speaker 1: for stage three.
- 6:36Speaker 2: This is the payoff launching the network connection. And remember
- 6:39Speaker 2: the whole goal is a reverse shell.
- 6:42Speaker 1: So our device connects out to a listening.
- 6:43Speaker 2: Machine, exactly out to some machine controlled by an attacker,
- 6:47Speaker 2: usually on a standard port like say by five five five.
- 6:51Speaker 2: This outbound connection just siales right past most firewalls because
- 6:54Speaker 2: they're designed to block inbound traffic.
- 6:56Speaker 1: So another runtime dot exec call to get this thing
- 6:59Speaker 1: run out.
- 7:00Speaker 2: Yep, same mechanism, just a different payload. The command this
- 7:04Speaker 2: time is any than the attacker's IP address, the port
- 7:07Speaker 2: five to five, five to five, and then the most
- 7:09Speaker 2: important part ad system bish.
- 7:11Speaker 1: Okay, that little E flag. It seems so small, but
- 7:14Speaker 1: it's clearly the key. What is the significance of the
- 7:17Speaker 1: netcat execute flag?
- 7:19Speaker 2: The E flag is what turns a simple network chat
- 7:22Speaker 2: into full remote access. It tells Netcat, once you connect
- 7:26Speaker 2: to the server, take this local program I'm giving you
- 7:29Speaker 2: and pipe its input and output over the network.
- 7:31Speaker 1: And the program it's being told to execute is system bish,
- 7:36Speaker 1: the device's.
- 7:36Speaker 2: Own command line, precisely.
- 7:38Speaker 1: So the attacker sitting maybe thousands of miles away, types
- 7:41Speaker 1: a command on their.
- 7:42Speaker 2: Machine, it gets sent through the netcat.
- 7:44Speaker 1: Connection executed by the shell on the device, and.
- 7:47Speaker 2: The results the output file, listings, whatever, are piped right
- 7:50Speaker 2: back to the attackers.
- 7:51Speaker 1: Screen instant interactive remote control.
- 7:54Speaker 2: That's the pipeline. But there's one last critical programming step here.
- 7:58Speaker 2: This entire thing has to happen the background threading.
- 8:01Speaker 1: Why can't you just run this network command on the
- 8:03Speaker 1: main app thread.
- 8:04Speaker 2: Because the main thread is sacred. It's what handles the
- 8:07Speaker 2: user interface, making sure buttons work, screen scroll, keeping the
- 8:11Speaker 2: app feeling responsive.
- 8:13Speaker 1: And network connections are not responsive, not at all.
- 8:16Speaker 2: They're slow, they can hang, they're unpredictable. If you try
- 8:20Speaker 2: to run this blocking netcat command on the main thread,
- 8:23Speaker 2: the app will just freeze. The user gets that dreaded
- 8:26Speaker 2: application not responding error, and the whole operation is blown.
- 8:30Speaker 1: So for this to be silent, it has to be separate.
- 8:32Speaker 2: It has to be encapsulated in its own thread object.
- 8:35Speaker 2: You put the logic in the run method, you call
- 8:37Speaker 2: thread dot start, and that pushes the whole long running
- 8:40Speaker 2: network operation into the background. The main app looks and
- 8:44Speaker 2: feels completely normal while this reverse shell is running silently underneath.
- 8:49Speaker 1: Which brings us to pitfalls because even if you get
- 8:51Speaker 1: all three steps, write, copied, permitted, and launched in a
- 8:55Speaker 1: background thread. There's still one big picture thing that can
- 8:58Speaker 1: make it all fail silently.
- 8:59Speaker 2: Yeah, yes, this is the permission check that happens before
- 9:02Speaker 2: any of your code even runs. It's a requirement in
- 9:05Speaker 2: the applications manifest.
- 9:06Speaker 1: File, the Android manifest dot xml file.
- 9:09Speaker 2: Right, your code can be perfect, the path can be flawless,
- 9:12Speaker 2: the thread can be running. But if the app hasn't
- 9:15Speaker 2: declared its intent to use the network in the first place,
- 9:18Speaker 2: the OS will block the connection.
- 9:19Speaker 1: We're talking about the basic Internet permission.
- 9:22Speaker 2: The one and only you have to include the line
- 9:25Speaker 2: uses dash permission android dot name, android dot permission dot Internet.
- 9:31Speaker 2: If that one line is missing, it's game over. There's
- 9:34Speaker 2: just no way around it.
- 9:35Speaker 1: And I imagine someone trying to build a really persistent
- 9:37Speaker 1: tool wouldn't stop there.
- 9:39Speaker 2: Right.
- 9:40Speaker 1: What about surviving a reboot.
- 9:42Speaker 2: That's the next level of thinking, moving from just execution
- 9:45Speaker 2: to persistence. If you want to survive a reboot, you'd
- 9:48Speaker 2: need more permissions, something like uses page's pace of chairin
- 9:52Speaker 2: android dot name, android dot permission dot receive boot completed.
- 9:56Speaker 1: What is that? Let you do?
- 9:57Speaker 2: Let's your app register a piece of code that listens
- 9:59Speaker 2: for the boot completed signal from the OS, so as
- 10:02Speaker 2: soon as the phone finishes starting up, your code can
- 10:04Speaker 2: run and kick off the reverse shell process all over again.
- 10:08Speaker 1: Wow. So these small permissions when combined, become incredibly powerful.
- 10:12Speaker 2: They do, and it shows how these seemingly minor things
- 10:16Speaker 2: can become serious security holes if they're misused.
- 10:20Speaker 1: This whole process it really reframes how you think about
- 10:23Speaker 1: a security It's not just one thing, it's these three
- 10:26Speaker 1: distinct vulnerable programmatic steps.
- 10:29Speaker 2: It's a chain. Copy the binary, use runtime dot e
- 10:32Speaker 2: exept to make it executable, and then launch that persistent
- 10:35Speaker 2: background connection. Understanding that chain is fundamental. When an app
- 10:39Speaker 2: can run external commands and has Internet access, its security
- 10:42Speaker 2: really depends on just rigorously validating every single system call.
- 10:47Speaker 1: A fantastic deep dive. So before we wrap up, here's
- 10:49Speaker 1: a final thought for you to chew on. If an
- 10:52Speaker 1: application successfully executes a system command that starts a persistent
- 10:55Speaker 1: network connection, why is it so absolutely crucial from a
- 10:59Speaker 1: user experience dam point that this command runs in a
- 11:02Speaker 1: separate thread. Think about what happens when an app freezes
- 11:05Speaker 1: on you and why avoiding that is the key to
- 11:07Speaker 1: silent persistence. Thank you for joining us on this deep
- 11:10Speaker 1: dive into the engineering behind application security. We'll catch you
- 11:13Speaker 1: next time.