Skip to content

Commit 0ae8e37

Browse files
committed
initial commit of the package
1 parent 70cd35d commit 0ae8e37

File tree

7 files changed

+291
-0
lines changed

7 files changed

+291
-0
lines changed

.github/workflows/npmpublish.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3+
4+
name: Node.js Package
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v1
16+
with:
17+
node-version: 12
18+
- run: npm ci
19+
20+
publish-npm:
21+
needs: build
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v2
25+
- uses: actions/setup-node@v1
26+
with:
27+
node-version: 12
28+
registry-url: https://registry.npmjs.org/
29+
- run: npm ci
30+
- run: npm publish
31+
env:
32+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
src/
2+
*.map
3+
.github
4+
.vscode
5+
tsconfig.json

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": [
12+
"<node_internals>/**"
13+
],
14+
"program": "${workspaceFolder}\\dist\\jsunpack.js",
15+
"preLaunchTask": "npm: compile",
16+
"outFiles": [
17+
"${workspaceFolder}/**/*.js"
18+
]
19+
}
20+
]
21+
}

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "unpacker",
3+
"version": "0.3.0-alpha-1",
4+
"description": "A nodejs library to unpack p.a.c.k.e.d javascript code.",
5+
"main": "dist\\main.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"prepublishOnly": "tsc"
9+
},
10+
"author": "mnsrulz",
11+
"license": "MIT",
12+
"dependencies": {},
13+
"devDependencies": {
14+
"@types/node": "^14.0.27",
15+
"typescript": "^3.9.7"
16+
},
17+
"repository": {
18+
"type": "git",
19+
"url": "git+https://github.com/mnsrulz/unpacker.git"
20+
}
21+
}

src/main.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
export class Unpacker {
2+
detect(source: string) {
3+
/* Detects whether `source` is P.A.C.K.E.R. coded. */
4+
return source.replace(" ", "").startsWith("eval(function(p,a,c,k,e,");
5+
}
6+
7+
unpack(source: string) {
8+
/* Unpacks P.A.C.K.E.R. packed js code. */
9+
let { payload, symtab, radix, count } = _filterargs(source);
10+
11+
if (count != symtab.length) {
12+
throw Error("Malformed p.a.c.k.e.r. symtab.");
13+
}
14+
15+
let unbase: Unbaser;
16+
try {
17+
unbase = new Unbaser(radix);
18+
} catch (e) {
19+
throw Error("Unknown p.a.c.k.e.r. encoding.");
20+
}
21+
22+
function lookup(match: string): string {
23+
/* Look up symbols in the synthetic symtab. */
24+
const word = match;
25+
let word2: string;
26+
if (radix == 1) {
27+
//throw Error("symtab unknown");
28+
word2 = symtab[parseInt(word)];
29+
} else {
30+
word2 = symtab[unbase.unbase(word)];
31+
}
32+
return word2 || word;
33+
}
34+
35+
source = payload.replace(/\b\w+\b/g, lookup);
36+
return _replacestrings(source);
37+
38+
39+
function _filterargs(source: string) {
40+
/* Juice from a source file the four args needed by decoder. */
41+
const juicers = [
42+
/}\('(.*)', *(\d+|\[\]), *(\d+), *'(.*)'\.split\('\|'\), *(\d+), *(.*)\)\)/,
43+
/}\('(.*)', *(\d+|\[\]), *(\d+), *'(.*)'\.split\('\|'\)/,
44+
];
45+
for (const juicer of juicers) {
46+
//const args = re.search(juicer, source, re.DOTALL);
47+
const args = juicer.exec(source);
48+
if (args) {
49+
let a = args;
50+
if (a[2] == "[]") {
51+
//don't know what it is
52+
// a = list(a);
53+
// a[1] = 62;
54+
// a = tuple(a);
55+
}
56+
try {
57+
return {
58+
payload: a[1],
59+
symtab: a[4].split("|"),
60+
radix: parseInt(a[2]),
61+
count: parseInt(a[3]),
62+
};
63+
} catch (ValueError) {
64+
throw Error("Corrupted p.a.c.k.e.r. data.");
65+
}
66+
}
67+
}
68+
throw Error(
69+
"Could not make sense of p.a.c.k.e.r data (unexpected code structure)",
70+
);
71+
}
72+
73+
function _replacestrings(source: string): string {
74+
/* Strip string lookup table (list) and replace values in source. */
75+
/* Need to work on this. */
76+
return source;
77+
}
78+
}
79+
}
80+
81+
class Unbaser {
82+
/* Functor for a given base. Will efficiently convert
83+
strings to natural numbers. */
84+
protected ALPHABET: Record<number, string> = {
85+
62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
86+
95:
87+
"' !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'",
88+
};
89+
protected base: number;
90+
protected dictionary: Record<string, number> = {};
91+
92+
constructor(base: number) {
93+
this.base = base;
94+
95+
// fill elements 37...61, if necessary
96+
if (36 < base && base < 62) {
97+
this.ALPHABET[base] = this.ALPHABET[base] ||
98+
this.ALPHABET[62].substr(0, base);
99+
}
100+
// If base can be handled by int() builtin, let it do it for us
101+
if (2 <= base && base <= 36) {
102+
this.unbase = (value) => parseInt(value, base);
103+
} else {
104+
// Build conversion dictionary cache
105+
try {
106+
[...this.ALPHABET[base]].forEach((cipher, index) => {
107+
this.dictionary[cipher] = index;
108+
});
109+
} catch (er) {
110+
throw Error("Unsupported base encoding.");
111+
}
112+
this.unbase = this._dictunbaser;
113+
}
114+
}
115+
116+
public unbase: (a: string) => number;
117+
118+
private _dictunbaser(value: string): number {
119+
/* Decodes a value to an integer. */
120+
let ret = 0;
121+
[...value].reverse().forEach((cipher, index) => {
122+
ret = ret + ((this.base ** index) * this.dictionary[cipher]);
123+
});
124+
return ret;
125+
}
126+
}

tsconfig.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
// "incremental": true, /* Enable incremental compilation */
5+
"target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
6+
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
7+
// "lib": [], /* Specify library files to be included in the compilation. */
8+
// "allowJs": true, /* Allow javascript files to be compiled. */
9+
// "checkJs": true, /* Report errors in .js files. */
10+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
11+
"declaration": true, /* Generates corresponding '.d.ts' file. */
12+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13+
"sourceMap": true, /* Generates corresponding '.map' file. */
14+
// "outFile": "./", /* Concatenate and emit output to single file. */
15+
"outDir": "./dist", /* Redirect output structure to the directory. */
16+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
17+
// "composite": true, /* Enable project compilation */
18+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
19+
// "removeComments": true, /* Do not emit comments to output. */
20+
// "noEmit": true, /* Do not emit outputs. */
21+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
22+
"downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24+
25+
/* Strict Type-Checking Options */
26+
"strict": true, /* Enable all strict type-checking options. */
27+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28+
// "strictNullChecks": true, /* Enable strict null checks. */
29+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
30+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34+
35+
/* Additional Checks */
36+
// "noUnusedLocals": true, /* Report errors on unused locals. */
37+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
38+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40+
41+
/* Module Resolution Options */
42+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
44+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
46+
// "typeRoots": [], /* List of folders to include type definitions from. */
47+
// "types": [], /* Type declaration files to be included in compilation. */
48+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
52+
53+
/* Source Map Options */
54+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
55+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
56+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
57+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
58+
59+
/* Experimental Options */
60+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
61+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
62+
63+
/* Advanced Options */
64+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
65+
}
66+
}

0 commit comments

Comments
 (0)