fixed issues with CA now generates its own new cert
All checks were successful
release / build-and-upload (push) Successful in 16m53s
All checks were successful
release / build-and-upload (push) Successful in 16m53s
This commit is contained in:
parent
a930a697fa
commit
c1948a1439
11 changed files with 458 additions and 34 deletions
|
|
@ -101,11 +101,37 @@ func Get(name string) (App, error) {
|
|||
ctor, ok := registry[name]
|
||||
mu.RUnlock()
|
||||
if !ok {
|
||||
if owner, ch, ok := FindChannel(name); ok {
|
||||
return nil, fmt.Errorf("%q is a channel of --app %s, not an app name — use: capture --app %s --channel %s", name, owner, owner, ch)
|
||||
}
|
||||
return nil, fmt.Errorf("unknown app %q — compiled-in apps: %v", name, Names())
|
||||
}
|
||||
return ctor()
|
||||
}
|
||||
|
||||
// FindChannel reports which registered app owns a channel id (via HasChannel).
|
||||
// Does not call Get's unknown-app path (constructs from the registry directly).
|
||||
func FindChannel(id string) (appName, channelID string, ok bool) {
|
||||
mu.RLock()
|
||||
names := make([]string, 0, len(registry))
|
||||
ctors := make([]func() (App, error), 0, len(registry))
|
||||
for n, ctor := range registry {
|
||||
names = append(names, n)
|
||||
ctors = append(ctors, ctor)
|
||||
}
|
||||
mu.RUnlock()
|
||||
for i, ctor := range ctors {
|
||||
a, err := ctor()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if a.HasChannel(id) {
|
||||
return names[i], id, true
|
||||
}
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
// Names lists registered app ids, sorted.
|
||||
func Names() []string {
|
||||
mu.RLock()
|
||||
|
|
|
|||
|
|
@ -76,6 +76,11 @@ func RunPassive(opt Options) (res Result, err error) {
|
|||
}
|
||||
|
||||
proxyBin, caHash := resolveProxy(nil, root)
|
||||
if h, err := proxy.InstallCA(c, "", false); err != nil {
|
||||
return res, fmt.Errorf("CA: %w", err)
|
||||
} else if h != "" {
|
||||
caHash = h
|
||||
}
|
||||
if err := proxy.PushAndStart(c, proxyBin); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
|
@ -201,32 +206,37 @@ func Run(opt Options) (res Result, err error) {
|
|||
|
||||
if useTProxy {
|
||||
// Transparent REDIRECT — keeps phone VPN (BBC UK geo) working.
|
||||
// StartTransparent creates a per-install CA under data/ if needed and pushes cert+key.
|
||||
proxy.ClearHTTPProxy(c)
|
||||
remoteDir := "/data/local/tmp/capture/" + a.Package()
|
||||
if err := proxy.StartTransparent(c, proxyBin, a.Package(), "8080", remoteDir, false); err != nil {
|
||||
return res, err
|
||||
return res, fmt.Errorf("transparent MITM start: %w", err)
|
||||
}
|
||||
defer proxy.StopTransparent(c)
|
||||
hints.RemoteCaps = []string{remoteDir + "/cap.json"}
|
||||
hints.RemoteLog = remoteDir + "/appproxy.log"
|
||||
fmt.Println("[*] Transparent MITM (VPN OK) — package", a.Package())
|
||||
} else {
|
||||
if h, err := proxy.InstallCA(c, "", false); err != nil {
|
||||
return res, fmt.Errorf("CA: %w", err)
|
||||
} else if h != "" {
|
||||
caHash = h
|
||||
}
|
||||
if err := proxy.PushAndStart(c, proxyBin, hints.Score.ProxyArgs()...); err != nil {
|
||||
return res, err
|
||||
return res, fmt.Errorf("proxy start: %w", err)
|
||||
}
|
||||
if err := proxy.EnsureHTTPProxy(c, ""); err != nil {
|
||||
return res, err
|
||||
return res, fmt.Errorf("http_proxy: %w", err)
|
||||
}
|
||||
proxy.ReinjectCA(c, caHash)
|
||||
}
|
||||
|
||||
if err := a.Launch(c); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[!] launch: %v\n", err)
|
||||
return res, fmt.Errorf("launch: %w", err)
|
||||
}
|
||||
if !opt.NoAutoPlay {
|
||||
if err := a.AutoPlay(c, opt.Channel); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[!] auto-play failed: %v\n", err)
|
||||
fmt.Println("[*] Open the channel on the phone manually and start playback…")
|
||||
return res, fmt.Errorf("auto-play: %w", err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("[*] Open the channel on the phone and wait for playback…")
|
||||
|
|
|
|||
61
apps/pkg/proxy/ca_test.go
Normal file
61
apps/pkg/proxy/ca_test.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnsureLocalCAGeneratesUniquePair(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cert1, err := EnsureLocalCA(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := filepath.Join(root, "data", "proxy-ca.crt")
|
||||
if cert1 != want {
|
||||
t.Fatalf("cert path: got %q want %q", cert1, want)
|
||||
}
|
||||
key1 := filepath.Join(root, "data", "proxy-ca.key")
|
||||
if _, err := os.Stat(key1); err != nil {
|
||||
t.Fatal("missing key", err)
|
||||
}
|
||||
hash1, err := AndroidCAHashFile(cert1)
|
||||
if err != nil || hash1 == "" {
|
||||
t.Fatalf("hash: %q %v", hash1, err)
|
||||
}
|
||||
|
||||
// Second call reuses the same pair.
|
||||
cert2, err := EnsureLocalCA(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cert2 != cert1 {
|
||||
t.Fatalf("expected reuse, got %q vs %q", cert2, cert1)
|
||||
}
|
||||
|
||||
// A different install root gets a different CA.
|
||||
rootB := t.TempDir()
|
||||
certB, err := EnsureLocalCA(rootB)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hashB, _ := AndroidCAHashFile(certB)
|
||||
if hashB == hash1 {
|
||||
t.Fatal("expected different installs to get different CA hashes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindLocalCAIgnoresCertWithoutKey(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
dir := filepath.Join(root, "apps", "proxy")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "rteproxy-ca.crt"), []byte("not-a-real-cert"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p := FindLocalCA(root); p != "" {
|
||||
t.Fatalf("expected empty without key, got %q", p)
|
||||
}
|
||||
}
|
||||
|
|
@ -2,10 +2,14 @@ package proxy
|
|||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/binary"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
|
@ -24,6 +28,7 @@ const (
|
|||
RemoteLog = "/data/local/tmp/appproxy.log"
|
||||
RemoteTraffic = "/data/local/tmp/appproxy_traffic.jsonl"
|
||||
RemoteCACrt = "/data/local/tmp/rteproxy-ca.crt"
|
||||
RemoteCAKey = "/data/local/tmp/rteproxy-ca.key"
|
||||
DefaultCAHash = "6c3578b4"
|
||||
)
|
||||
|
||||
|
|
@ -237,22 +242,124 @@ func FindLocalBin(root string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// FindLocalCA returns the first existing MITM CA cert under the repo.
|
||||
// FindLocalCA returns the MITM CA cert for this install.
|
||||
// Prefers a per-user pair under data/ (gitignored); falls back to a bundled
|
||||
// apps/proxy/*.crt only when its matching .key is also present.
|
||||
func FindLocalCA(root string) string {
|
||||
if root == "" {
|
||||
root = repo.Root()
|
||||
}
|
||||
user := filepath.Join(root, "data", "proxy-ca.crt")
|
||||
if caPairExists(user) {
|
||||
return user
|
||||
}
|
||||
for _, p := range []string{
|
||||
filepath.Join(root, "apps", "proxy", "rteproxy-ca.crt"),
|
||||
filepath.Join(root, "data", "proxy-ca.crt"),
|
||||
filepath.Join(root, "apps", "proxy", "appproxy-ca.crt"),
|
||||
} {
|
||||
if st, err := os.Stat(p); err == nil && !st.IsDir() {
|
||||
if caPairExists(p) {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func caPairExists(certPath string) bool {
|
||||
if st, err := os.Stat(certPath); err != nil || st.IsDir() {
|
||||
return false
|
||||
}
|
||||
keyPath := strings.TrimSuffix(certPath, filepath.Ext(certPath)) + ".key"
|
||||
st, err := os.Stat(keyPath)
|
||||
return err == nil && !st.IsDir()
|
||||
}
|
||||
|
||||
// EnsureLocalCA returns the path to this install's MITM CA cert, creating a
|
||||
// unique cert+key under data/ on first use so each user gets their own CA.
|
||||
func EnsureLocalCA(root string) (certPath string, err error) {
|
||||
if root == "" {
|
||||
root = repo.Root()
|
||||
}
|
||||
if p := FindLocalCA(root); p != "" {
|
||||
return p, nil
|
||||
}
|
||||
dir := filepath.Join(root, "data")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
certPath = filepath.Join(dir, "proxy-ca.crt")
|
||||
keyPath := filepath.Join(dir, "proxy-ca.key")
|
||||
fmt.Println("[*] No local MITM CA yet — generating a unique cert+key for this install…")
|
||||
if err := generateMITMCA(certPath, keyPath); err != nil {
|
||||
return "", err
|
||||
}
|
||||
hash, err := AndroidCAHashFile(certPath)
|
||||
if err != nil {
|
||||
return certPath, nil
|
||||
}
|
||||
fmt.Printf("[+] Wrote %s and %s (hash %s)\n", certPath, keyPath, hash)
|
||||
fmt.Println(" Keep data/ private; each machine should generate its own CA.")
|
||||
return certPath, nil
|
||||
}
|
||||
|
||||
func generateMITMCA(certPath, keyPath string) error {
|
||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Unique CN so Android subject_hash_old differs per install (filename HASH.0).
|
||||
cn := fmt.Sprintf("Null DRM Capture CA %x", serial.Bytes()[:8])
|
||||
tmpl := &x509.Certificate{
|
||||
SerialNumber: serial,
|
||||
Subject: pkix.Name{
|
||||
Organization: []string{"Null DRM MITM CA"},
|
||||
CommonName: cn,
|
||||
},
|
||||
NotBefore: time.Now().Add(-time.Hour),
|
||||
NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
MaxPathLen: 1,
|
||||
}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
certOut, err := os.OpenFile(certPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: der}); err != nil {
|
||||
certOut.Close()
|
||||
return err
|
||||
}
|
||||
certOut.Close()
|
||||
|
||||
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}); err != nil {
|
||||
keyOut.Close()
|
||||
return err
|
||||
}
|
||||
return keyOut.Close()
|
||||
}
|
||||
|
||||
// AndroidCAHashFile is AndroidCAHash for a PEM file on disk.
|
||||
func AndroidCAHashFile(certPath string) (string, error) {
|
||||
pemBytes, err := os.ReadFile(certPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return AndroidCAHash(pemBytes)
|
||||
}
|
||||
|
||||
// AndroidCAHash returns the OpenSSL subject_hash_old used for system CA files.
|
||||
func AndroidCAHash(certPEM []byte) (string, error) {
|
||||
block, _ := pem.Decode(certPEM)
|
||||
|
|
@ -270,12 +377,13 @@ func AndroidCAHash(certPEM []byte) (string, error) {
|
|||
|
||||
// InstallCA pushes the MITM CA onto the device as HASH.0 and optionally Magisk-reinjects it.
|
||||
// When reinject is true, runs the full Magisk conscrypt bind (needs root / Magisk busybox).
|
||||
// If this install has no CA yet, EnsureLocalCA creates a unique pair under data/.
|
||||
func InstallCA(c *adb.Client, localCA string, reinject bool) (hash string, err error) {
|
||||
if localCA == "" {
|
||||
localCA = FindLocalCA("")
|
||||
}
|
||||
if localCA == "" {
|
||||
return "", fmt.Errorf("no local CA cert found (expected apps/proxy/rteproxy-ca.crt)")
|
||||
localCA, err = EnsureLocalCA("")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
pemBytes, err := os.ReadFile(localCA)
|
||||
if err != nil {
|
||||
|
|
@ -293,6 +401,12 @@ func InstallCA(c *adb.Client, localCA string, reinject bool) (hash string, err e
|
|||
// Also drop a copy named after the process for clarity.
|
||||
_ = c.Push(localCA, "/data/local/tmp/appproxy-ca.crt")
|
||||
|
||||
// Private key must match the cert. Without it, transparent appproxy exits
|
||||
// immediately with "tls: private key does not match public key".
|
||||
if err := pushMatchingCAKey(c, localCA); err != nil {
|
||||
return hash, err
|
||||
}
|
||||
|
||||
tmpHash := filepath.Join(os.TempDir(), hash+".0")
|
||||
if err := os.WriteFile(tmpHash, pemBytes, 0o644); err != nil {
|
||||
return hash, err
|
||||
|
|
@ -314,6 +428,33 @@ func InstallCA(c *adb.Client, localCA string, reinject bool) (hash string, err e
|
|||
return hash, nil
|
||||
}
|
||||
|
||||
// pushMatchingCAKey pushes the PEM key that belongs with localCA cert.
|
||||
func pushMatchingCAKey(c *adb.Client, localCA string) error {
|
||||
candidates := []string{
|
||||
strings.TrimSuffix(localCA, filepath.Ext(localCA)) + ".key",
|
||||
filepath.Join(repo.Root(), "data", "proxy-ca.key"),
|
||||
filepath.Join(filepath.Dir(localCA), "rteproxy-ca.key"),
|
||||
filepath.Join(filepath.Dir(localCA), "appproxy-ca.key"),
|
||||
}
|
||||
var localKey string
|
||||
for _, p := range candidates {
|
||||
if st, err := os.Stat(p); err == nil && !st.IsDir() {
|
||||
localKey = p
|
||||
break
|
||||
}
|
||||
}
|
||||
if localKey == "" {
|
||||
return fmt.Errorf("no CA private key for %s — run: drm proxy install-ca --reinject (generates data/proxy-ca.key)", localCA)
|
||||
}
|
||||
if err := c.Push(localKey, RemoteCAKey); err != nil {
|
||||
return fmt.Errorf("push CA key: %w", err)
|
||||
}
|
||||
_ = c.Push(localKey, "/data/local/tmp/appproxy-ca.key")
|
||||
_, _ = c.Shell("chmod", "600", RemoteCAKey, "/data/local/tmp/appproxy-ca.key")
|
||||
fmt.Printf("[+] Pushed matching CA key (%s)\n", filepath.Base(localKey))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops the on-device mitm process.
|
||||
func Stop(c *adb.Client) {
|
||||
stopProxy(c)
|
||||
|
|
@ -339,8 +480,16 @@ func StartTransparent(c *adb.Client, localBin, pkg, port, remoteDir string, rein
|
|||
// Ensure no global HTTP proxy — transparent mode must not use one.
|
||||
ClearHTTPProxy(c)
|
||||
|
||||
localCA, err := EnsureLocalCA("")
|
||||
if err != nil {
|
||||
return fmt.Errorf("local CA: %w", err)
|
||||
}
|
||||
caHash, err := AndroidCAHashFile(localCA)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CA hash: %w", err)
|
||||
}
|
||||
if reinject {
|
||||
ReinjectCA(c, DefaultCAHash)
|
||||
ReinjectCA(c, caHash)
|
||||
}
|
||||
|
||||
if st, err := os.Stat(localBin); err != nil || st.IsDir() {
|
||||
|
|
@ -384,6 +533,22 @@ func StartTransparent(c *adb.Client, localBin, pkg, port, remoteDir string, rein
|
|||
return err
|
||||
}
|
||||
_, _ = c.Shell("chmod", "755", "/data/local/tmp/start_appproxy_tproxy.sh")
|
||||
// Push this install's cert+key (never a mismatched leftover on the phone).
|
||||
if err := c.Push(localCA, RemoteCACrt); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = c.Push(localCA, "/data/local/tmp/appproxy-ca.crt")
|
||||
if err := pushMatchingCAKey(c, localCA); err != nil {
|
||||
return fmt.Errorf("CA key: %w", err)
|
||||
}
|
||||
// Also drop HASH.0 for Magisk reinject / trust checks.
|
||||
pemBytes, _ := os.ReadFile(localCA)
|
||||
tmpHash := filepath.Join(os.TempDir(), caHash+".0")
|
||||
_ = os.WriteFile(tmpHash, pemBytes, 0o644)
|
||||
_ = c.Push(tmpHash, "/data/local/tmp/"+caHash+".0")
|
||||
_ = os.Remove(tmpHash)
|
||||
_, _ = c.Shell("chmod", "644", "/data/local/tmp/"+caHash+".0", RemoteCACrt)
|
||||
|
||||
_, _, _ = c.Run("shell", "setsid /data/local/tmp/start_appproxy_tproxy.sh </dev/null >/dev/null 2>&1 &")
|
||||
|
||||
var pid string
|
||||
|
|
@ -395,6 +560,16 @@ func StartTransparent(c *adb.Client, localBin, pkg, port, remoteDir string, rein
|
|||
}
|
||||
}
|
||||
if pid == "" {
|
||||
logTail := c.Out("shell", "tail", "-n", "20", remoteDir+"/appproxy.log")
|
||||
if strings.TrimSpace(logTail) == "" {
|
||||
logTail = c.Out("shell", "cat", "/data/local/tmp/appproxy-start.out")
|
||||
}
|
||||
if strings.Contains(logTail, "private key does not match") {
|
||||
return fmt.Errorf("appproxy failed to start (transparent): CA cert/key mismatch on device — run: drm proxy install-ca --reinject\n%s", strings.TrimSpace(logTail))
|
||||
}
|
||||
if strings.TrimSpace(logTail) != "" {
|
||||
return fmt.Errorf("appproxy failed to start (transparent):\n%s", strings.TrimSpace(logTail))
|
||||
}
|
||||
return fmt.Errorf("appproxy failed to start (transparent)")
|
||||
}
|
||||
fmt.Printf("[+] appproxy pid=%s; capture → %s\n", pid, remoteDir)
|
||||
|
|
@ -565,13 +740,26 @@ ls /apex/com.android.conscrypt/cacerts/$HASH.0 2>/dev/null && echo CA_OK || echo
|
|||
case r := <-ch:
|
||||
out = r.out
|
||||
case <-time.After(20 * time.Second):
|
||||
fmt.Println("[!] CA reinject timed out after 20s — continuing (CA likely already mounted)")
|
||||
_, _ = c.Shell("su", "-c", "killall reinject_ca.sh 2>/dev/null; true")
|
||||
// Script may have finished mounting even if su -mm hung; verify on device.
|
||||
if caPresentOnDevice(c, caHash) {
|
||||
fmt.Println("[+] System CA present in conscrypt (reinject timed out, mount OK)")
|
||||
} else {
|
||||
fmt.Println("[!] CA reinject timed out after 20s — re-run: drm proxy install-ca --reinject")
|
||||
}
|
||||
return
|
||||
}
|
||||
if strings.Contains(out, "CA_OK") {
|
||||
if strings.Contains(out, "CA_OK") || caPresentOnDevice(c, caHash) {
|
||||
fmt.Println("[+] System CA present in conscrypt")
|
||||
} else {
|
||||
fmt.Println("[!] CA inject skipped/failed — if TLS errors, re-run Magisk CA mount")
|
||||
fmt.Println("[!] CA inject skipped/failed — re-run: drm proxy install-ca --reinject")
|
||||
}
|
||||
}
|
||||
|
||||
func caPresentOnDevice(c *adb.Client, caHash string) bool {
|
||||
if caHash == "" {
|
||||
caHash = DefaultCAHash
|
||||
}
|
||||
out := c.Out("shell", "su", "-c", "ls /apex/com.android.conscrypt/cacerts/"+caHash+".0")
|
||||
return strings.Contains(out, caHash+".0")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue