Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for patching an unpacked apk #97

Merged
merged 2 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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