Skip to content

Instantly share code, notes, and snippets.

@imvasen
Created March 30, 2020 15:54
Show Gist options
  • Select an option

  • Save imvasen/3d50797bb8ce4e4f7b776e583752a904 to your computer and use it in GitHub Desktop.

Select an option

Save imvasen/3d50797bb8ce4e4f7b776e583752a904 to your computer and use it in GitHub Desktop.
Uses brute force to get a numerical password from a PDF.
/**
* Requirements:
* pdftops installed on your system.
* Usage:
* $ node pdf.js filename.pdf
*/
const { exec } = require('child_process');
async function fn(file) {
return new Promise((resolve, reject) => {
// Search for password in the range [0000-9999]
for (let i = 0; i < 1e4; i++) {
const password = `${i}`.padStart(4, '0');
exec(
`pdftops -upw ${password} ${file}`,
(error, stdout, stderr) => {
if (error) {
return;
}
resolve(password);
});
}
});
}
// Throws error if no name is provided
const filename = process.argv[2];
fn().then(password => console.log(`Password: ${password}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment