Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsconfuser: new plugin #117

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import PluginCommon from './plugin/common.js'
import PluginJjencode from './plugin/jjencode.js'
import PluginJsconfuser from './plugin/jsconfuser.js'
import PluginSojson from './plugin/sojson.js'
import PluginSojsonV7 from './plugin/sojsonv7.js'
import PluginObfuscator from './plugin/obfuscator.js'
Expand Down Expand Up @@ -41,6 +42,8 @@ const main = () => {
code = PluginAwsc(sourceCode)
} else if (type === 'jjencode') {
code = PluginJjencode(sourceCode)
} else if (type === 'jsconfuser') {
code = PluginJsconfuser(sourceCode)
} else {
code = PluginCommon(sourceCode)
}
Expand Down
62 changes: 62 additions & 0 deletions src/plugin/jsconfuser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { parse } from '@babel/parser'
import _generate from '@babel/generator'
const generator = _generate.default
import _traverse from '@babel/traverse'
const traverse = _traverse.default

import calculateConstantExp from '../visitor/calculate-constant-exp.js'
import pruneIfBranch from '../visitor/prune-if-branch.js'
import jcAntiTooling from '../visitor/jsconfuser/anti-tooling.js'
import jcControlFlow from '../visitor/jsconfuser/control-flow.js'
import jcDuplicateLiteral from '../visitor/jsconfuser/duplicate-literal.js'
import jcGlobalConcealing from '../visitor/jsconfuser/global-concealing.js'
import jcMinifyInit from '../visitor/jsconfuser/minify.js'
import jcOpaquePredicates from '../visitor/jsconfuser/opaque-predicates.js'
import jcStackInit from '../visitor/jsconfuser/stack.js'
import jcStringCompression from '../visitor/jsconfuser/string-compression.js'
import jcStringConceal from '../visitor/jsconfuser/string-concealing.js'

export default function (code) {
let ast
try {
ast = parse(code, { errorRecovery: true })
} catch (e) {
console.error(`Cannot parse code: ${e.reasonCode}`)
return null
}
// AntiTooling
traverse(ast, jcAntiTooling)
// Minify
const jcMinify = jcMinifyInit()
traverse(ast, jcMinify.deMinifyArrow)
// DuplicateLiteralsRemoval
traverse(ast, jcDuplicateLiteral)
// Stack
const jcStack = jcStackInit(jcMinify.arrowFunc)
traverse(ast, jcStack.deStackFuncLen)
traverse(ast, jcStack.deStackFuncOther)
// StringCompression
traverse(ast, jcStringCompression)
// StringConcealing
traverse(ast, jcStringConceal.deStringConcealing)
traverse(ast, jcStringConceal.deStringConcealingPlace)
// StringSplitting
traverse(ast, calculateConstantExp)
// Stack (run again)
traverse(ast, jcStack.deStackFuncOther)
// OpaquePredicates
traverse(ast, jcOpaquePredicates)
traverse(ast, calculateConstantExp)
traverse(ast, pruneIfBranch)
// GlobalConcealing
traverse(ast, jcGlobalConcealing)
// ControlFlowFlattening
traverse(ast, jcControlFlow.deControlFlowFlatteningStateless)
traverse(ast, calculateConstantExp)
// ExpressionObfuscation
code = generator(ast, {
comments: false,
jsescOption: { minimal: true },
}).code
return code
}
15 changes: 15 additions & 0 deletions src/utility/check-func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function checkPattern(code, pattern) {
let i = 0
let j = 0
while (i < code.length && j < pattern.length) {
if (code[i] == pattern[j]) {
++j
}
++i
}
return j == pattern.length
}

export default {
checkPattern,
}
72 changes: 72 additions & 0 deletions src/utility/safe-func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as t from '@babel/types'

function safeDeleteNode(name, path) {
let binding
if (path.isFunctionDeclaration()) {
binding = path.parentPath.scope.getBinding(name)
} else {
binding = path.scope.getBinding(name)
}
if (!binding) {
return false
}
binding.scope.crawl()
binding = binding.scope.getBinding(name)
if (binding.references) {
return false
}
for (const item of binding.constantViolations) {
item.remove()
}
const decl = binding.path
if (decl.removed) {
return true
}
if (!decl.isVariableDeclarator() && !decl.isFunctionDeclaration()) {
return true
}
binding.path.remove()
return true
}

function safeGetLiteral(path) {
if (path.isUnaryExpression()) {
if (path.node.operator === '-' && path.get('argument').isNumericLiteral()) {
return -1 * path.get('argument').node.value
}
return null
}
if (path.isLiteral()) {
return path.node.value
}
return null
}

function safeGetName(path) {
if (path.isIdentifier()) {
return path.node.name
}
if (path.isLiteral()) {
return path.node.value
}
return null
}

function safeReplace(path, value) {
if (typeof value === 'string') {
path.replaceWith(t.stringLiteral(value))
return
}
if (typeof value === 'number') {
path.replaceWith(t.numericLiteral(value))
return
}
path.replaceWithSourceString(value)
}

export default {
safeDeleteNode,
safeGetLiteral,
safeGetName,
safeReplace,
}
53 changes: 53 additions & 0 deletions src/visitor/jsconfuser/anti-tooling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as t from '@babel/types'

function deAntiToolingCheckFunc(path) {
if (path.node.params.length) {
return false
}
const body = path.node.body
if (!t.isBlockStatement(body)) {
return false
}
if (body.body.length) {
return false
}
return true
}

function deAntiToolingExtract(path, func_name) {
let binding = path.scope.getBinding(func_name)
for (let ref of binding.referencePaths) {
if (!ref.parentPath.isCallExpression() || !ref.key === 'callee') {
continue
}
const call = ref.parentPath
if (!call.listKey === 'body') {
continue
}
for (let node of call.node.arguments) {
call.insertBefore(node)
}
call.remove()
}
binding.scope.crawl()
binding = path.scope.getBinding(func_name)
if (binding.references === 0) {
path.remove()
}
}

const deAntiTooling = {
FunctionDeclaration(path) {
const func_name = path.node.id?.name
if (!func_name) {
return
}
if (!deAntiToolingCheckFunc(path)) {
return
}
console.log(`AntiTooling Func Name: ${func_name}`)
deAntiToolingExtract(path, func_name)
},
}

export default deAntiTooling
Loading