web/javascript
JavaScript Obfuscator (자바스크립트 난독화)
blackbearwow
2024. 9. 3. 22:22
난독화는 프로그래밍 언어로 작성된 코드에 대해 읽기 어렵게 만드는 작업이다.
온라인 js 난독화 사이트: https://obfuscator.io/
1. 설치
npm으로 설치
npm install --save-dev javascript-obfuscator
CDN으로 불러오기
<script src="https://cdn.jsdelivr.net/npm/javascript-obfuscator/dist/index.browser.js"></script>
2. 사용법
const jsObfuscator = require('javascript-obfuscator');
let obfuscationResult = jsObfuscator.obfuscate(
`let i=0;
for(;i<10;i++)console.log(i);`,
{
compact:false,
controlFlowFlattening: true,
controlFlowFlatteningThreshold:0.75,
numbersToExpressions: true,
simplify:true,
stringArrayShuffle: true,
splitStrings:true,
stringArrayThreshold:1
}
)
console.log(obfuscationResult.getObfuscatedCode());
obfuscate(sourceCode, options)
다음 메소드를 가진 obfuscationResult객체를 반환한다.
- getObfuscatedCode() - 난독화된 코드를 string형태로 반환한다.
메소드는 두 파라미터를 가진다
- sourceCode (string, default:null)
- options (Object, default:null)
options
key | value, default | 설명 |
compact | boolean, true | 한 라인에 코드를 출력한다. |
controlFlowFlattening | boolean, false | 프로그램 이해를 방해하는 소스코드 구조 변환을 일으킨다. |
controlFlowFlatteningThreshold | number, 0.75 Min:0, Max:1 |
주어진 노드마다 controlFlowFlattening이 적용될 확률이다. |
deadCodeInjection | boolean, false | 랜덤으로 불필요한 코드를 추가한다. |
deadCodeInjectionThreshold | number, 0.4 Min:0, Max:1 |
deadCodeInjection의 영향을 받는 노드의 확률을 설정한다. |
debugProtection | boolean, false | 개발자도구에서 debugger기능을 거의 사용할 수 없게 만든다. |
debugProtectionInterval | number, 0 | 개발자도구에서 다른기능을 사용하기 어렵게 콘솔 탭에서 디버그 모드를 강제로 ms단위로 실행시킨다. 2000에서 4000ms가 추천된다. |
numbersToExpressions | boolean, false | 숫자를 수식으로 바꾼다. |
simplify | boolean, true | 단순화를 통해 코드 난독화 정도를 추가한다. |
splitStrings | boolean, false | 문자열을 splitStringsChunkLengh옵션 값의 길이로 청크들을 만들어 분리한다. |
splitStringsChunkLength | number, 10 | splitStrings옵션의 청크 길이를 세팅한다. |
stringArray | boolean, true | 문자열 그 자체를 없애고 그 자리에 특별한 배열을 만든다. |
stringArrayShuffle | boolean, true | 랜덤으로 stringArray 배열 아이템들을 섞는다. |
stringArrayThreshold | number, 0.8 Min:0, Max:1 |
문자열이 stringArray에 삽입될 확률을 조정한다. |
2.1. CLI 사용법
하나의 파일을 난독화 하는법
javascript-obfuscator input_file_name.js --output output_file_name.js [options]
예시
javascript-obfuscator samples/sample.js --output output/output.js --compact true --self-defending false
2.2. 옵션 프리셋
높은 난독화, 낮은 성능
{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
debugProtection: true,
debugProtectionInterval: 4000,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: true,
renameGlobals: false,
selfDefending: true,
simplify: true,
splitStrings: true,
splitStringsChunkLength: 5,
stringArray: true,
stringArrayCallsTransform: true,
stringArrayEncoding: ['rc4'],
stringArrayIndexShift: true,
stringArrayRotate: true,
stringArrayShuffle: true,
stringArrayWrappersCount: 5,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersParametersMaxCount: 5,
stringArrayWrappersType: 'function',
stringArrayThreshold: 1,
transformObjectKeys: true,
unicodeEscapeSequence: false
}
중간 난독화, 최적 성능
{
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.75,
deadCodeInjection: true,
deadCodeInjectionThreshold: 0.4,
debugProtection: false,
debugProtectionInterval: 0,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: true,
renameGlobals: false,
selfDefending: true,
simplify: true,
splitStrings: true,
splitStringsChunkLength: 10,
stringArray: true,
stringArrayCallsTransform: true,
stringArrayCallsTransformThreshold: 0.75,
stringArrayEncoding: ['base64'],
stringArrayIndexShift: true,
stringArrayRotate: true,
stringArrayShuffle: true,
stringArrayWrappersCount: 2,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersParametersMaxCount: 4,
stringArrayWrappersType: 'function',
stringArrayThreshold: 0.75,
transformObjectKeys: true,
unicodeEscapeSequence: false
}
낮은 난독화, 높은 성능
{
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: 0,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: false,
renameGlobals: false,
selfDefending: true,
simplify: true,
splitStrings: false,
stringArray: true,
stringArrayCallsTransform: false,
stringArrayEncoding: [],
stringArrayIndexShift: true,
stringArrayRotate: true,
stringArrayShuffle: true,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersParametersMaxCount: 2,
stringArrayWrappersType: 'variable',
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false
}
기본 프리셋, 높은 성능
{
compact: true,
controlFlowFlattening: false,
deadCodeInjection: false,
debugProtection: false,
debugProtectionInterval: 0,
disableConsoleOutput: false,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: false,
renameGlobals: false,
selfDefending: false,
simplify: true,
splitStrings: false,
stringArray: true,
stringArrayCallsTransform: false,
stringArrayCallsTransformThreshold: 0.5,
stringArrayEncoding: [],
stringArrayIndexShift: true,
stringArrayRotate: true,
stringArrayShuffle: true,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersParametersMaxCount: 2,
stringArrayWrappersType: 'variable',
stringArrayThreshold: 0.75,
unicodeEscapeSequence: false
}
3. 예시
난독화 전:
난독화 전에는 코드를 분석하기 쉽다.
난독화 후:
난독화 후에는 코드를 읽기 어렵게 되었다.
참조: https://github.com/javascript-obfuscator/javascript-obfuscator
-