Skip to content

Commit

Permalink
Add support for patching an unpacked apk (#97)
Browse files Browse the repository at this point in the history
* Use decoded apk folder from apktool as input

* Skip decoding when folder is given
  • Loading branch information
PlessioTihsrah authored Apr 17, 2022
1 parent 51b8c55 commit 816cd99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type TaskOptions = {
wait: boolean
isAppBundle: boolean
debuggable: boolean
skipDecode: boolean
}

interface PatchingError extends Error {
Expand Down Expand Up @@ -57,11 +58,11 @@ async function main() {
const fileExtension = path.extname(input)
const baseName = path.basename(input, fileExtension)
const outputName = `${baseName}-patched${fileExtension}`
const outputPath = path.resolve(path.dirname(inputPath), outputName)
let outputPath = path.resolve(path.dirname(inputPath), outputName)
let skipDecode = false

let isAppBundle = false
let taskFunction: (options: TaskOptions) => Listr

switch (fileExtension) {
case '.apk':
taskFunction = patchApk
Expand All @@ -75,6 +76,11 @@ async function main() {
isAppBundle = true
taskFunction = patchApksBundle
break
case '':
taskFunction = patchApk
skipDecode = true
outputPath += '.apk'
break
default:
showSupportedExtensions()
}
Expand Down Expand Up @@ -102,7 +108,13 @@ async function main() {
const uberApkSigner = new UberApkSigner()

showVersions({ apktool, uberApkSigner })
console.log(chalk.dim(` Using temporary directory:\n ${tmpDir}\n`))
if (skipDecode) {
console.log(
chalk.dim(` Patching from decoded apktool directory:\n ${inputPath}\n`),
)
} else {
console.log(chalk.dim(` Using temporary directory:\n ${tmpDir}\n`))
}

taskFunction({
inputPath,
Expand All @@ -115,6 +127,7 @@ async function main() {
skipPatches: args.skipPatches,
isAppBundle,
debuggable: args.debuggable,
skipDecode,
})
.run()
.then(async context => {
Expand Down Expand Up @@ -190,7 +203,7 @@ function formatCommandError(error: string, { tmpDir }: { tmpDir: string }) {

function showHelp() {
console.log(chalk`
$ {bold apk-mitm} <path-to-apk/xapk/apks>
$ {bold apk-mitm} <path-to-apk/xapk/apks/decoded-directory-by-apktool>
{blue {dim.bold *} Optional flags:}
{dim {bold --wait} Wait for manual changes before re-encoding}
Expand Down
5 changes: 4 additions & 1 deletion src/patch-apk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import checkPrerequisites from './tasks/check-prerequisites'
export default function patchApk(options: TaskOptions) {
const { apktool, uberApkSigner } = options

const decodeDir = path.join(options.tmpDir, 'decode')
const decodeDir = options.skipDecode
? options.inputPath
: path.join(options.tmpDir, 'decode')
const tmpApkPath = path.join(options.tmpDir, 'tmp.apk')

let fallBackToAapt = false
Expand All @@ -24,6 +26,7 @@ export default function patchApk(options: TaskOptions) {
},
{
title: 'Decoding APK file',
skip: () => options.skipDecode,
task: () => apktool.decode(options.inputPath, decodeDir),
},
{
Expand Down

0 comments on commit 816cd99

Please sign in to comment.