SlideShare a Scribd company logo
7
Most read
11
Most read
14
Most read
Advanced JS
DeObfuscation via AST
Stefano Di Paola CTO + Chief Scientist @MindedSecurity
JS And Obfuscation
❖ JS is super flexible!
❖ 1k+N ways the do the same thing - +N is the JS way
❖ OK from a Dev POV - performances apart
❖ Not Always OK for readability.
❖ SUPER OK for Obfuscation!
Goals of Obfuscation
❖Block-Limit RE
– Intellectual Property preservation
– AV Bypass of Exploits
– WAF Bypass of Cross Site Scripting Payload
3
JS Obfuscators
❖Several Public Obfuscation techniques:
– Eval Packer: http://dean.edwards.name/packer/
– Metasploit JSObfu: https://github.com/rapid7/jsobfu
– JSFuck (From Slackers): http://www.jsfuck.com/
– JJEncode : http://utf-8.jp/public/jjencode.html
– AAEncode: http://utf-8.jp/public/aaencode.html
– Node-Obf: https://github.com/wearefractal/node-obf
– https://github.com/search?p=2&q=obfuscator+JavaScript&type=Repositories&utf8=%
E2%9C%93
– On the wild ...
 Some commercial Obfuscator & Malware Obfuscator
JSObfu JSF#*k
Output Example
AAEncode
JJEncode
Output Example
Why Do We Want to Deobfuscate?
❖Defense!
❖Mainly to revert the Scope of Obfuscation:
– AV detection of known Exploits
– Precise WAF identification of Cross Site Scripting Payload
– Intellectual property (yeah that too)
 The Final Goal is to create a "Normalized" version of the
code that will allow easier comparison and analysis
Deobfuscation from P to P1
❖Semantics preservation:
– Semantics preservation is required.
❖Automation:
– P1 is obtained from P without the need for hand work (Ideally).
❖Robustness:
– All code valid to the interpreter should be parsable by the
deobfuscator.
❖Readability:
– P1 is easy to adapt and analyze.
❖Efficiency:
– Program P1 should not be much slower or larger than P.
Deobfuscation Techniques
❖ Easy way:
– Runtime. Sandboxed Environment to execute payload. (PhantomJS, Thug,
JSCli..)
– Pro : Easy
– Cons: behavior based. Can't classify by source code. Hard to analyze what's
going on. Possible Auto Pwnage.
❖ Harder Way:
– By hand (!!!)
– Pro: Human brain can be used.
– Cons: Human brain MUST be used. Slow, High Expertise… A Lot.
❖ Hard/Easy Way:
– Runtime + Static Analysis -> Hybrid approach via Partial Evaluation.
– Pro: Leads to interesting results.
– Cons: Hard to implement. Not trivial to cover all techniques.
Deobfuscation Via Partial Evaluation
❖ Partial evaluator task is to split a program in two parts
– Static part: precomputed by the partial evaluator. (reduced to
lowest terms)
– Dynamic part: executed at runtime. (dependent on runtime
environment)
 Two possible approaches:
– Online: all evaluations are made on-the-fly.
– Offline: Multipass. Performs binding time analysis to classify
expressions as static or dynamic, according to whether their
values will be fully determined at specialisation time.
AST > SubTree Reduction > Deobfuscated code
1.Use JS for JS : Node + Esprima
2.ESPrima Parser > AST > http://esprima.org/demo/parse.html#
3.Traverse AST (Tree Walking) as the interpreter would
4.Reduce Sub trees by applying:
– Constant folding
– Encapsulation
– Virtual dispatch
– ...
5.Rewrite the Code w/ escodegen
6.Hopefully Enjoy the new code
Start from Scratch, oh wait ^_^’!
❖ @M1el already wrote some AST Based deobf for JSObfu:
– https://github.com/m1el/esdeobfuscate
https://github.com/m1el/esdeobfuscate/blob/master/esdeobfuscate.js#L109
 Super Cool! Alas, is strictly related to JSObfu. We have:
– Constant folding w binary ops: +,-,*,/,^ and partial unary ops ~ - .. (On simple
types)
– String.fromCharCode execution
– function returning constants are “evaluated” and Reduced to their return value
– Partial “scope wise” implementation.
❖A very good starting point!
What we want
❖Improve Global Variables management
– "console","window","document","String","Object","Array","eval"...
❖Operations on Native Data (JSFuck … ) +[] ..
❖Global functions execution
– escape, unescape, String.*,Array.*..
❖Variable Substitution w/ constants or globals
– var win=window; …. t=win > var win=window; …. t=window
❖Scoping and Function Evaluation
– Function evaluation according to variable scoping.
 Objects Management:
