Latest / CyberCode Academy / Course 15 - Write an Android Trojan from scratch | Episode 2: Building the Trojan "Party App": UI Design and Netcat Preparation
Transcript
- 0:00Speaker 1: Right, let's unpack this. We often talk about mobile trojans
- 0:04Speaker 1: and these you know, sophisticated cyber threats.
- 0:07Speaker 2: And these really broad strokes, yes.
- 0:09Speaker 1: Exactly, but we rarely get to actually peek behind the
- 0:12Speaker 1: curtain and analyze the architecture.
- 0:14Speaker 2: See how it's actually put together.
- 0:15Speaker 1: Yeah, so today that's what we're doing. We're taking a
- 0:18Speaker 1: deep dive into the technical steps needed to turn a
- 0:21Speaker 1: totally innocent application. Let's just imagine a simple party app
- 0:25Speaker 1: into a powerful tool for a reverse shell connection.
- 0:29Speaker 2: And what's fascinating here is that the whole process that
- 0:32Speaker 2: starts completely normally. The question for us is really, what
- 0:36Speaker 2: are those critical steps in app has to take to
- 0:38Speaker 2: pivot from being benign to being malicious?
- 0:41Speaker 1: Right?
- 0:41Speaker 2: It's not about some you know, crazy zero day exploit. Yeah,
- 0:45Speaker 2: it's about subverting the system's own security, specifically the application
- 0:48Speaker 2: sandbox to run code it was never supposed to exactly.
- 0:52Speaker 1: So our mission here is to trace that whole development process,
- 0:55Speaker 1: from setting up the perfectly legitimate facade and Android studio
- 0:58Speaker 1: to embedding the payload and crucially writing the code to
- 1:02Speaker 1: execute that payload within the app's own protected space.
- 1:05Speaker 2: You're going to see exactly how these apps are architecturally
- 1:09Speaker 2: forced to kind of fight the system just to run
- 1:12Speaker 2: their secret code.
- 1:13Speaker 1: So we have to start where any developer would right,
- 1:15Speaker 1: just setting up the project.
- 1:17Speaker 2: Absolutely, you fired up the development environment, create a new
- 1:20Speaker 2: project and give it a name, something harmless like party app.
- 1:24Speaker 1: Why start there? It seems so basic.
- 1:26Speaker 2: Because camouflage is everything. The initial setup defines the app's
- 1:29Speaker 2: entire footprint. You specify the package name DH say com
- 1:33Speaker 2: dot cloudscraining dot party.
- 1:35Speaker 1: App, and that's this unique idea on the phone, it's.
- 1:38Speaker 2: The unique identifier the Android OS uses to create that
- 1:42Speaker 2: specific private storage location for the app. Then you just
- 1:46Speaker 2: pick the standard phone and tablet configuration, create an empty
- 1:49Speaker 2: activity and you have your skeleton.
- 1:52Speaker 1: So the first real job for the trojan is building
- 1:54Speaker 1: a convincing user interface, the perfect cover story.
- 1:57Speaker 2: One hundred percent. You use the standard layout file activity XML.
- 2:01Speaker 2: You're not trying to do anything fancy.
- 2:02Speaker 1: It just has to look legitimate, and part of that
- 2:04Speaker 1: is visuals.
- 2:05Speaker 2: Of course, you copy a nice Hires background image into
- 2:08Speaker 2: the resdrawable folder. That's I mean, that's standard practice for
- 2:11Speaker 2: any app with graphics. Okay, Then you just modify the
- 2:14Speaker 2: layout file with a single tag to use that image
- 2:17Speaker 2: and the result. The moment a user launches the app,
- 2:19Speaker 2: they see a fully formed, legitimate looking UI.
- 2:22Speaker 1: And all the while the actual engine room of the app,
- 2:26Speaker 1: the main code file, mainactivity dot Java.
- 2:28Speaker 2: It's deceptively clean. It launches the UY, but the core
- 2:32Speaker 2: logic is empty. It's just a perfect stage waiting for
- 2:36Speaker 2: the hidden architectural work we're about to add.
- 2:38Speaker 1: Okay, the stage is set. The app looks harmless. Now
- 2:41Speaker 1: we introduce.
- 2:42Speaker 2: The weapon, right, and in this case, it's a specialized
- 2:45Speaker 2: network utility called netcab, which.
- 2:47Speaker 1: People often call the the Swiss Army knife for networks.
- 2:50Speaker 2: Right, the one. It's a superversaal tool that can read
- 2:53Speaker 2: and write data across networks and for an attack, it's
- 2:57Speaker 2: perfect for establishing a reverse shell connection.
- 3:00Speaker 1: So instead of the attacker connecting into the phone, the.
- 3:02Speaker 2: Phone connects out to the attacker, which bypasses most firewall rules.
- 3:07Speaker 1: And you can't just use any version of that.
- 3:08Speaker 2: Cat, no, not at all. Modern phones run on specific hardware,
- 3:12Speaker 2: usually arm architecture, so this binary has to be compiled
- 3:16Speaker 2: specifically for that mobile platform. Otherwise the system just won't
- 3:21Speaker 2: know what to do with it.
- 3:21Speaker 1: Once you have that special binary, how do you get
- 3:24Speaker 1: it into the app package, you have to.
- 3:26Speaker 2: Put it in a very specific place, a non compiled
- 3:28Speaker 2: resource folder called the assets folder.
- 3:31Speaker 1: Think of it like the app suitcase.
- 3:33Speaker 2: That's a great way to put it. It holds raw
- 3:35Speaker 2: data the app might need later config files, databases, or
- 3:39Speaker 2: in our case, a hidden executable. It travels with the app,
- 3:43Speaker 2: but it's not yet in a place where it can
- 3:45Speaker 2: actually be launched.
- 3:46Speaker 1: And that separation brings us to the most fundamental security boundary.
- 3:49Speaker 1: We need to talk.
- 3:49Speaker 2: About the application sandbox, right.
- 3:51Speaker 1: This is everything when it comes to mobile security. Android
- 3:55Speaker 1: has this very strict model where apps are only allowed
- 3:58Speaker 1: to execute files that live in their own designated protected
- 4:02Speaker 1: private storage area.
- 4:03Speaker 2: That zone is usually found way down in the file
- 4:06Speaker 2: system at data data followed by the package name. It's
- 4:09Speaker 2: totally private to that one app.
- 4:11Speaker 1: So private that if you even try to access it
- 4:14Speaker 1: from the outside with a command line tool like ADB.
- 4:17Speaker 2: You're immediately hit with permission denied, even if you own
- 4:19Speaker 2: the device. It's a highly restricted zone, a locked box.
- 4:23Speaker 1: So the file is embedded in the app suitcase, but
- 4:26Speaker 1: it's physically stuck outside the execution zone the sandbox directory.
- 4:30Speaker 2: That is the architectural dilemma, and it's a dilemma the
- 4:34Speaker 2: trojan has to solve with a very precise, multi step operation.
- 4:38Speaker 1: So this whole problem requires a plan, a blueprint for
- 4:42Speaker 1: the attack.
- 4:42Speaker 2: A deliberate three step plan. Yeah, and every single step
- 4:46Speaker 2: has to be choreographed perfectly for it to work.
- 4:48Speaker 1: Okay, let's walk through it. What's step one?
- 4:50Speaker 2: Step one is to copy? The application must programmatically copy
- 4:55Speaker 2: the netcat file it's just call it nec from its starting.
- 4:58Speaker 1: Place, from the suitcase the assets into.
- 5:00Speaker 2: Its private sandbox data directory on the device. It has
- 5:04Speaker 2: to physically move the weapon into its own house.
- 5:06Speaker 1: Okay, that makes sense. Then what step two?
- 5:08Speaker 2: Step two permissions? Once that file is copied, it just
- 5:11Speaker 2: arrives as raw data. It doesn't have any.
- 5:14Speaker 1: Execution rights right because of the underlying Linux system.
- 5:17Speaker 2: Exactly, So the application has to programmatically change the file's
- 5:21Speaker 2: permissions to make it executable. If this step fails, the
- 5:24Speaker 2: file is just inert data and step three is impossible.
- 5:28Speaker 1: And step three is the endgame.
- 5:30Speaker 2: Step three is the endgame. Get the reverse shell. Once
- 5:34Speaker 2: the file is in the right place with the right permissions,
- 5:37Speaker 2: the app executes the binary, it launches and c and
- 5:41Speaker 2: that establishes the secret reverse shell connection back to the attackers.
- 5:45Speaker 1: Machine a dedicated tunnel opened by the phone itself.
- 5:48Speaker 2: And all of this logic has to fire instantly the
- 5:51Speaker 2: moment the app opens, right inside the main on creat method.
- 5:54Speaker 1: All right, so this is where the programming logic it's
- 5:56Speaker 1: really compelling. Inside that on creat method, we're placing three
- 5:59Speaker 1: functions in sequence.
- 6:00Speaker 2: YEP copy file, change permissions, and get reverse shell. We're
- 6:04Speaker 2: going to focus first on implementing copy file because it's
- 6:07Speaker 2: way more complex than it sounds.
- 6:08Speaker 1: It's not just a simple move command, no.
- 6:10Speaker 2: Not at all. You're not dealing with an unzipped folder.
- 6:13Speaker 2: You're extracting raw data from the compressed app package and
- 6:16Speaker 2: writing it to the filesystem. That requires using specific Android
- 6:20Speaker 2: architecture input and output streams.
- 6:23Speaker 1: So the first job is just getting access to the
- 6:25Speaker 1: netcut file in the assets.
- 6:26Speaker 2: Right. The app has to use the operating system's asset
- 6:29Speaker 2: manager to open a channel to the file. We call
- 6:32Speaker 2: it the input stream.
- 6:33Speaker 1: And there's a huge cat chair, right, a critical one.
- 6:36Speaker 2: Any file operation can fail, maybe the app installed wrong,
- 6:40Speaker 2: maybe the file's missing. If the code just try to
- 6:42Speaker 2: open it without any safeguards, the app would crash instantly.
- 6:46Speaker 1: Which is a dead giveaway that something is wrong, a
- 6:48Speaker 1: huge giveaway.
- 6:50Speaker 2: This is why the entire operation has to be wrapped
- 6:52Speaker 2: in what's called a tricatch block. Yeah, it's a mechanism
- 6:55Speaker 2: that basically says, try to do this, but something goes wrong,
- 6:59Speaker 2: don't crash. Handle the exception gracefully. The trojan has to
- 7:03Speaker 2: be robust.
- 7:04Speaker 1: A safety net for the malware.
- 7:06Speaker 2: A safety net exactly. So, once the source is ready,
- 7:09Speaker 2: we define the destination. The app uses special functions to
- 7:12Speaker 2: dynamically create a new private folder. Say app files right
- 7:16Speaker 2: inside its protected sandbox.
- 7:18Speaker 1: And it has to be dynamic because you don't know
- 7:19Speaker 1: the exact path ahead of time.
- 7:21Speaker 2: You don't the path changes based on the package name.
- 7:24Speaker 2: So the code figures out its own address at runtime,
- 7:27Speaker 2: builds the whole destination path, and then points the destination
- 7:30Speaker 2: channel the output stream right where the file needs to land.
- 7:33Speaker 1: So we have the input stream and the output stream.
- 7:36Speaker 1: How does the actual copy happen.
- 7:38Speaker 2: It happens in small chunks using a buffer. A buffer
- 7:41Speaker 2: is just a small temporary piece of memory. Think of
- 7:43Speaker 2: it like a small bucket. Okay, the code sets up
- 7:46Speaker 2: a loop. It reads a small chunk of data from
- 7:48Speaker 2: the input stream into the bucket, scoops it up, and
- 7:50Speaker 2: immediately writes or dumps that bucket's contents into the output stream.
- 7:56Speaker 2: It repeats that cycle read write, read write hundreds or
- 7:59Speaker 2: thousands of times until the entire file is transferred efficiently,
- 8:03Speaker 2: and when it's done, as a final act of ironically
- 8:07Speaker 2: good programming, it cleans up after itself. It closes both
- 8:11Speaker 2: streams and sets them to NOLL. Step one is complete.
- 8:14Speaker 2: The trojan has successfully untacked its weapon into its own
- 8:17Speaker 2: private armory.
- 8:18Speaker 1: That brings us to step two, which seems simple but
- 8:21Speaker 1: is architecturally mandatory. The binary is now sitting in the
- 8:24Speaker 1: private folder. Why can't the app just run it?
- 8:27Speaker 2: This is where we pay tribute to Android's heritage as
- 8:30Speaker 2: a Linux based operating system. When a neckcat file was copied,
- 8:35Speaker 2: it was just treated as data as sequence of bytes.
- 8:38Speaker 1: By default, it has no permission to execute none.
- 8:42Speaker 2: You might have read and wright access inside your own folder,
- 8:45Speaker 2: but the operating system still will not let you run
- 8:47Speaker 2: the file unless the execution bit is flipped.
- 8:49Speaker 1: So if the app tried to launch it now, the
- 8:52Speaker 1: system would just throw a permission denied error.
- 8:55Speaker 2: It would not because the app is in the wrong place,
- 8:57Speaker 2: but because the file itself lacks the right app tribute.
- 9:00Speaker 2: It's like having a car in your garage but no
- 9:03Speaker 2: key to start it.
- 9:04Speaker 1: So how does it get the key?
- 9:05Speaker 2: The solution is the programmatic equivalent of running the Linux
- 9:08Speaker 2: command schmod plus x and C. The Java code does
- 9:12Speaker 2: this using the file class and just explicitly setting the
- 9:15Speaker 2: executable permission flag to true.
- 9:17Speaker 1: It's using its own authority within its sandbox to grant
- 9:20Speaker 1: execution rights to the file.
- 9:22Speaker 2: And this is the critical pivot point. The application is
- 9:25Speaker 2: leveraging a perfectly legitimate function managing its own file permissions
- 9:30Speaker 2: to enable a completely illegitimate action. Once that permission is set,
- 9:35Speaker 2: the trojan has everything it needs, and.
- 9:37Speaker 1: That transitions us to the final step step three execution.
- 9:41Speaker 1: The file is in place, it has the rights. How
- 9:44Speaker 1: does the application actually launch a system command from its
- 9:48Speaker 1: Java code?
- 9:49Speaker 2: The standard way to do this from an Android app
- 9:51Speaker 2: is using the runtime dot get runtime dot eas method.
- 9:55Speaker 2: This function lets the job application talk to the underlying
- 9:58Speaker 2: operating system and issue a co.
- 10:00Speaker 1: You're basically asking the phone, hey, run this file for me.
- 10:03Speaker 1: What does that actual command look like?
- 10:04Speaker 2: It's very precise, so they'll typically be something like nce system
- 10:07Speaker 2: ish followed by the attacker's IP import.
- 10:09Speaker 1: Okay, let's break that down for you. NC is our
- 10:12Speaker 1: netcat binary. That is flag. What does that do?
- 10:14Speaker 2: The E flag taels netcat to execute a program after.
- 10:17Speaker 1: It connects, and the program it's told to execute is
- 10:19Speaker 1: system bindish. That's the system shell right the command line exactly.
- 10:24Speaker 2: That one command effectively links the device's command line interface.
- 10:29Speaker 2: The shell directly to the network connection that Netcat just made.
- 10:33Speaker 2: The attacker listing on their IP and port suddenly receives
- 10:36Speaker 2: a live interactive command prompt for the victim's device.
- 10:40Speaker 1: And all of this happens instantaneously. When the user opens
- 10:43Speaker 1: the party.
- 10:43Speaker 2: App, they just see the themed UI, But behind the scenes,
- 10:47Speaker 2: within that sandbox, the app is copied, permitted, and executed
- 10:51Speaker 2: a reverse shell, giving the attack a remote access within seconds.
- 10:55Speaker 2: That is the complete architectural journey of deception.
- 10:58Speaker 1: So we have successfully tried that entire path of construction
- 11:02Speaker 1: from the project setup and that innocuous UI all the way.
- 11:05Speaker 2: To the sophisticated three part programming logic that moves and
- 11:08Speaker 2: executes a hostile payload inside the app's secure sandbox.
- 11:12Speaker 1: This deep dive really highlights that mobile security is often
- 11:15Speaker 1: less about breaking the fence and more about exploiting the
- 11:17Speaker 1: gates the system must leave open.
- 11:20Speaker 2: Right, the ability of an app to self manage its resources,
- 11:23Speaker 2: to copy files, to set its own permissions. That's all
- 11:25Speaker 2: necessary for legitimate functions. But that necessary capability is what
- 11:30Speaker 2: gets repurposed here to manage a malicious binary.
- 11:33Speaker 1: And that leads us to a final provocative thought for
- 11:35Speaker 1: you to consider. We spend a lot of time discussing
- 11:37Speaker 1: the execution of that netcab binary using run time dot exec.
- 11:43Speaker 1: If the application is successfully sandboxed, meaning it can only
- 11:46Speaker 1: access its own private data, why is it still allowed
- 11:49Speaker 1: to execute a command to connect out to a remote
- 11:52Speaker 1: IP address on the Internet. What critical manifest permission one
- 11:55Speaker 1: that's easily visible to you? The user must the trojan
- 11:59Speaker 1: request to me extep three even possible.
- 12:01Speaker 2: Something to mull over the next time you see that
- 12:03Speaker 2: permission's list before you hit except