Skip to content

Instantly share code, notes, and snippets.

@tylergets
Created April 14, 2023 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylergets/08a15d1a551c6045cdc878a76316af00 to your computer and use it in GitHub Desktop.
Save tylergets/08a15d1a551c6045cdc878a76316af00 to your computer and use it in GitHub Desktop.
Search for OpenAI API Keys in APK files
const fs = require('fs');
const { exec } = require('child_process');
const apktoolPath = 'apktool'; // Update this with the path to apktool if it's not in your PATH
const rgPath = 'rg'; // Update this with the path to ripgrep if it's not in your PATH
// Regular expression you want to search for
const keyPattern = 'sk\\-[A-Za-z0-9]{32}';
// Execute a command and return a promise
function runCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
if (error.code === 1) { // No matches found
resolve('');
} else {
console.warn(`Error: ${error.message}`);
reject(error);
}
} else if (stderr) {
console.warn(`Stderr: ${stderr}`);
resolve(stderr);
} else {
resolve(stdout);
}
});
});
}
async function main() {
try {
const files = fs.readdirSync('.');
const apkFiles = files.filter(file => file.endsWith('.apk'));
for (const apkFile of apkFiles) {
const outputDir = `${apkFile}_decompiled`;
console.log(`Decompiling ${apkFile}...`);
await runCommand(`${apktoolPath} d ${apkFile} -f -o ${outputDir}`);
console.log(`Searching for regex '${keyPattern}' in ${outputDir}...`);
const grepResults = await runCommand(`${rgPath} -I -l '${keyPattern}' ${outputDir}`);
if (grepResults) {
console.log(`Found '${keyPattern}' in ${apkFile}:`);
console.log(grepResults);
} else {
console.log(`No matches found for '${keyPattern}' in ${apkFile}.`);
}
console.log('--------------------------------');
}
} catch (error) {
console.error(`An error occurred: ${error.message}`);
}
}
main();
module.exports = {
main,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment