| 1 | Description: remove this until apkverifier is packaged |
| 2 | |
| 3 | --- a/axml2xml/main.go |
| 4 | +++ /dev/null |
| 5 | @@ -1,296 +0,0 @@ |
| 6 | -// This is a tool to extract AndroidManifest.xml from apks and verify their signatures. |
| 7 | -package main |
| 8 | - |
| 9 | -import ( |
| 10 | - "bufio" |
| 11 | - "crypto/x509" |
| 12 | - "encoding/xml" |
| 13 | - "flag" |
| 14 | - "fmt" |
| 15 | - "github.com/avast/apkparser" |
| 16 | - "github.com/avast/apkverifier" |
| 17 | - "io" |
| 18 | - "io/ioutil" |
| 19 | - "os" |
| 20 | - "runtime/pprof" |
| 21 | - "strings" |
| 22 | -) |
| 23 | - |
| 24 | -type optsType struct { |
| 25 | - isApk bool |
| 26 | - isManifest bool |
| 27 | - isResources bool |
| 28 | - verifyApk bool |
| 29 | - dumpManifest bool |
| 30 | - extractCert bool |
| 31 | - |
| 32 | - cpuProfile string |
| 33 | - fileListPath string |
| 34 | - dumpFrostingProto string |
| 35 | - xmlFileName string |
| 36 | -} |
| 37 | - |
| 38 | -func main() { |
| 39 | - var opts optsType |
| 40 | - |
| 41 | - flag.BoolVar(&opts.isApk, "a", false, "The input file is an apk (default if INPUT is *.apk)") |
| 42 | - flag.BoolVar(&opts.isManifest, "m", false, "The input file is an AndroidManifest.xml (default)") |
| 43 | - flag.BoolVar(&opts.isResources, "r", false, "The input is resources.arsc file (default if INPUT is *.arsc)") |
| 44 | - flag.BoolVar(&opts.verifyApk, "v", false, "Verify the file signature if it is an APK.") |
| 45 | - flag.BoolVar(&opts.extractCert, "e", false, "Extract the certificate without verifying it.") |
| 46 | - flag.BoolVar(&opts.dumpManifest, "d", true, "Print the AndroidManifest.xml (only makes sense for APKs)") |
| 47 | - flag.StringVar(&opts.cpuProfile, "cpuprofile", "", "Write cpu profiling info") |
| 48 | - flag.StringVar(&opts.fileListPath, "l", "", "Process file list") |
| 49 | - flag.StringVar(&opts.dumpFrostingProto, "dumpfrosting", "", "Dump Google Play Frosting protobuf data") |
| 50 | - flag.StringVar(&opts.xmlFileName, "f", "AndroidManifest.xml", "Name of the XML file from inside apk to parse") |
| 51 | - |
| 52 | - flag.Parse() |
| 53 | - |
| 54 | - if opts.fileListPath == "" && len(flag.Args()) < 1 { |
| 55 | - fmt.Printf("%s INPUT\n", os.Args[0]) |
| 56 | - os.Exit(1) |
| 57 | - } |
| 58 | - |
| 59 | - exitcode := 0 |
| 60 | - defer func() { |
| 61 | - if r := recover(); r != nil { |
| 62 | - panic(r) |
| 63 | - } |
| 64 | - os.Exit(exitcode) |
| 65 | - }() |
| 66 | - |
| 67 | - if opts.cpuProfile != "" { |
| 68 | - f, err := os.Create(opts.cpuProfile) |
| 69 | - if err != nil { |
| 70 | - fmt.Fprintln(os.Stderr, err) |
| 71 | - exitcode = 1 |
| 72 | - return |
| 73 | - } |
| 74 | - defer f.Close() |
| 75 | - |
| 76 | - pprof.StartCPUProfile(f) |
| 77 | - defer pprof.StopCPUProfile() |
| 78 | - } |
| 79 | - |
| 80 | - if opts.fileListPath == "" { |
| 81 | - for i, input := range flag.Args() { |
| 82 | - if i != 0 { |
| 83 | - fmt.Println() |
| 84 | - } |
| 85 | - |
| 86 | - if len(flag.Args()) != 1 { |
| 87 | - fmt.Println("File:", input) |
| 88 | - } |
| 89 | - |
| 90 | - if !processInput(input, &opts) { |
| 91 | - exitcode = 1 |
| 92 | - } |
| 93 | - } |
| 94 | - } else { |
| 95 | - f, err := os.Open(opts.fileListPath) |
| 96 | - if err != nil { |
| 97 | - fmt.Fprintln(os.Stderr, err) |
| 98 | - os.Exit(1) |
| 99 | - } |
| 100 | - defer f.Close() |
| 101 | - |
| 102 | - s := bufio.NewScanner(f) |
| 103 | - for s.Scan() { |
| 104 | - if !processInput(s.Text(), &opts) { |
| 105 | - exitcode = 1 |
| 106 | - } |
| 107 | - } |
| 108 | - } |
| 109 | -} |
| 110 | - |
| 111 | -func processInput(input string, opts *optsType) bool { |
| 112 | - var r io.Reader |
| 113 | - |
| 114 | - if !opts.isApk && !opts.isManifest && !opts.isResources { |
| 115 | - if strings.HasSuffix(input, ".apk") { |
| 116 | - opts.isApk = true |
| 117 | - } else if strings.HasSuffix(input, ".arsc") { |
| 118 | - opts.isResources = true |
| 119 | - } else { |
| 120 | - opts.isManifest = true |
| 121 | - } |
| 122 | - } |
| 123 | - |
| 124 | - if opts.isApk { |
| 125 | - return processApk(input, opts) |
| 126 | - } else { |
| 127 | - if input == "-" { |
| 128 | - r = os.Stdin |
| 129 | - } else { |
| 130 | - f, err := os.Open(input) |
| 131 | - if err != nil { |
| 132 | - fmt.Fprintln(os.Stderr, err) |
| 133 | - return false |
| 134 | - } |
| 135 | - defer f.Close() |
| 136 | - r = f |
| 137 | - } |
| 138 | - |
| 139 | - var err error |
| 140 | - if opts.isManifest { |
| 141 | - enc := xml.NewEncoder(os.Stdout) |
| 142 | - enc.Indent("", " ") |
| 143 | - |
| 144 | - err = apkparser.ParseManifest(r, enc, nil) |
| 145 | - } else { |
| 146 | - _, err = apkparser.ParseResourceTable(r) |
| 147 | - } |
| 148 | - |
| 149 | - fmt.Println() |
| 150 | - if err != nil { |
| 151 | - fmt.Fprintln(os.Stderr, err) |
| 152 | - return false |
| 153 | - } |
| 154 | - } |
| 155 | - return true |
| 156 | -} |
| 157 | - |
| 158 | -func processApk(input string, opts *optsType) bool { |
| 159 | - enc := xml.NewEncoder(os.Stdout) |
| 160 | - enc.Indent("", " ") |
| 161 | - |
| 162 | - apkReader, err := apkparser.OpenZip(input) |
| 163 | - if err != nil { |
| 164 | - fmt.Fprintln(os.Stderr, err) |
| 165 | - return false |
| 166 | - } |
| 167 | - defer apkReader.Close() |
| 168 | - |
| 169 | - if opts.dumpManifest { |
| 170 | - parser, reserr := apkparser.NewParser(apkReader, enc) |
| 171 | - if reserr != nil { |
| 172 | - fmt.Fprintf(os.Stderr, "\nFailed to parse resources: %s", reserr.Error()) |
| 173 | - } |
| 174 | - |
| 175 | - err := parser.ParseXml(opts.xmlFileName) |
| 176 | - |
| 177 | - fmt.Println() |
| 178 | - if err != nil { |
| 179 | - fmt.Fprintln(os.Stderr, err) |
| 180 | - return false |
| 181 | - } |
| 182 | - } |
| 183 | - |
| 184 | - if !opts.verifyApk && !opts.extractCert { |
| 185 | - return true |
| 186 | - } |
| 187 | - |
| 188 | - if opts.dumpManifest { |
| 189 | - fmt.Print("\n=====================================\n") |
| 190 | - } |
| 191 | - |
| 192 | - if opts.verifyApk { |
| 193 | - return verifyApk(input, apkReader, opts) |
| 194 | - } else if opts.extractCert { |
| 195 | - certs, err := apkverifier.ExtractCerts(input, apkReader) |
| 196 | - if err != nil { |
| 197 | - fmt.Fprintln(os.Stderr, "Error:", err) |
| 198 | - return false |
| 199 | - } |
| 200 | - printCerts(certs) |
| 201 | - } |
| 202 | - |
| 203 | - return true |
| 204 | -} |
| 205 | - |
| 206 | -func verifyApk(input string, apkReader *apkparser.ZipReader, opts *optsType) bool { |
| 207 | - res, err := apkverifier.Verify(input, apkReader) |
| 208 | - |
| 209 | - fmt.Printf("Verification scheme used: v%d\n", res.SigningSchemeId) |
| 210 | - |
| 211 | - printCerts(res.SignerCerts) |
| 212 | - |
| 213 | - fmt.Println() |
| 214 | - |
| 215 | - if blk := res.SigningBlockResult; blk != nil { |
| 216 | - if blk.SigningLineage != nil { |
| 217 | - fmt.Println("Signing lineage:") |
| 218 | - for i, n := range blk.SigningLineage.Nodes { |
| 219 | - fmt.Printf("Node #%d:\n", i) |
| 220 | - n.Dump(os.Stdout) |
| 221 | - fmt.Println() |
| 222 | - } |
| 223 | - } |
| 224 | - |
| 225 | - fmt.Printf("Google Play Store Frosting: ") |
| 226 | - if blk.Frosting != nil { |
| 227 | - fmt.Println("present") |
| 228 | - if blk.Frosting.Error == nil { |
| 229 | - fmt.Printf(" verification: ok\n") |
| 230 | - } else { |
| 231 | - fmt.Printf(" verification: FAILED, %s\n", blk.Frosting.Error.Error()) |
| 232 | - } |
| 233 | - |
| 234 | - fmt.Println(" protobuf data length:", len(blk.Frosting.ProtobufInfo)) |
| 235 | - |
| 236 | - if blk.Frosting.KeySha256 != "" { |
| 237 | - fmt.Println(" used key sha256:", blk.Frosting.KeySha256) |
| 238 | - } |
| 239 | - fmt.Println() |
| 240 | - |
| 241 | - if opts.dumpFrostingProto != "" { |
| 242 | - if err := ioutil.WriteFile(opts.dumpFrostingProto, blk.Frosting.ProtobufInfo, 0644); err != nil { |
| 243 | - fmt.Fprintf(os.Stderr, "Failed to dump Google Play Frosting protobuf: %s", err.Error()) |
| 244 | - } |
| 245 | - } |
| 246 | - } else { |
| 247 | - fmt.Println("missing") |
| 248 | - } |
| 249 | - |
| 250 | - if len(blk.Warnings) != 0 { |
| 251 | - fmt.Println("Warnings:") |
| 252 | - for _, w := range blk.Warnings { |
| 253 | - fmt.Println(" ", w) |
| 254 | - } |
| 255 | - fmt.Println() |
| 256 | - } |
| 257 | - |
| 258 | - if len(blk.Errors) > 1 { |
| 259 | - fmt.Println("Additional errors:") |
| 260 | - for i := 0; i < len(blk.Errors)-1; i++ { |
| 261 | - fmt.Println(" ", blk.Errors[i]) |
| 262 | - } |
| 263 | - fmt.Println() |
| 264 | - } |
| 265 | - } |
| 266 | - |
| 267 | - if err != nil { |
| 268 | - fmt.Fprintln(os.Stderr, "Error:", err) |
| 269 | - return false |
| 270 | - } |
| 271 | - return true |
| 272 | -} |
| 273 | - |
| 274 | -func printCerts(certs [][]*x509.Certificate) { |
| 275 | - _, picked := apkverifier.PickBestApkCert(certs) |
| 276 | - |
| 277 | - cinfo := &apkverifier.CertInfo{} |
| 278 | - var x int |
| 279 | - var cert *x509.Certificate |
| 280 | - for i, ca := range certs { |
| 281 | - for x, cert = range ca { |
| 282 | - cinfo.Fill(cert) |
| 283 | - |
| 284 | - fmt.Println() |
| 285 | - if picked == cert { |
| 286 | - fmt.Printf("Chain %d, cert %d [PICKED AS BEST]:\n", i, x) |
| 287 | - } else { |
| 288 | - fmt.Printf("Chain %d, cert %d:\n", i, x) |
| 289 | - } |
| 290 | - fmt.Println("algo:", cert.SignatureAlgorithm) |
| 291 | - fmt.Println("validfrom:", cinfo.ValidFrom) |
| 292 | - fmt.Println("validto:", cinfo.ValidTo) |
| 293 | - fmt.Println("serialnumber:", cert.SerialNumber.Text(16)) |
| 294 | - fmt.Println("thumbprint-md5:", cinfo.Md5) |
| 295 | - fmt.Println("thumbprint-sha1:", cinfo.Sha1) |
| 296 | - fmt.Println("thumbprint-sha256:", cinfo.Sha256) |
| 297 | - fmt.Printf("Subject:\n %s\n", cinfo.Subject) |
| 298 | - fmt.Printf("Issuer:\n %s\n", cinfo.Issuer) |
| 299 | - } |
| 300 | - } |
| 301 | -} |