– var t={a:2}; var b=t.a;
Possibly Deobfuscate all known obfuscators
Function Evaluation
❖Check for literal returned value
– function xx(){
return String.fromCharCode( 0x61)+"X"
}
– if (return val is constant )
substitute the value to the whole sub tree.
– (JSObf DEMO)
❖Check for independent scope (Closed scope)
– If function is a closure > execute function in a JS environment.
– ( Fun.js DEMO)
Dealing W/ Complex Data
❖ Hardest task so far
❖ Similar to Variable Substitution but harder
❖ Deal w/ Arrays and Objects
❖ Deal with dynamic properties
----------------------------
❖ Ended up creating a scope wise state machine. :O
❖ Partially implemented
var h={w:2};
var t="a";
h[t]=3;
var b=h.w+h[t]
JStillery
DEMO
Conclusions
 This research aims to prove that although AST based deobfuscation
is not an easy task, it could lead to quite interesting results.
❖ Offline approach (multi pass + time analysis) could solve particular
anti deobfuscation techniques.
❖ BTW Function Hoisting was not covered! In case someone
wondered.
❖ Does it work? Depends on the goals, of course ;)
❖ ActionScript would be mostly covered (as ECMAScript compatible)
Contacts + Q&A
Mail: stefano.dipaola@mindedsecurity.com
Twitter: @wisecwisec
Global Corporate Site: http://www.mindedsecurity.com
Blog: http://blog.mindedsecurity.com
Twitter: http://www.twitter.com/mindedsecurity
YouTube: http://www.youtube.com/user/mindedsecurity
Thanks!

More Related Content

PDF
적당한 스터디 발표자료 만들기
PPTX
모두가 성장하는 스터디 만들기
PDF
오픈 소스를 활용한 캐쥬얼 게임 서버 프레임워크 개발
ODP
Specifications pattern
PDF
AtCoder Beginner Contest 029 解説
PPTX
Introduction to Kotlin Language and its application to Android platform
PPTX
Code smell overview
PDF
[132] rust
적당한 스터디 발표자료 만들기
모두가 성장하는 스터디 만들기
오픈 소스를 활용한 캐쥬얼 게임 서버 프레임워크 개발
Specifications pattern
AtCoder Beginner Contest 029 解説
Introduction to Kotlin Language and its application to Android platform
Code smell overview
[132] rust

What's hot (20)

PPTX
人生を豊かにするための暗号技術の話 - ネクストスケープ 2014/04/24 パワーランチ資料
PPTX
Gazebo, 9개의 파일로 간단히 시작하는 로봇 시뮬레이션
PDF
Original slides from Ryan Dahl's NodeJs intro talk
PDF
Javascript Design Patterns
PPTX
Coding standards and guidelines
PDF
PDF
Ontology engineering ESTC2008
PPTX
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기
PDF
Ruby on Rails Presentation
PDF
AtCoder Regular Contest 034 解説
PDF
abc027
PDF
AtCoder Regular Contest 046
PPT
Refactoring Tips by Martin Fowler
PDF
AtCoder Regular Contest 019 解説
PDF
GCP 자격증 취득 후 찾아온 기회들
ODP
Object Oriented Javascript
PDF
React Development with the MERN Stack
PPTX
Net core
PDF
중앙 서버 없는 게임 로직
人生を豊かにするための暗号技術の話 - ネクストスケープ 2014/04/24 パワーランチ資料
Gazebo, 9개의 파일로 간단히 시작하는 로봇 시뮬레이션
Original slides from Ryan Dahl's NodeJs intro talk
Javascript Design Patterns
Coding standards and guidelines
Ontology engineering ESTC2008
유니티 + Nodejs를 활용한 멀티플레이어 게임 개발하기
Ruby on Rails Presentation
AtCoder Regular Contest 034 解説
abc027
AtCoder Regular Contest 046
Refactoring Tips by Martin Fowler
AtCoder Regular Contest 019 解説
GCP 자격증 취득 후 찾아온 기회들
Object Oriented Javascript
React Development with the MERN Stack
Net core
중앙 서버 없는 게임 로직
Ad

Similar to Advanced JS Deobfuscation (20)

