Skip to content

Instantly share code, notes, and snippets.

@Tommy0412
Forked from steffengy/Dean Edwards Unpacker.js
Last active August 9, 2025 13:46
Show Gist options
  • Save Tommy0412/a5152a5974d8523624b2f14ef765d197 to your computer and use it in GitHub Desktop.
Save Tommy0412/a5152a5974d8523624b2f14ef765d197 to your computer and use it in GitHub Desktop.
var unpacker = {
unpack: function(str) {
var params = unpacker.filterargs(str);
var payload = params[0],
symtab = params[1],
radix = params[2],
count = params[3];
if (count != symtab.length) {
throw new Error("Malformed p.a.c.k.e.r. symtab. (" + count + " != " + symtab.length + ")");
}
var unbase = unpacker.unbaser(radix);
var lookup = (word) => symtab[unbase(word)] || word;
var source = payload.replace(/\b\w+\b/g, lookup);
source = source
.replace(/\\'/g, "'")
.replace(/\\"/g, '"')
.replace(/\\n/g, '\n')
.replace(/\\r/g, '\r');
return source;
},
filterargs: function(str) {
var juicers = [
/}\('([\s\S]*)', *(\d+), *(\d+), *'([\s\S]*)'\.split\('\|'\), *(\d+), *([\s\S]*)\)\)/,
/}\('([\s\S]*)', *(\d+), *(\d+), *'([\s\S]*)'\.split\('\|'\)/
];
for (var c = 0; c < juicers.length; ++c) {
var m, juicer = juicers[c];
if (m = juicer.exec(str)) {
return [m[1], m[4].split('|'), parseInt(m[2]), parseInt(m[3])];
}
}
throw new Error("Could not make sense of p.a.c.k.e.r data (unexpected code structure)");
},
alphabet: {
62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
95: '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
},
unbaser: function(base) {
if (2 <= base <= 36) return (str) => parseInt(str, base);
var dictionary = {};
var alphabet = unpacker.alphabet[base];
if (!alphabet) throw new Error("Unsupported encoding");
for (var c = 0; c < alphabet.length; ++alphabet) {
dictionary[alphabet[c]] = c;
}
return (str) => str.split("").reverse().reduce((cipher, ind) => Math.pow(base, ind) * dictionary[cipher]);
}
};
//usage example
//const packed = `eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(0(){4 1="5 6 7 8";0 2(3){9(3)}2(1)})();',10,10,'function|b|something|a|var|some|sample|packed|code|alert'.split('|'),0,{}))`;
//const unpacked = unpacker.unpack(packed);
//console.log(unpacked);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment