SlideShare a Scribd company logo
JS DeObfuscation with JStillery
Stefano Di Paola CTO + Chief Scientist
@MindedSecurity
13 January 2018
❖ Research (Spare Time)
➢ Bug Hunter & Sec Research (Pdf UXSS, Flash Security, HPP, DOMinator)
➢ Software Security Since ~'99
➢ Dealing with JavaScript since 2006
❖ Work
➢ CTO @ Minded Security
➢ Chief Scientist
$ WhoAmI
❖ 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!
❖ Scope of Obfuscation: Block-Limit RE
➢ Intellectual Property preservation
➢ AV Bypass of Exploits
➢ WAF Bypass of Cross Site Scripting Payload
JS And Obfuscation
❖ Publicly known JS 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/javascript-obfuscator/javascript-obfuscator
➢ https://github.com/search?p=2&q=obfuscator+JavaScript&type=Repositories&utf8=%
E2%9C%93
❖ Vendor Based JS Obfuscators:
➢ https://javascriptobfuscator.com/
➢ https://jscrambler.com
JS And Obfuscation
JSObfu
JSFuck
AAEncode
JJEncode
❖ 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)
Why Do We Want to Deobfuscate?
Deobfuscation from P to P’
❖ Semantics preservation:
➢ Semantics preservation is required.
❖ Automation:
➢ P’ 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:
➢ P’ is easy to adapt and analyze.
❖ Efficiency:
➢ Program P’ should not be much slower or larger than P.
Deobfuscation Techniques
❖ Easy way:
➢ Runtime. Use Sandboxed Environment to execute the 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 ^_^’!
❖ Someone already wrote some AST Based deobf for JSObfu:
➢ https://github.com/m1el/esdeobfuscate (DEMO)
➢ https://github.com/m1el/esdeobfuscate/blob/master/esdeobfuscate.js#L109
❖ Super Cool! Alas, is strictly related to JSObfu (DEMO)
❖ 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.
➢ https://github.com/m1el/esdeobfuscate/blob/master/esdeobfuscate.js
❖ A very good starting point!
❖ Possibly Deobfuscate all known obfuscators
❖ 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;
What we want
Implementation: Function execution
❖ Check for literal returned value (JSObf uDEMO)
➢ function xx(){
return String.fromCharCode(“x61”)+”X”
}
➢ if return val = constant -> substitute the value to
the whole sub tree.
❖ Check for independent scope (Closed scope) ( Fun.js DEMO)
➢ if function is closure > execute function in a JS environment.
Implementation: Function Scoping
❖ To Deal W/ Variable substitution & Function scope Analysis.
❖ Scopes are Objects
❖ SubScopes are Object whose prototype is the super Scope:
➢ function_scope = Object.create(scope);
function findScope(key,scope){
if( !scope ) return false;
if(scope.hasOwnProperty(key)){
return {scope:scope,value:scope[key]};
}
return findScope(key,scope.__proto__);
}
Implementation: Dealing W/ Complex Data (Objects etc)
❖ 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
https://www.youtube.com/watch?v=QITb12MGvX4
Conclusions
❖ Release in a few days!! https://github.com/mindedsecurity/JStillery
❖ Research took about 15 days
❖ Not easy task, although I’m not a JS rookie :)
❖ Offline approach (multi pass + time analysis) could solve particular anti
deobf techniques.
❖ Hybrid approach can lead to interesting results
❖ 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)
Related projects
❖ https://github.com/svent/jsdetox
❖ https://illuminatejs.com/#/
❖ https://github.com/buffer/thug
Q&A
JStillery: https://github.com/mindedsecurity/JStillery
Email: stefano.dipaola@mindedsecurity.com
Twitter: @WisecWisec
Blog: http://blog.mindedsecurity.com
Company: http://www.mindedsecurity.com
゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`*/ ['_']; o=(゚ー゚) =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); (゚Д゚) =(゚Θ゚)= (o^_^o)/
(o^_^o);
(゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] ,゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] ,゚Д゚ノ:
((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚)
];
(゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚];
(゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + ((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+
((゚ー゚==3) +'_') [゚Θ゚]+
((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+((゚ー゚==3) +'_') [゚Θ゚];
(゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+((゚Д゚)+'_')
[(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; (゚ー゚)+=(゚Θ゚);
(゚Д゚)[゚ε゚]=''; (゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];(゚Д゚) [゚o゚]='"';
(゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+(゚Д゚)[゚o゚]+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((o^_^o) - (゚Θ゚))+
(゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) - (゚Θ゚))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+
(゚ー゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ ((o^_^o) +(o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+
(o^_^o)+
(゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) +(o^_^o))+ (o^_^o)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((o^_^o) -
(゚Θ゚))+ (゚Д゚)
[゚o゚]) (゚Θ゚)) ('_');

More Related Content

PDF
Advanced JS Deobfuscation
PDF
Veil-PowerView - NovaHackers
PDF
The State of the Veil Framework
PPTX
Adventures in Asymmetric Warfare
PPTX
Derbycon - Passing the Torch
PPTX
Defcon - Veil-Pillage
PDF
Ruxmon feb 2013 what happened to rails
PDF
The old is new, again. CVE-2011-2461 is back!
Advanced JS Deobfuscation
Veil-PowerView - NovaHackers
The State of the Veil Framework
Adventures in Asymmetric Warfare
Derbycon - Passing the Torch
Defcon - Veil-Pillage
Ruxmon feb 2013 what happened to rails
The old is new, again. CVE-2011-2461 is back!

What's hot (20)

PDF
Building an EmPyre with Python
PPTX
Building an Empire with PowerShell
PDF
Windows attacks - AT is the new black
PDF
Fuzzing - Part 2
PDF
[Wroclaw #7] Why So Serial?
PPTX
I Hunt Sys Admins
PDF
Web Exploitation
PDF
Windows Attacks AT is the new black
PDF
Bypassing Web Application Firewalls and other security filters
PDF
Defcon CTF quals
PPTX
Introduction to Node js
PPTX
Invoke-Obfuscation nullcon 2017
PDF
Modern UI Development With Node.js
PDF
Entomology 101
PDF
Building web apps with node.js, socket.io, knockout.js and zombie.js - Codemo...
PDF
Ace Up the Sleeve
PDF
DevOops & How I hacked you DevopsDays DC June 2015
PDF
9 anti-patterns for node.js teams
PDF
Ruxmon cve 2012-2661
PDF
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
Building an EmPyre with Python
Building an Empire with PowerShell
Windows attacks - AT is the new black
Fuzzing - Part 2
[Wroclaw #7] Why So Serial?
I Hunt Sys Admins
Web Exploitation
Windows Attacks AT is the new black
Bypassing Web Application Firewalls and other security filters
Defcon CTF quals
Introduction to Node js
Invoke-Obfuscation nullcon 2017
Modern UI Development With Node.js
Entomology 101
Building web apps with node.js, socket.io, knockout.js and zombie.js - Codemo...
Ace Up the Sleeve
DevOops & How I hacked you DevopsDays DC June 2015
9 anti-patterns for node.js teams
Ruxmon cve 2012-2661
ECMAScript 6 from an Attacker's Perspective - Breaking Frameworks, Sandboxes,...
Ad

Similar to Js deobfuscation with JStillery - bsides-roma 2018 (20)

PDF
Engineer Engineering Software
PDF
Craftsmanship in Computational Work
PPTX
[Mas 500] Software Development Strategies
PDF
Copass + Ruby on Rails = <3 - From Simplicity to Complexity
PPTX
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
PDF
Building a JavaScript Library
PPTX
Docker for Development
PPTX
Adversarial Post Ex - Lessons from the Pros
PPTX
Adversarial Post-Ex: Lessons From The Pros
PDF
The Peanut Butter Cup of Web-dev: Plack and single page web apps
PDF
Persistent mobile JavaScript
PDF
Building an Extensible, Resumable DSL on Top of Apache Groovy
PPTX
Visual Regression Testing: In search of an Ember solution
PDF
Web Development: The Next Five Years
PDF
Ops for NoOps - Operational Challenges for Serverless Apps
PDF
Web a Quebec - JS Debugging
ODP
Extjs Howto
PDF
Experiences with Microservices at Tuenti
PPT
Node.js: CAMTA Presentation
PDF
Open shift
Engineer Engineering Software
Craftsmanship in Computational Work
[Mas 500] Software Development Strategies
Copass + Ruby on Rails = <3 - From Simplicity to Complexity
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
Building a JavaScript Library
Docker for Development
Adversarial Post Ex - Lessons from the Pros
Adversarial Post-Ex: Lessons From The Pros
The Peanut Butter Cup of Web-dev: Plack and single page web apps
Persistent mobile JavaScript
Building an Extensible, Resumable DSL on Top of Apache Groovy
Visual Regression Testing: In search of an Ember solution
Web Development: The Next Five Years
Ops for NoOps - Operational Challenges for Serverless Apps
Web a Quebec - JS Debugging
Extjs Howto
Experiences with Microservices at Tuenti
Node.js: CAMTA Presentation
Open shift
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
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Website Design Services for Small Businesses.pdf
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PPTX
assetexplorer- product-overview - presentation
PDF
AutoCAD Professional Crack 2025 With License Key
PDF
Download FL Studio Crack Latest version 2025 ?
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
iTop VPN Crack Latest Version Full Key 2025
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Cost to Outsource Software Development in 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Website Design Services for Small Businesses.pdf
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
assetexplorer- product-overview - presentation
AutoCAD Professional Crack 2025 With License Key
Download FL Studio Crack Latest version 2025 ?
Computer Software and OS of computer science of grade 11.pptx
Monitoring Stack: Grafana, Loki & Promtail
Odoo Companies in India – Driving Business Transformation.pdf
iTop VPN Crack Latest Version Full Key 2025
Oracle Fusion HCM Cloud Demo for Beginners
Designing Intelligence for the Shop Floor.pdf
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Reimagine Home Health with the Power of Agentic AI​
Why Generative AI is the Future of Content, Code & Creativity?
Cost to Outsource Software Development in 2025

Js deobfuscation with JStillery - bsides-roma 2018

  • 1. JS DeObfuscation with JStillery Stefano Di Paola CTO + Chief Scientist @MindedSecurity 13 January 2018
  • 2. ❖ Research (Spare Time) ➢ Bug Hunter & Sec Research (Pdf UXSS, Flash Security, HPP, DOMinator) ➢ Software Security Since ~'99 ➢ Dealing with JavaScript since 2006 ❖ Work ➢ CTO @ Minded Security ➢ Chief Scientist $ WhoAmI
  • 3. ❖ 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! ❖ Scope of Obfuscation: Block-Limit RE ➢ Intellectual Property preservation ➢ AV Bypass of Exploits ➢ WAF Bypass of Cross Site Scripting Payload JS And Obfuscation
  • 4. ❖ Publicly known JS 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/javascript-obfuscator/javascript-obfuscator ➢ https://github.com/search?p=2&q=obfuscator+JavaScript&type=Repositories&utf8=% E2%9C%93 ❖ Vendor Based JS Obfuscators: ➢ https://javascriptobfuscator.com/ ➢ https://jscrambler.com JS And Obfuscation
  • 7. ❖ 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) Why Do We Want to Deobfuscate?
  • 8. Deobfuscation from P to P’ ❖ Semantics preservation: ➢ Semantics preservation is required. ❖ Automation: ➢ P’ 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: ➢ P’ is easy to adapt and analyze. ❖ Efficiency: ➢ Program P’ should not be much slower or larger than P.
  • 9. Deobfuscation Techniques ❖ Easy way: ➢ Runtime. Use Sandboxed Environment to execute the 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 ^_^’! ❖ Someone already wrote some AST Based deobf for JSObfu: ➢ https://github.com/m1el/esdeobfuscate (DEMO) ➢ https://github.com/m1el/esdeobfuscate/blob/master/esdeobfuscate.js#L109 ❖ Super Cool! Alas, is strictly related to JSObfu (DEMO) ❖ 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. ➢ https://github.com/m1el/esdeobfuscate/blob/master/esdeobfuscate.js ❖ A very good starting point!
  • 13. ❖ Possibly Deobfuscate all known obfuscators ❖ 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; What we want
  • 14. Implementation: Function execution ❖ Check for literal returned value (JSObf uDEMO) ➢ function xx(){ return String.fromCharCode(“x61”)+”X” } ➢ if return val = constant -> substitute the value to the whole sub tree. ❖ Check for independent scope (Closed scope) ( Fun.js DEMO) ➢ if function is closure > execute function in a JS environment.
  • 15. Implementation: Function Scoping ❖ To Deal W/ Variable substitution & Function scope Analysis. ❖ Scopes are Objects ❖ SubScopes are Object whose prototype is the super Scope: ➢ function_scope = Object.create(scope); function findScope(key,scope){ if( !scope ) return false; if(scope.hasOwnProperty(key)){ return {scope:scope,value:scope[key]}; } return findScope(key,scope.__proto__); }
  • 16. Implementation: Dealing W/ Complex Data (Objects etc) ❖ 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]
  • 18. Conclusions ❖ Release in a few days!! https://github.com/mindedsecurity/JStillery ❖ Research took about 15 days ❖ Not easy task, although I’m not a JS rookie :) ❖ Offline approach (multi pass + time analysis) could solve particular anti deobf techniques. ❖ Hybrid approach can lead to interesting results ❖ 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)
  • 19. Related projects ❖ https://github.com/svent/jsdetox ❖ https://illuminatejs.com/#/ ❖ https://github.com/buffer/thug
  • 20. Q&A JStillery: https://github.com/mindedsecurity/JStillery Email: stefano.dipaola@mindedsecurity.com Twitter: @WisecWisec Blog: http://blog.mindedsecurity.com Company: http://www.mindedsecurity.com
  • 21. ゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`*/ ['_']; o=(゚ー゚) =_=3; c=(゚Θ゚) =(゚ー゚)-(゚ー゚); (゚Д゚) =(゚Θ゚)= (o^_^o)/ (o^_^o); (゚Д゚)={゚Θ゚: '_' ,゚ω゚ノ : ((゚ω゚ノ==3) +'_') [゚Θ゚] ,゚ー゚ノ :(゚ω゚ノ+ '_')[o^_^o -(゚Θ゚)] ,゚Д゚ノ: ((゚ー゚==3) +'_')[゚ー゚] }; (゚Д゚) [゚Θ゚] =((゚ω゚ノ==3) +'_') [c^_^o];(゚Д゚) ['c'] = ((゚Д゚)+'_') [ (゚ー゚)+(゚ー゚)-(゚Θ゚) ]; (゚Д゚) ['o'] = ((゚Д゚)+'_') [゚Θ゚]; (゚o゚)=(゚Д゚) ['c']+(゚Д゚) ['o']+(゚ω゚ノ +'_')[゚Θ゚]+ ((゚ω゚ノ==3) +'_') [゚ー゚] + ((゚Д゚) +'_') [(゚ー゚)+(゚ー゚)]+ ((゚ー゚==3) +'_') [゚Θ゚]+ ((゚ー゚==3) +'_') [(゚ー゚) - (゚Θ゚)]+(゚Д゚) ['c']+((゚Д゚)+'_') [(゚ー゚)+(゚ー゚)]+ (゚Д゚) ['o']+((゚ー゚==3) +'_') [゚Θ゚]; (゚Д゚) ['_'] =(o^_^o) [゚o゚] [゚o゚];(゚ε゚)=((゚ー゚==3) +'_') [゚Θ゚]+ (゚Д゚) .゚Д゚ノ+((゚Д゚)+'_') [(゚ー゚) + (゚ー゚)]+((゚ー゚==3) +'_') [o^_^o -゚Θ゚]+((゚ー゚==3) +'_') [゚Θ゚]+ (゚ω゚ノ +'_') [゚Θ゚]; (゚ー゚)+=(゚Θ゚); (゚Д゚)[゚ε゚]=''; (゚Д゚).゚Θ゚ノ=(゚Д゚+ ゚ー゚)[o^_^o -(゚Θ゚)];(o゚ー゚o)=(゚ω゚ノ +'_')[c^_^o];(゚Д゚) [゚o゚]='"'; (゚Д゚) ['_'] ( (゚Д゚) ['_'] (゚ε゚+(゚Д゚)[゚o゚]+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((o^_^o) - (゚Θ゚))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) - (゚Θ゚))+ (゚ー゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (c^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ (゚ー゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ ((o^_^o) +(o^_^o))+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((゚ー゚) + (゚Θ゚))+ (o^_^o)+ (゚Д゚)[゚ε゚]+(゚Θ゚)+ ((o^_^o) +(o^_^o))+ (o^_^o)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ (゚Θ゚)+ (゚Д゚)[゚ε゚]+(゚ー゚)+ ((o^_^o) - (゚Θ゚))+ (゚Д゚) [゚o゚]) (゚Θ゚)) ('_');