PDF
Js deobfuscation with JStillery - bsides-roma 2018
PPTX
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
PDF
Play framework
PPT
PDF
A new execution model for Nashorn in Java 9
PDF
Engineer Engineering Software
PDF
Why scala is not my ideal language and what I can do with this
PPTX
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
PDF
Practical Chaos Engineering
PPTX
Ropython-windbg-python-extensions
PDF
The Peanut Butter Cup of Web-dev: Plack and single page web apps
PDF
Creating, obfuscating and analyzing malware JavaScript
ODP
Ceph Day Melbourne - Troubleshooting Ceph
PDF
Inside the JVM - Follow the white rabbit! / Breizh JUG
PDF
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
PDF
From Zero to Application Delivery with NixOS
PDF
Node.js at Joyent: Engineering for Production
PDF
Persistent mobile JavaScript
PPT
JS everywhere 2011
PDF
Accelerometer and Open GL
Js deobfuscation with JStillery - bsides-roma 2018
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
Play framework
A new execution model for Nashorn in Java 9
Engineer Engineering Software
Why scala is not my ideal language and what I can do with this
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
Practical Chaos Engineering
Ropython-windbg-python-extensions
The Peanut Butter Cup of Web-dev: Plack and single page web apps
Creating, obfuscating and analyzing malware JavaScript
Ceph Day Melbourne - Troubleshooting Ceph
Inside the JVM - Follow the white rabbit! / Breizh JUG
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
From Zero to Application Delivery with NixOS
Node.js at Joyent: Engineering for Production
Persistent mobile JavaScript
JS everywhere 2011
Accelerometer and Open GL
Ad

More from Minded Security (14)

PDF
Ieee S&P 2020 - Software Security: from Research to Industry.
PDF
Matteo Meucci - Security Summit 12th March 2019
PDF
Microservices Security: dos and don'ts
PDF
Live hacking Demo
PDF
Matteo Meucci Isaca Venice - 2017
PDF
BlueClosure Pitch - Cybertech Europe 2017
PDF
Minded Security - Fabrizio Bugli - (3rd) party like nobody's watching...
PDF
Matteo meucci Software Security - Napoli 10112016
PDF
Matteo Meucci Software Security in practice - Aiea torino - 30-10-2015
PDF
Sandboxing JS and HTML. A lession Learned
PDF
Concrete5 Sendmail RCE Advisory
PDF
Concrete5 Multiple Reflected XSS Advisory
PDF
PHP Object Injection
PDF
iOS Masque Attack
Ieee S&P 2020 - Software Security: from Research to Industry.
Matteo Meucci - Security Summit 12th March 2019
Microservices Security: dos and don'ts
Live hacking Demo
Matteo Meucci Isaca Venice - 2017
BlueClosure Pitch - Cybertech Europe 2017
Minded Security - Fabrizio Bugli - (3rd) party like nobody's watching...
Matteo meucci Software Security - Napoli 10112016
Matteo Meucci Software Security in practice - Aiea torino - 30-10-2015
Sandboxing JS and HTML. A lession Learned
Concrete5 Sendmail RCE Advisory
Concrete5 Multiple Reflected XSS Advisory
PHP Object Injection
iOS Masque Attack

Recently uploaded (20)

PDF
System and Network Administration Chapter 2
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
System and Network Administraation Chapter 3
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPT
Introduction Database Management System for Course Database
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
AI in Product Development-omnex systems
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
ai tools demonstartion for schools and inter college
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Online Work Permit System for Fast Permit Processing
System and Network Administration Chapter 2
2025 Textile ERP Trends: SAP, Odoo & Oracle
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Navsoft: AI-Powered Business Solutions & Custom Software Development
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administraation Chapter 3
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Introduction Database Management System for Course Database
Design an Analysis of Algorithms I-SECS-1021-03
AI in Product Development-omnex systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Upgrade and Innovation Strategies for SAP ERP Customers
ai tools demonstartion for schools and inter college
Softaken Excel to vCard Converter Software.pdf
Nekopoi APK 2025 free lastest update
Online Work Permit System for Fast Permit Processing

