Skip to content

Commit

Permalink
feat: mark keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
kuizuo committed Dec 22, 2023
1 parent 609f94a commit 2327a1a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
31 changes: 15 additions & 16 deletions packages/deob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { findDecoderByArray } from './transforms/find-decoder-by-array'
import { findDecoderByCallCount } from './transforms/find-decoder-by-call-count'
import { designDecoder } from './transforms/design-decoder'
import { decodeStrings } from './transforms/decode-strings'
import markComment from './transforms/mark-comment'
import { markKeyword } from './transforms/mark-keyword'
import mangle from './transforms/mangle'

export {
Expand Down Expand Up @@ -122,6 +122,17 @@ export class Deob {
this.reParse()
}

eval(code: string) {
try {
const result = global.eval(code)
logger('注入代码执行结果', result)
}
catch (error) {
logger(`code to be eval:\n${code}`)
throw new Error('evalCode 无法运行, 请在控制台中查看错误信息')
}
}

run(): DeobResult {
let outputCode = ''

Expand Down Expand Up @@ -217,6 +228,9 @@ export class Deob {
].flat(),
)
},
/** 标记关键字 */
options.isMarkEnable && (() => markKeyword(this.ast, options.keywords)),
/** 输出代码 */
() => (outputCode = generate(this.ast)),
].filter(Boolean) as (() => unknown)[]

Expand Down Expand Up @@ -259,19 +273,4 @@ export class Deob {
},
}
}

eval(code: string) {
try {
const result = global.eval(code)
logger('注入代码执行结果', result)
}
catch (error) {
logger(`code to be eval:\n${code}`)
throw new Error('evalCode 无法运行, 请在控制台中查看错误信息')
}
}

markComment({ keywords, label }: { keywords: string[]; label: string }) {
applyTransform(this.ast, markComment, { keywords, label })
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import traverse from '@babel/traverse'
import * as t from '@babel/types'
import type { Transform } from '../ast-utils'

export interface Options {
keywords: string[]
Expand All @@ -15,19 +14,13 @@ export interface Options {
* // TOLOOK
* debugger;
*/
export default {
name: '标记标识符',
tags: ['safe'],
run(ast, state, options) {
let { keywords, label } = options || { keywords: ['debugger'], label: ' TOLOOK' }
export function markKeyword(ast: t.Node, keywords = ['debugger'], label = ' TOLOOK') {
const defaultKeywords = ['debugger', 'setTimeout', 'setInterval']
keywords = [...new Set([...keywords.map(k => k.toLowerCase()), ...defaultKeywords])]

const defaultKeywords = ['debugger']
keywords = [
...new Set([...keywords.map(k => k.toLowerCase()), ...defaultKeywords]),
]

traverse(ast, {
DebuggerStatement(path) {
traverse(ast, {
DebuggerStatement: {
exit(path) {
// 如果已注释,则跳过
const hasComment = path.node.leadingComments?.find(
c => (c.value = label),
Expand All @@ -37,33 +30,28 @@ export default {

path.addComment('leading', label, true)
},
CallExpression(path) {
if (t.isMemberExpression(path.node.callee)) {
// if (
// !['setTimeout', 'setInterval'].includes(
// getPropName(path.node.callee.property),
// )
// )
// return
path.addComment('leading', label, true)
return
}

},
CallExpression: {
exit(path) {
if (t.isIdentifier(path.node.callee)) {
if (!['setTimeout', 'setInterval'].includes(path.node.callee.name))
if (!keywords.includes(path.node.callee.name))
return
path.addComment('leading', label, true)
}
},
StringLiteral(path) {
},
StringLiteral: {
exit(path) {
if (keywords.includes(path.node.value.toLowerCase())) {
const statementPath = path.findParent(p => p.isStatement())
if (statementPath)
statementPath.addComment('leading', label, true)
else path.addComment('leading', label, true)
}
},
Identifier(path) {
},
Identifier: {
exit(path) {
const name = path.node.name
if (keywords.includes(name.toLowerCase())) {
const statementPath = path.findParent(p => p.isStatement())
Expand All @@ -72,6 +60,6 @@ export default {
else path.addComment('leading', label, true)
}
},
})
},
} satisfies Transform<Options>
},
})
}

0 comments on commit 2327a1a

Please sign in to comment.