Latest / CyberCode Academy / Course 15 - Write an Android Trojan from scratch | Episode 4: Implementing an Android Reverse Shell using Java Native APIs (without Netcat)
Transcript
- 0:00Speaker 1: When developers or security engineers build tools for remote access,
- 0:04Speaker 1: the goal is always the same, get in, stay in,
- 0:08Speaker 1: and don't get caught. We're talking about stealth, and often
- 0:12Speaker 1: the easiest path is just to bundle a non utility
- 0:14Speaker 1: something like netcat right into your application. But here's the problem.
- 0:17Speaker 2: It's a huge red flag, a huge red flag.
- 0:20Speaker 1: Modern security tools are well, they're really good at spotting
- 0:23Speaker 1: those known files. They're like these big loud digital cargo.
- 0:27Speaker 2: Truck exactly, they have a signature.
- 0:29Speaker 1: Okay, so let's unpack this. What if you could get
- 0:32Speaker 1: that same power, that same remote access what we call
- 0:34Speaker 1: a reverse shell, but build it using only the the
- 0:38Speaker 1: core features of the operating system itself. For Android, that
- 0:42Speaker 1: means pure Java, no external files.
- 0:45Speaker 2: And that's the core of what we're talking about today.
- 0:47Speaker 2: In this deep dive, we're going to get into the
- 0:49Speaker 2: mechanics of creating one of these reverse connections on an
- 0:51Speaker 2: Android device using nothing but Java's native APIs, no dependencies,
- 0:56Speaker 2: zero dependencies. The whole mission here is to give you
- 1:00Speaker 2: those aha moments to really understand how seemingly innocent language
- 1:05Speaker 2: features can be strung together to create a powerful, and
- 1:08Speaker 2: more importantly, a very stealthy tool.
- 1:11Speaker 1: So we're going to break down the entire process.
- 1:13Speaker 2: The whole assembly line. How you established the connection, how
- 1:16Speaker 2: you create a loop to constantly listen for commands, how
- 1:20Speaker 2: you execute them on the device, and of course, how
- 1:23Speaker 2: you secretly stream those results back out. You have to
- 1:26Speaker 2: know how these things are built to have any chance
- 1:28Speaker 2: of defending against them.
- 1:29Speaker 1: Let's start with the basics. If you're going to run
- 1:32Speaker 1: something that needs to be constantly connected, constantly waiting for
- 1:35Speaker 1: network input, you can't just run it on the application's
- 1:38Speaker 1: main thread.
- 1:39Speaker 2: No, you'd freeze the entire user interface. The second he
- 1:42Speaker 2: waits for a command, the whole app just locks up.
- 1:44Speaker 2: It's a dead giveaway.
- 1:45Speaker 1: So how do you get this thing running in the
- 1:47Speaker 1: background without crippling the app?
- 1:49Speaker 2: The answer is a fundamental concept in programming threading. You're
- 1:53Speaker 2: basically hiring a dedicated assistant to handle this one unstable
- 1:58Speaker 2: and resource intensive job, a separate worker, a completely separate worker.
- 2:03Speaker 2: You implement the entire logic inside a new method. Let's
- 2:07Speaker 2: call it get job reverse shell for our purposes, and
- 2:10Speaker 2: that method's only job is to spawn a brand new thread. Okay,
- 2:14Speaker 2: all the important stuff, the networking, the command execution. It
- 2:18Speaker 2: all lives inside the public void run method of this
- 2:21Speaker 2: new thread. You kick it off with thread dot start
- 2:24Speaker 2: and that's it.
- 2:25Speaker 1: It's isolated, and that isolation is key. If the connection
- 2:28Speaker 1: drops or something goes wrong, the.
- 2:30Speaker 2: Main app doesn't care. It just keeps on running smoothly.
- 2:33Speaker 2: The user sees nothing, and the app itself is more stable,
- 2:36Speaker 2: which ironically makes the malicious part even stealthier.
- 2:40Speaker 1: It's compartmentalization. So we've got our background worker ready to go.
- 2:43Speaker 1: Now it needs to know where to call and needs
- 2:45Speaker 1: coordinates exactly.
- 2:46Speaker 2: You need two simple things. First, a string for the
- 2:48Speaker 2: server IP so you know the attacker's listening address. A
- 2:51Speaker 2: common example is something like one ninety two point one
- 2:54Speaker 2: sixty eight point one point one three at a port number,
- 2:57Speaker 2: and an integer for the port, say five, five to five.
- 3:00Speaker 2: Those two variables to find the exact destination the device
- 3:03Speaker 2: needs to connect to.
- 3:04Speaker 1: Now you're dealing with networking, which means things are guaranteed
- 3:07Speaker 1: to fail at some point.
- 3:08Speaker 2: It's not a possibility, it's a certainty, so you have
- 3:11Speaker 2: to account for it from the very beginning.
- 3:13Speaker 1: Which means a tricatch block.
- 3:14Speaker 2: A massive one. You basically wrap almost the entire connection
- 3:18Speaker 2: and execution logic inside a tricatch block, the server might
- 3:22Speaker 2: not be listening, the Wi Fi could drop. Any number
- 3:25Speaker 2: of things can go wrong, and without that block, the
- 3:27Speaker 2: whole app just crashes. That throws an exception and dies.
- 3:30Speaker 2: That's a huge alert to the user that something is
- 3:33Speaker 2: fundamentally broken. So we use exception E and E dot
- 3:36Speaker 2: print stack trace to well to quietly log the error
- 3:40Speaker 2: and just try again later, keeping the app alive.
- 3:43Speaker 1: Okay, so let's get to the actual connection. Building the socket.
- 3:46Speaker 1: You can't just throw that IP address string into the
- 3:48Speaker 1: socket call, can you.
- 3:50Speaker 2: No, Jama needs it in a specific format. You first
- 3:52Speaker 2: have to use a net address dot get by name
- 3:55Speaker 2: and pass it your server IP string.
- 3:57Speaker 1: And what does that do?
- 3:59Speaker 2: It translates that and readable address into a network ready object,
- 4:03Speaker 2: a net address object, which maybe we store in a
- 4:05Speaker 2: variable called host. It's like turning a street address into
- 4:08Speaker 2: GPS coordinates that the network can actually navigate with.
- 4:11Speaker 1: Got it. So once you have those coordinates.
- 4:13Speaker 2: Then comes the moment of truth. You create the socket
- 4:17Speaker 2: object itself, passing in that host object and the numerical
- 4:21Speaker 2: port YEA. That single line of code is what forces
- 4:24Speaker 2: the Android device to reach out and try to connect
- 4:27Speaker 2: to the attacker's machine.
- 4:28Speaker 1: And that's the reverse part of the reverse show.
- 4:30Speaker 2: That is the reverse part. The client connects out to
- 4:32Speaker 2: the server, not the other way around.
- 4:34Speaker 1: So the connections made. The goal now is to keep
- 4:36Speaker 1: it alive. One command is useless. You need to maintain control.
- 4:40Speaker 2: And for that you use a simple but really critical structure,
- 4:44Speaker 2: a wild true loop. As long as that socket connection
- 4:46Speaker 2: is open, the code just runs inside this infinite loop.
- 4:50Speaker 2: It's constantly waiting, ready to receive the next command.
- 4:53Speaker 1: Okay, so we have this persistent pipe, but now we
- 4:56Speaker 1: have to manage traffic going both ways through it. The
- 4:59Speaker 1: attackers commands in and the device sends results out. How
- 5:03Speaker 1: do you handle that two way flow.
- 5:05Speaker 2: This is where it gets a little more complex. We
- 5:07Speaker 2: need to set up what are called input output or
- 5:09Speaker 2: IO objects. We declared two high level ones, a print
- 5:13Speaker 2: writer called OUT and a buffer reader called.
- 5:15Speaker 1: IN, and the names pretty much say it all they do.
- 5:18Speaker 2: Print writer out is for sending data out to the
- 5:20Speaker 2: attacker the results of the commands, and buffer reader IN
- 5:25Speaker 2: is for reading the data coming in that commands themselves.
- 5:28Speaker 1: But setting these up, especially the outstream, it's not just
- 5:31Speaker 1: one object. It's a whole chain of them, isn't it.
- 5:33Speaker 1: Why is it so complicated?
- 5:35Speaker 2: The complexity is really about translation. The socket itself just
- 5:38Speaker 2: speaks in raw bites, but we want to work with
- 5:41Speaker 2: clean lines of texts, so you have to build a
- 5:43Speaker 2: little assembly line of translators. I see. So for the output,
- 5:46Speaker 2: your print writer is the final step, but it wraps
- 5:49Speaker 2: a buffer writer, which itself wraps an output stream writer,
- 5:52Speaker 2: and that output stream writer is what's attached directly to
- 5:55Speaker 2: the socket's raw output stream.
- 5:57Speaker 1: So each layer has a job.
- 5:58Speaker 2: Each layer has a specific The output stream rider turns
- 6:02Speaker 2: Java characters into bytes. The buffer writer makes that process efficient,
- 6:06Speaker 2: and the print writer gives us helpful metals like printing
- 6:09Speaker 2: to send data line by line.
- 6:11Speaker 1: And there's a fascinating little detail in that chain. You
- 6:14Speaker 1: have to set auto flush to true. That seems minor,
- 6:17Speaker 1: but it's vital, isn't.
- 6:18Speaker 2: It, Oh, absolutely vital. If you don't, the buffer will
- 6:22Speaker 2: just collect data and wait until it's full before sending anything.
- 6:25Speaker 2: The attacker would type of command and see nothing maybe
- 6:27Speaker 2: for seconds. With autoflush said to true, the moment you
- 6:30Speaker 2: call print lane, that data is forced across the network immediately.
- 6:34Speaker 1: Instant feedback. It keeps the connection feeling like a real
- 6:37Speaker 1: local terminal.
- 6:38Speaker 2: It creates that illusion of interactivity.
- 6:39Speaker 1: Yes, and the input side for the commands coming in,
- 6:42Speaker 1: it's a similar chain structure it is.
- 6:44Speaker 2: Yeah, that's simpler. The in object is a buffer reader
- 6:48Speaker 2: that wraps an input stream reader which is attached to
- 6:51Speaker 2: the socket's input stream. Once you're inside that wild loop,
- 6:55Speaker 2: your code just pauses on one critical line in that
- 6:58Speaker 2: read line, it just wastes waits. Execution halts right there
- 7:02Speaker 2: until a full line of tech a command arise from
- 7:04Speaker 2: the attacker. Then it reads, it stores in our command variable,
- 7:08Speaker 2: and the loop continues.
- 7:09Speaker 1: And now we're at the real heart of it. The
- 7:11Speaker 1: command is received, it has to be executed on the
- 7:14Speaker 1: Android device. This is where you use that powerful Java
- 7:18Speaker 1: call that every security team is watching for runtime dot
- 7:22Speaker 1: gren time, dot egencyc.
- 7:24Speaker 2: That's the one. This single native call is the gateway.
- 7:27Speaker 2: It's what turns this communication tool into a full compromise.
- 7:30Speaker 1: Because you're not just running app code anymore.
- 7:32Speaker 2: You're running actual system shell commands, things like elemationals to
- 7:37Speaker 2: list files or cat to read them. And because of that,
- 7:41Speaker 2: the exec method needs the command in a very specific format.
- 7:45Speaker 2: Usually a string array.
- 7:46Speaker 1: What goes in that array.
- 7:48Speaker 2: Typically three things. The pass to the system shell itself,
- 7:52Speaker 2: so SI send a mish, then the execute flag which
- 7:55Speaker 2: is nancy see, and finally the actual command string that
- 7:58Speaker 2: we just read from the.
- 7:59Speaker 1: Network that launches the command.
- 8:01Speaker 2: It does, and in return, Java gives you a process object.
- 8:05Speaker 2: You can think of this object as a handle to
- 8:08Speaker 2: that running command and crucially to its output.
- 8:10Speaker 1: Right. Because the result doesn't just automatically fly back across
- 8:13Speaker 1: the socket, it's trapped in a way inside that process object.
- 8:17Speaker 1: You have to go in and get it.
- 8:18Speaker 2: You have to manually pull it out, and that means
- 8:20Speaker 2: setting up another temporary iosystem just to read the command's output.
- 8:24Speaker 1: So another buffer reader.
- 8:26Speaker 2: Another buffered reader. Yeah, this one is tied specifically to
- 8:30Speaker 2: the process's input stream. So process dot get input stream. Now,
- 8:35Speaker 2: the output could be one word, or it could be thousands.
- 8:37Speaker 1: Of lines, so you can't just read it all at once.
- 8:39Speaker 2: You can't, so you need an efficient way to collect it.
- 8:42Speaker 2: We usually declare a small temporary char buffer maybe forty
- 8:46Speaker 2: ninety six bytes, to act as a scoop, and then
- 8:49Speaker 2: we have a larger string buffer let's just call it
- 8:51Speaker 2: output to assemble everything in and.
- 8:53Speaker 1: You just keep scooping until it's all collected exactly.
- 8:56Speaker 2: Use another wild loop that just reads chunks of data
- 9:00Speaker 2: into that small character buffer for every scoop it gets,
- 9:03Speaker 2: it uses output dot a pen to add it to
- 9:06Speaker 2: the big string buffer. That loop runs until the stream
- 9:10Speaker 2: from the process is completely empty.
- 9:12Speaker 1: Which guarantees you've captured every single.
- 9:14Speaker 2: Character of the output, every last character.
- 9:16Speaker 1: So now you've got this string buffer with the full result,
- 9:19Speaker 1: but you need it as a clean single message to
- 9:21Speaker 1: send back.
- 9:22Speaker 2: Right, that's the final preparation step. You just call the
- 9:24Speaker 2: dot two string method on that string buffer, and that
- 9:26Speaker 2: gives you a clean single string that we can call
- 9:30Speaker 2: command output Ready for transmission.
- 9:31Speaker 1: Okay, the final step. We've received the command executed, it
- 9:36Speaker 1: collected the output. How do we send this little package
- 9:39Speaker 1: back across the wire.
- 9:41Speaker 2: We go back to that print writer object, the one
- 9:42Speaker 2: we called out that we set up earlier. We just
- 9:44Speaker 2: do a quick check to make sure it's still valid,
- 9:46Speaker 2: and then we use out dot printal in ellen and
- 9:49Speaker 2: pass it our command output string.
- 9:50Speaker 1: And then the flush and then.
- 9:52Speaker 2: The all important out dot flush. That's the command that
- 9:54Speaker 2: forces all that collected output immediately across the network. Back
- 9:58Speaker 2: to the attacker. It completes the cycle, and.
- 10:01Speaker 1: The verdict here is, well, it's pretty powerful. We've just
- 10:05Speaker 1: walked through building a fully functional, persistent reverse shell on Android.
- 10:10Speaker 2: Without a single external binary right.
- 10:13Speaker 1: No netcat, nothing that a traditional signature based scanner would
- 10:17Speaker 1: ever find. It's all just native Java APIs.
- 10:19Speaker 2: And that's a huge takeaway for anyone in security. We've
- 10:22Speaker 2: seen how these internal foundational APIs can be perfectly orchestrated
- 10:26Speaker 2: to be incredibly stealthy, which raises a really important question
- 10:30Speaker 2: for you, for everyone listening. If the attack isn't using
- 10:33Speaker 2: known malicious files, but it is instead weaponizing the language itself,
- 10:38Speaker 2: how do you defend against it? What are the mechanisms
- 10:41Speaker 2: that can monitor internal API calls like that runtime dot
- 10:45Speaker 2: getruntime dot exact call.
- 10:47Speaker 1: You're saying, the focus has to shift.
- 10:49Speaker 2: It has to the defensive mindset has to move from
- 10:51Speaker 2: looking for bad files to looking for bad behavior watching
- 10:54Speaker 2: what an application is actually doing with the APIs it's given.
- 10:58Speaker 2: That's really the next frontier, something powerful to think.
- 11:00Speaker 1: About, an excellent place to leave it. We've reached the
- 11:03Speaker 1: end of this deep dive. Thank you for joining us
- 11:05Speaker 1: as we unpacked the code behind the curtain