Advanced JS Deobfuscation

  • 1. Advanced JS DeObfuscation via AST Stefano Di Paola CTO + Chief Scientist @MindedSecurity
  • 2. JS And Obfuscation ❖ JS is super flexible! ❖ 1k+N ways the do the same thing - +N is the JS way ❖ OK from a Dev POV - performances apart ❖ Not Always OK for readability. ❖ SUPER OK for Obfuscation!
  • 3. Goals of Obfuscation ❖Block-Limit RE – Intellectual Property preservation – AV Bypass of Exploits – WAF Bypass of Cross Site Scripting Payload 3
  • 4. JS Obfuscators ❖Several Public Obfuscation techniques: – Eval Packer: http://dean.edwards.name/packer/ – Metasploit JSObfu: https://github.com/rapid7/jsobfu – JSFuck (From Slackers): http://www.jsfuck.com/ – JJEncode : http://utf-8.jp/public/jjencode.html – AAEncode: http://utf-8.jp/public/aaencode.html – Node-Obf: https://github.com/wearefractal/node-obf – https://github.com/search?p=2&q=obfuscator+JavaScript&type=Repositories&utf8=% E2%9C%93 – On the wild ...  Some commercial Obfuscator & Malware Obfuscator
  • 7. Why Do We Want to Deobfuscate? ❖Defense! ❖Mainly to revert the Scope of Obfuscation: – AV detection of known Exploits – Precise WAF identification of Cross Site Scripting Payload – Intellectual property (yeah that too)  The Final Goal is to create a "Normalized" version of the code that will allow easier comparison and analysis
  • 8. Deobfuscation from P to P1 ❖Semantics preservation: – Semantics preservation is required. ❖Automation: – P1 is obtained from P without the need for hand work (Ideally). ❖Robustness: – All code valid to the interpreter should be parsable by the deobfuscator. ❖Readability: – P1 is easy to adapt and analyze. ❖Efficiency: – Program P1 should not be much slower or larger than P.
  • 9. Deobfuscation Techniques ❖ Easy way: – Runtime. Sandboxed Environment to execute payload. (PhantomJS, Thug, JSCli..) – Pro : Easy – Cons: behavior based. Can't classify by source code. Hard to analyze what's going on. Possible Auto Pwnage. ❖ Harder Way: – By hand (!!!) – Pro: Human brain can be used. – Cons: Human brain MUST be used. Slow, High Expertise… A Lot. ❖ Hard/Easy Way: – Runtime + Static Analysis -> Hybrid approach via Partial Evaluation. – Pro: Leads to interesting results. – Cons: Hard to implement. Not trivial to cover all techniques.
  • 10. Deobfuscation Via Partial Evaluation ❖ Partial evaluator task is to split a program in two parts – Static part: precomputed by the partial evaluator. (reduced to lowest terms) – Dynamic part: executed at runtime. (dependent on runtime environment)  Two possible approaches: – Online: all evaluations are made on-the-fly. – Offline: Multipass. Performs binding time analysis to classify expressions as static or dynamic, according to whether their values will be fully determined at specialisation time.
  • 11. AST > SubTree Reduction > Deobfuscated code 1.Use JS for JS : Node + Esprima 2.ESPrima Parser > AST > http://esprima.org/demo/parse.html# 3.Traverse AST (Tree Walking) as the interpreter would 4.Reduce Sub trees by applying: – Constant folding – Encapsulation – Virtual dispatch – ... 5.Rewrite the Code w/ escodegen 6.Hopefully Enjoy the new code
  • 12. Start from Scratch, oh wait ^_^’! ❖ @M1el already wrote some AST Based deobf for JSObfu: – https://github.com/m1el/esdeobfuscate https://github.com/m1el/esdeobfuscate/blob/master/esdeobfuscate.js#L109  Super Cool! Alas, is strictly related to JSObfu. We have: – Constant folding w binary ops: +,-,*,/,^ and partial unary ops ~ - .. (On simple types) – String.fromCharCode execution – function returning constants are “evaluated” and Reduced to their return value – Partial “scope wise” implementation. ❖A very good starting point!
  • 13. What we want ❖Improve Global Variables management – "console","window","document","String","Object","Array","eval"... ❖Operations on Native Data (JSFuck … ) +[] .. ❖Global functions execution – escape, unescape, String.*,Array.*.. ❖Variable Substitution w/ constants or globals – var win=window; …. t=win > var win=window; …. t=window ❖Scoping and Function Evaluation – Function evaluation according to variable scoping.  Objects Management: – var t={a:2}; var b=t.a; Possibly Deobfuscate all known obfuscators
  • 14. Function Evaluation ❖Check for literal returned value – function xx(){ return String.fromCharCode( 0x61)+"X" } – if (return val is constant ) substitute the value to the whole sub tree. – (JSObf DEMO) ❖Check for independent scope (Closed scope) – If function is a closure > execute function in a JS environment. – ( Fun.js DEMO)
  • 15. Dealing W/ Complex Data ❖ Hardest task so far ❖ Similar to Variable Substitution but harder ❖ Deal w/ Arrays and Objects ❖ Deal with dynamic properties ---------------------------- ❖ Ended up creating a scope wise state machine. :O ❖ Partially implemented var h={w:2}; var t="a"; h[t]=3; var b=h.w+h[t]
  • 17. Conclusions  This research aims to prove that although AST based deobfuscation is not an easy task, it could lead to quite interesting results. ❖ Offline approach (multi pass + time analysis) could solve particular anti deobfuscation techniques. ❖ BTW Function Hoisting was not covered! In case someone wondered. ❖ Does it work? Depends on the goals, of course ;) ❖ ActionScript would be mostly covered (as ECMAScript compatible)
  • 18. Contacts + Q&A Mail: stefano.dipaola@mindedsecurity.com Twitter: @wisecwisec Global Corporate Site: http://www.mindedsecurity.com Blog: http://blog.mindedsecurity.com Twitter: http://www.twitter.com/mindedsecurity YouTube: http://www.youtube.com/user/mindedsecurity Thanks!