DEV Community

Vladimir Ignatev
Vladimir Ignatev

Posted on • Edited on

Decode obfuscated Javascript code with help of ChatGPT

Back in 2022 Anthony Smith shared a fun tip about code obfuscation in Javascript.

Problem?

Analyzing obfuscated JavaScript code is tough because the code is purposely made confusing to hide its real purpose. This makes it hard to debug, understand, and check for security issues, requiring special tools and methods.

When analyzing obfuscated JavaScript code, try using GPT! Here’s how to do it.

Quick tip on decoding obfuscate Javascript code using GPT

  1. Ask GPT to analyze the code and paste the code after.
  2. Then ask to rewrite it for better readability!

I/O

Here is what I fed to GPT:

Please analyze the following code and tell me what it does? 

function _0x33bc(_0x357a49, _0x4d043b) {
    var _0x3af57c = _0x3af5();
    return _0x33bc = function (_0x33bc59, _0x2d7f6b) {
        _0x33bc59 = _0x33bc59 - 0xac;
        var _0x433c74 = _0x3af57c[_0x33bc59];
        return _0x433c74;
    }, _0x33bc(_0x357a49, _0x4d043b);
}
var _0x5b0965 = _0x33bc;

function _0x3af5() {
    var _0x16d4f2 = ['2293120DDXwAC', '5tguhIh', 'ExpressJS', '32097acJbHr', '1234303tDdtZs', 'HTML', '51273EoArEE', 'CSS', 'JavaScript', '3214428qqnLuG', 'MongoDB', '40qdyzEo', 'Smith', '252346qcgJWs', '292cvJCiT', 'Anthony', 'PHP', '2392880xWQHuO',...
Enter fullscreen mode Exit fullscreen mode

Then she explained details, but still wasn't clear what this code is supposed to do.

After that I asked for more!.. She flawlessly re-wrote it and turned it into human-readable and friendly source code! Amazing!

function generateProgrammingLanguages() {
    return ['HTML', 'CSS', 'JavaScript', 'React', 'NodeJS', 'MongoDB', 'ExpressJS', 'PHP'];
}

function selectLanguage(index) {
    const languages = generateProgrammingLanguages();
    return languages[index];
}

const person = {
    first_name: 'Anthony',
    last_name: 'Smith',
    occupation: 'Full Stack Developer',
    languages: [
        selectLanguage(0),
        selectLanguage(1),
        selectLanguage(2),
        'React',
        selectLanguage(3),
        selectLanguage(4),
        selectLanguage(5),
        selectLanguage(6),
    ]
};
Enter fullscreen mode Exit fullscreen mode

Now the response is very close to sample from Anthony's tip. At least, it is much easier to understand what this code is doing.

How do you de-obfuscate source code? What features of ChatGPT do you use for explaining source code? Answer in comments!

Thanks for reading.

Reference

Check out the full session in ChatGPT.

Top comments (0)