SlideShare a Scribd company logo
C+ +11 
METAPROGRAMMING 
A P P L IED 
TO 
SOF TWARE 
OBFUSCAT ION 
SEBAST IEN 
ANDRIVET 
Application Security Forum - 2014 
Western Switzerland 
05-06 November 2014 
Y-Parc / Yverdon-les-Bains 
http://www.appsec-forum.ch 
1
About me 
Senior Security Engineer 
a t SCRT (Swiss) 
Deve l o p e r 
a t ADVTOOLS (Swiss) 
Sebastien ANDRIVET 
Cyberfeminist & hack t i v i s t 
Reverse engineer Intel & ARM 
C + + , C , O b j - C , C # d e ve l o p e r 
Trainer ( iOS & Android appsec) 
2
PROBLEM 
3
Reverse engineering 
• Reverse engineering of an application if often like 
following the “white rabbit” 
• i.e. following string literals 
• Live demo 
• Reverse engineering of an application using IDA 
• Well-known MDM (Mobile Device Management) for 
iOS 
4
A SOLUTION 
OBFUSCATION 
5
What is Obfuscation? 
6
Obfuscator O 
O( ) = 
7
YES! It is also Katy Perry! 
• (almost) same 
semantics 
• obfuscated 
8
“Deliberate act of creating source or machine 
code difficult for humans to understand” 
–WIKIPEDIA, APRIL 2014 
Obfuscation 
9
C++ templates 
• Example: Stack of objects 
OBJECT2 
OBJECT1 
• Push 
• Pop 
10
Without templates 
c l a s s Stack 
{ 
v o i d p u s h (void* o b j e c t ) ; 
void* p o p ( ) ; 
} ; 
Stack singers; 
singers.push(britney); 
Stack apples; 
apples.push(macintosh); 
s i n g e r s apples 
• Reuse the same code (binary) 
• Only 1 instance of Stack class 
11
With C++ templates 
Stack<Singer> singers; 
singers.push(britney); 
Stack<Apple> apples; 
apples.push(macintosh); 
template<typename T> 
c l a s s Stack 
{ 
Stack<Singers> Stack<Apples*> 
v o i d p u s h ( T o b j e c t ) ; 
T p o p ( ) ; 
s i n g e r s apples 
} ; 
12
With C++ templates 
Stack<Singer> singers; 
singers.push(britney); 
Stack<Apple> apples; 
apples.push(macintosh); 
Stack<Singers> Stack<Apples*> 
s i n g e r s apples 
13
C++ templates 
• Two instances of Stack class 
• One per type 
• Does not reuse code 
• By default 
• Permit optimisations based on types 
• For ex. reuse code for all pointers to objects 
• Type safety, verified at compile time 
14
Type safety 
• singers.push(apple); // compilation error 
• apples.push(U2); 
• may or may not work… 
15
Optimisation based on types 
• Generate different code based on types (template 
parameters) 
• Example: enable_if 
template<typename T> 
class MyClass 
{ 
... 
enable_if_t<is_pointer<T>::value, T> 
member_function(T t) { ... }; 
... 
}; 
• member_function is only defined if T is a pointer type 
• (warning: C++14 code, not C++11) 
16
C++ metaprogramming 
• Programs that manipulate or produce programs 
• Subset of C++ 
• Turing-complete (~ full programming language) 
• Close to Functional programming 
• Part of C++ standards 
• Major enhancements in C++11 et C++14 
17
Application 1 - Strings literals obfuscation 
• original string is source code 
• original string in DEBUG builds 
• developer-friendly syntax 
• no trace of original string in compiled code in 
RELEASE builds 
18
1st implementation 
template<int... Indexes> 
struct MetaString1 { 
constexpr MetaString1(const char* str) 
: buffer_ {encrypt(str[Indexes])...} { } 
const char* decrypt(); 
RUNTIME 
private: 
constexpr char encrypt(char c) const { return c ^ 0x55; } 
constexpr char decrypt(char c) const { return encrypt(c); } 
private: 
char buffer_[sizeof...(Indexes) + 1]; 
}; 
19
1st implementation - Usage 
#define OBFUSCATED1(str) (MetaString1<0, 1, 2, 3, 4, 5>(str).decrypt()) 
cout << OBFUSCATED1(“Britney Spears”) << endl; 
20
1st implementation - Problem 
• List of indexes is hard-coded 
• 0, 1, 2, 3, 4, 5 
• As a consequence, strings are truncated! 
21
2nd implementation 
• Generate a list of indexes with metaprogramming 
• C++14 introduces std:index_sequence 
• With C++11, we have to implement our own version 
• Very simplified 
• MakeIndex<N>::type generates: 
• Indexes<0, 1, 2, 3, …, N> 
22
2nd implementation 
• Instead of: 
MetaString1<0, 1, 2, 3, 4, 5>(str) 
• we have: 
MetaString2<Make_Indexes<sizeof(str)-1>::type>(str) 
23
2nd implementation - Usage 
cout << OBFUSCATED2(“Katy Perry”) << endl; 
• No more truncation 
24
3rd implementation 
• In previous implementations, key is hard-coded 
constexpr char encrypt(char c) const { return c ^ 0x55; } 
• New template parameter for Key 
template<int... I, int K> 
struct MetaString3<Indexes<I...>, K> 
25
Generating (pseudo-) random numbers 
• C++11 includes <random>, but for runtime, not compile time 
• MetaRandom<N, M> 
N: Nth generated number 
M: Maximum value (excluded) 
• Linear congruential engine 
• Park-Miller (1988), “minimal standard” 
• Not exactly a uniform distribution (modulo operation) 
• Recursive 
26
Seed 
• template<> 
struct MetaRandomGenerator<0> { 
static const int value = seed; 
}; 
• How to choose an acceptable compile-time seed? 
• Macros (C & C++): 
• __TIME__: compilation time (standard) 
• __COUNTER__: incremented each time it is used 
(non-standard but well supported by compilers) 
27
3rd implementation 
• Different key for each string 
• thanks to __COUNTER__ 
• Different keys for each compilation 
• thanks to __TIME__ 
• Sometimes not desirable: can give hints to attackers 
(differences between versions) 
28
4th implementation 
• Different and random keys, great! 
• Why not go even further? 
• Choose a different encryption algorithm, randomly! 
29
4th implementation 
• Template partial specialization 
• template<int A, int K, typename Indexes> 
struct MetaString4; 
• template<int K, int... I> 
struct MetaString4<0, K, Indexes<I...>> { … c ^ K … }; 
• template<int K, int... I> 
struct MetaString4<1, K, Indexes<I...>> { … c + K … }; 
• #define DEF_OBFUSCATED4(str) 
MetaString4<MetaRandom<__COUNTER__, 2>::value, … 
30
Result 
• Without obfuscation 
cout << "Britney Spears” << endl; 
• With obfuscation 
cout << OBFUSCATED4("Britney Spears") << endl; 
31
Without obfuscation 
32
With obfuscation 
Encrypted 
characters 
(mixed with MOV) 
Decryption 
33
Application 2 - Obfuscate calls 
• How to obfuscate call such as: 
• function_to_protect(); 
• against static analysis (or even dynamic analysis)? 
34
Finite State Machine (simple example) 
35
Boost Meta State Machine (MSM) library 
types 
template parameter 
Compile time entity 
Generates code (FSM) at compile-time 
36
Result 
• Without obfuscation 
function_to_protect(“did”, “again”); 
• With obfuscation 
OBFUSCATED_CALL(function_to_protect, “did”, “again”); 
• Even better 
OBFUSCATED_CALL(function_to_protect, 
OBFUSCATED(“did”), OBFUSCATED(“again”)); 
37
Without obfuscation 
38
With obfuscation 
39
Application 3: FSM + Debugger Detection 
• FSM 
• To fight against static analysis 
• Debugger detection 
• To fight against dynamic analysis 
40
Finite State Machine 
• Follows a different path depending of a predicate 
(Debugged or not Debugged, that is the question) 
41
More obfuscation 
• Obfuscate predicate result 
• Avoid simple “if”, too simple for reverse engineers 
• Make computation instead 
• If the example, counter is odd if predicate is false 
42
More obfuscation 
• Obfuscate function address 
• Otherwise, IDA is smart enough to get it 
• Simply make some computation on address 
• Using MetaRandom (like for strings obfuscation) 
43
Predicate 
• Debugger detection is only an example 
• In the example, implemented only for Mac OS X / 
iOS 
• Virtual environment detection 
• Jailbreak detection 
• Etc 
44
Compilers support 
Compiler Compatible Remark 
Apple LLVM 5.1 Yes Previous versions not tested 
Apple LLVM 6.0 Yes Xcode 6, 6.1 beta 
LLVM 3.4, 3.5 Yes Previous versions not tested 
GCC 4.8.2 or higher Yes Previous versions not tested 
Compile with -std=c++11 
Intel C++ 2013 Yes Version 14.0.3 
(2013 SP1 Update 3) 
Visual Studio 2013 No Lack of constexpr support 
Visual Studio 14 Almost Not far, lack init of arrays 
CTP3 tested 
45
White paper 
46
Credits 
47 
• I take inspiration from 
many sources 
• In particular: 
• Samuel Neves & Filipe 
Araujo 
• LeFF (virus maker)
Infos 
• @AndrivetSeb sebastien@andrivet.com 
• All code presented here is available on GitHub 
• https://github.com/andrivet/ADVobfuscator 
• Contains 
• obfuscator (in source) 
• examples 
• Whitepaper 
• BSD 3-clauses license 
48
Thank you 
Questions? 
49

More Related Content

PDF
Hacking the swisscom modem
PDF
Killing any security product … using a Mimikatz undocumented feature
PDF
Reverse engineering Swisscom's Centro Grande Modem
PDF
Zn task - defcon russia 20
PPTX
Cisco IOS shellcode: All-in-one
PDF
iCloud keychain
PDF
Specializing the Data Path - Hooking into the Linux Network Stack
PDF
Vm ware fuzzing - defcon russia 20
Hacking the swisscom modem
Killing any security product … using a Mimikatz undocumented feature
Reverse engineering Swisscom's Centro Grande Modem
Zn task - defcon russia 20
Cisco IOS shellcode: All-in-one
iCloud keychain
Specializing the Data Path - Hooking into the Linux Network Stack
Vm ware fuzzing - defcon russia 20

What's hot (20)

PDF
Csw2016 gawlik bypassing_differentdefenseschemes
PDF
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
PDF
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
PDF
Advanced cfg bypass on adobe flash player 18 defcon russia 23
PDF
Csw2016 wheeler barksdale-gruskovnjak-execute_mypacket
PDF
Debugging TV Frame 0x09
PDF
Pledge in OpenBSD
PDF
Linux seccomp(2) vs OpenBSD pledge(2)
PDF
Osol Pgsql
PDF
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...
PPTX
Humantalk Angers 14 Mars
PPTX
System Calls
PDF
44CON London - Attacking VxWorks: from Stone Age to Interstellar
PDF
The true story_of_hello_world
PDF
CSW2017 Amanda rousseau cansecwest2017_net_hijacking_powershell
PDF
Feldo: Function Event Listing and Dynamic Observing for Detecting and Prevent...
ODP
Proxy arp
PDF
A little systemtap
PDF
The origin: Init (compact version)
PDF
Meltdown & Spectre
Csw2016 gawlik bypassing_differentdefenseschemes
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Csw2016 gong pwn_a_nexus_device_with_a_single_vulnerability
Advanced cfg bypass on adobe flash player 18 defcon russia 23
Csw2016 wheeler barksdale-gruskovnjak-execute_mypacket
Debugging TV Frame 0x09
Pledge in OpenBSD
Linux seccomp(2) vs OpenBSD pledge(2)
Osol Pgsql
Migrating KSM page causes the VM lock up as the KSM page merging list is too ...
Humantalk Angers 14 Mars
System Calls
44CON London - Attacking VxWorks: from Stone Age to Interstellar
The true story_of_hello_world
CSW2017 Amanda rousseau cansecwest2017_net_hijacking_powershell
Feldo: Function Event Listing and Dynamic Observing for Detecting and Prevent...
Proxy arp
A little systemtap
The origin: Init (compact version)
Meltdown & Spectre
Ad

Viewers also liked (20)

PDF
PDF
3 d pie chart circular with hole in center 12 stages powerpoint presentation ...
PPTX
Your big idea (1)
PDF
User experience design.
PDF
ENTREPRENEURIAL OPPORTUNITY E X P L O I T A T I O N A N D T H E F A M I L Y
PPT
創意跟行銷
PPTX
Team Building in Sydney - 7 Important Insights with Beyond the Boardroom
PPTX
Social Learning no dia a dia dos usuários
PPT
Sebi compliances and penalty 13.11.05 jaipur
PPTX
Insanity: The Chase
PDF
Xero certified Advisor
PPT
070หลวงพี่เอี้ยงตอนการให้ทานที่ได้บุญมากกว่า
PDF
A Primer - Comparing Japanese, Australian, Dutch and UK RMBS and Mortgage Mar...
PDF
Ericsson Mobility Report, November 2015 - Regional report North East Asia
KEY
iPods & Podcasting for Whole Language Instruction
DOCX
Anas Wardi En 12
PPTX
Hiring Hacks For Founders
PDF
Herramientas para crear videotutoriales
PPT
фотомонтаж
PDF
10 Superpowers of the World's Greatest Social Media Marketer
3 d pie chart circular with hole in center 12 stages powerpoint presentation ...
Your big idea (1)
User experience design.
ENTREPRENEURIAL OPPORTUNITY E X P L O I T A T I O N A N D T H E F A M I L Y
創意跟行銷
Team Building in Sydney - 7 Important Insights with Beyond the Boardroom
Social Learning no dia a dia dos usuários
Sebi compliances and penalty 13.11.05 jaipur
Insanity: The Chase
Xero certified Advisor
070หลวงพี่เอี้ยงตอนการให้ทานที่ได้บุญมากกว่า
A Primer - Comparing Japanese, Australian, Dutch and UK RMBS and Mortgage Mar...
Ericsson Mobility Report, November 2015 - Regional report North East Asia
iPods & Podcasting for Whole Language Instruction
Anas Wardi En 12
Hiring Hacks For Founders
Herramientas para crear videotutoriales
фотомонтаж
10 Superpowers of the World's Greatest Social Media Marketer
Ad

Similar to App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obfuscation (20)

PDF
Binary code obfuscation through c++ template meta programming
PPTX
Return of c++
PDF
c++-language-1208539706757125-9.pdf
PPT
Basics of objective c
PDF
The Renaissance of C++
PPTX
Whats New in Visual Studio 2012 for C++ Developers
PPTX
C++ Generators and Property-based Testing
PDF
Basic c++ 11/14 for python programmers
PPTX
Multiple file programs, inheritance, templates
PDF
Programming in C 2nd Edition Safari download pdf
PDF
Mastering Modern C++: C++11, C++14, C++17, C++20, C++23
DOCX
PDF
Download full ebook of Programming in C 2nd Edition Safari instant download pdf
PDF
c++.pdf
PPTX
Silicon Valley Code Camp - Do you C what I C
PPTX
Introduction to c_plus_plus
PPTX
Introduction to c_plus_plus (6)
PDF
C++ Training
PPTX
C++ lectures all chapters in one slide.pptx
PPTX
C++ language
Binary code obfuscation through c++ template meta programming
Return of c++
c++-language-1208539706757125-9.pdf
Basics of objective c
The Renaissance of C++
Whats New in Visual Studio 2012 for C++ Developers
C++ Generators and Property-based Testing
Basic c++ 11/14 for python programmers
Multiple file programs, inheritance, templates
Programming in C 2nd Edition Safari download pdf
Mastering Modern C++: C++11, C++14, C++17, C++20, C++23
Download full ebook of Programming in C 2nd Edition Safari instant download pdf
c++.pdf
Silicon Valley Code Camp - Do you C what I C
Introduction to c_plus_plus
Introduction to c_plus_plus (6)
C++ Training
C++ lectures all chapters in one slide.pptx
C++ language

More from Cyber Security Alliance (20)

PDF
Bug Bounty @ Swisscom
PDF
Robots are among us, but who takes responsibility?
PDF
iOS malware: what's the risk and how to reduce it
PDF
Why huntung IoC fails at protecting against targeted attacks
PDF
Corporations - the new victims of targeted ransomware
PDF
Blockchain for Beginners
PDF
Le pentest pour les nuls #cybsec16
PDF
Introducing Man in the Contacts attack to trick encrypted messaging apps
PDF
Understanding the fundamentals of attacks
PDF
Rump : iOS patch diffing
PDF
An easy way into your sap systems v3.0
PDF
Easy public-private-keys-strong-authentication-using-u2 f
PDF
Create a-strong-two-factors-authentication-device-for-less-than-chf-100
PDF
Offline bruteforce attack on wi fi protected setup
PPTX
Warning Ahead: SecurityStorms are Brewing in Your JavaScript
PDF
Rump attaque usb_caralinda_fabrice
PDF
Operation emmental appsec
PDF
Colt sp sec2014_appsec-nf-vfinal
PDF
Asfws2014 tproxy
PDF
Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0
Bug Bounty @ Swisscom
Robots are among us, but who takes responsibility?
iOS malware: what's the risk and how to reduce it
Why huntung IoC fails at protecting against targeted attacks
Corporations - the new victims of targeted ransomware
Blockchain for Beginners
Le pentest pour les nuls #cybsec16
Introducing Man in the Contacts attack to trick encrypted messaging apps
Understanding the fundamentals of attacks
Rump : iOS patch diffing
An easy way into your sap systems v3.0
Easy public-private-keys-strong-authentication-using-u2 f
Create a-strong-two-factors-authentication-device-for-less-than-chf-100
Offline bruteforce attack on wi fi protected setup
Warning Ahead: SecurityStorms are Brewing in Your JavaScript
Rump attaque usb_caralinda_fabrice
Operation emmental appsec
Colt sp sec2014_appsec-nf-vfinal
Asfws2014 tproxy
Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Modernizing your data center with Dell and AMD
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Empathic Computing: Creating Shared Understanding
PPTX
MYSQL Presentation for SQL database connectivity
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Cloud computing and distributed systems.
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Understanding_Digital_Forensics_Presentation.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation_ Review paper, used for researhc scholars
Modernizing your data center with Dell and AMD
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
NewMind AI Monthly Chronicles - July 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Unlocking AI with Model Context Protocol (MCP)
Empathic Computing: Creating Shared Understanding
MYSQL Presentation for SQL database connectivity
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Cloud computing and distributed systems.
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf

App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obfuscation

  • 1. C+ +11 METAPROGRAMMING A P P L IED TO SOF TWARE OBFUSCAT ION SEBAST IEN ANDRIVET Application Security Forum - 2014 Western Switzerland 05-06 November 2014 Y-Parc / Yverdon-les-Bains http://www.appsec-forum.ch 1
  • 2. About me Senior Security Engineer a t SCRT (Swiss) Deve l o p e r a t ADVTOOLS (Swiss) Sebastien ANDRIVET Cyberfeminist & hack t i v i s t Reverse engineer Intel & ARM C + + , C , O b j - C , C # d e ve l o p e r Trainer ( iOS & Android appsec) 2
  • 4. Reverse engineering • Reverse engineering of an application if often like following the “white rabbit” • i.e. following string literals • Live demo • Reverse engineering of an application using IDA • Well-known MDM (Mobile Device Management) for iOS 4
  • 8. YES! It is also Katy Perry! • (almost) same semantics • obfuscated 8
  • 9. “Deliberate act of creating source or machine code difficult for humans to understand” –WIKIPEDIA, APRIL 2014 Obfuscation 9
  • 10. C++ templates • Example: Stack of objects OBJECT2 OBJECT1 • Push • Pop 10
  • 11. Without templates c l a s s Stack { v o i d p u s h (void* o b j e c t ) ; void* p o p ( ) ; } ; Stack singers; singers.push(britney); Stack apples; apples.push(macintosh); s i n g e r s apples • Reuse the same code (binary) • Only 1 instance of Stack class 11
  • 12. With C++ templates Stack<Singer> singers; singers.push(britney); Stack<Apple> apples; apples.push(macintosh); template<typename T> c l a s s Stack { Stack<Singers> Stack<Apples*> v o i d p u s h ( T o b j e c t ) ; T p o p ( ) ; s i n g e r s apples } ; 12
  • 13. With C++ templates Stack<Singer> singers; singers.push(britney); Stack<Apple> apples; apples.push(macintosh); Stack<Singers> Stack<Apples*> s i n g e r s apples 13
  • 14. C++ templates • Two instances of Stack class • One per type • Does not reuse code • By default • Permit optimisations based on types • For ex. reuse code for all pointers to objects • Type safety, verified at compile time 14
  • 15. Type safety • singers.push(apple); // compilation error • apples.push(U2); • may or may not work… 15
  • 16. Optimisation based on types • Generate different code based on types (template parameters) • Example: enable_if template<typename T> class MyClass { ... enable_if_t<is_pointer<T>::value, T> member_function(T t) { ... }; ... }; • member_function is only defined if T is a pointer type • (warning: C++14 code, not C++11) 16
  • 17. C++ metaprogramming • Programs that manipulate or produce programs • Subset of C++ • Turing-complete (~ full programming language) • Close to Functional programming • Part of C++ standards • Major enhancements in C++11 et C++14 17
  • 18. Application 1 - Strings literals obfuscation • original string is source code • original string in DEBUG builds • developer-friendly syntax • no trace of original string in compiled code in RELEASE builds 18
  • 19. 1st implementation template<int... Indexes> struct MetaString1 { constexpr MetaString1(const char* str) : buffer_ {encrypt(str[Indexes])...} { } const char* decrypt(); RUNTIME private: constexpr char encrypt(char c) const { return c ^ 0x55; } constexpr char decrypt(char c) const { return encrypt(c); } private: char buffer_[sizeof...(Indexes) + 1]; }; 19
  • 20. 1st implementation - Usage #define OBFUSCATED1(str) (MetaString1<0, 1, 2, 3, 4, 5>(str).decrypt()) cout << OBFUSCATED1(“Britney Spears”) << endl; 20
  • 21. 1st implementation - Problem • List of indexes is hard-coded • 0, 1, 2, 3, 4, 5 • As a consequence, strings are truncated! 21
  • 22. 2nd implementation • Generate a list of indexes with metaprogramming • C++14 introduces std:index_sequence • With C++11, we have to implement our own version • Very simplified • MakeIndex<N>::type generates: • Indexes<0, 1, 2, 3, …, N> 22
  • 23. 2nd implementation • Instead of: MetaString1<0, 1, 2, 3, 4, 5>(str) • we have: MetaString2<Make_Indexes<sizeof(str)-1>::type>(str) 23
  • 24. 2nd implementation - Usage cout << OBFUSCATED2(“Katy Perry”) << endl; • No more truncation 24
  • 25. 3rd implementation • In previous implementations, key is hard-coded constexpr char encrypt(char c) const { return c ^ 0x55; } • New template parameter for Key template<int... I, int K> struct MetaString3<Indexes<I...>, K> 25
  • 26. Generating (pseudo-) random numbers • C++11 includes <random>, but for runtime, not compile time • MetaRandom<N, M> N: Nth generated number M: Maximum value (excluded) • Linear congruential engine • Park-Miller (1988), “minimal standard” • Not exactly a uniform distribution (modulo operation) • Recursive 26
  • 27. Seed • template<> struct MetaRandomGenerator<0> { static const int value = seed; }; • How to choose an acceptable compile-time seed? • Macros (C & C++): • __TIME__: compilation time (standard) • __COUNTER__: incremented each time it is used (non-standard but well supported by compilers) 27
  • 28. 3rd implementation • Different key for each string • thanks to __COUNTER__ • Different keys for each compilation • thanks to __TIME__ • Sometimes not desirable: can give hints to attackers (differences between versions) 28
  • 29. 4th implementation • Different and random keys, great! • Why not go even further? • Choose a different encryption algorithm, randomly! 29
  • 30. 4th implementation • Template partial specialization • template<int A, int K, typename Indexes> struct MetaString4; • template<int K, int... I> struct MetaString4<0, K, Indexes<I...>> { … c ^ K … }; • template<int K, int... I> struct MetaString4<1, K, Indexes<I...>> { … c + K … }; • #define DEF_OBFUSCATED4(str) MetaString4<MetaRandom<__COUNTER__, 2>::value, … 30
  • 31. Result • Without obfuscation cout << "Britney Spears” << endl; • With obfuscation cout << OBFUSCATED4("Britney Spears") << endl; 31
  • 33. With obfuscation Encrypted characters (mixed with MOV) Decryption 33
  • 34. Application 2 - Obfuscate calls • How to obfuscate call such as: • function_to_protect(); • against static analysis (or even dynamic analysis)? 34
  • 35. Finite State Machine (simple example) 35
  • 36. Boost Meta State Machine (MSM) library types template parameter Compile time entity Generates code (FSM) at compile-time 36
  • 37. Result • Without obfuscation function_to_protect(“did”, “again”); • With obfuscation OBFUSCATED_CALL(function_to_protect, “did”, “again”); • Even better OBFUSCATED_CALL(function_to_protect, OBFUSCATED(“did”), OBFUSCATED(“again”)); 37
  • 40. Application 3: FSM + Debugger Detection • FSM • To fight against static analysis • Debugger detection • To fight against dynamic analysis 40
  • 41. Finite State Machine • Follows a different path depending of a predicate (Debugged or not Debugged, that is the question) 41
  • 42. More obfuscation • Obfuscate predicate result • Avoid simple “if”, too simple for reverse engineers • Make computation instead • If the example, counter is odd if predicate is false 42
  • 43. More obfuscation • Obfuscate function address • Otherwise, IDA is smart enough to get it • Simply make some computation on address • Using MetaRandom (like for strings obfuscation) 43
  • 44. Predicate • Debugger detection is only an example • In the example, implemented only for Mac OS X / iOS • Virtual environment detection • Jailbreak detection • Etc 44
  • 45. Compilers support Compiler Compatible Remark Apple LLVM 5.1 Yes Previous versions not tested Apple LLVM 6.0 Yes Xcode 6, 6.1 beta LLVM 3.4, 3.5 Yes Previous versions not tested GCC 4.8.2 or higher Yes Previous versions not tested Compile with -std=c++11 Intel C++ 2013 Yes Version 14.0.3 (2013 SP1 Update 3) Visual Studio 2013 No Lack of constexpr support Visual Studio 14 Almost Not far, lack init of arrays CTP3 tested 45
  • 47. Credits 47 • I take inspiration from many sources • In particular: • Samuel Neves & Filipe Araujo • LeFF (virus maker)
  • 48. Infos • @AndrivetSeb sebastien@andrivet.com • All code presented here is available on GitHub • https://github.com/andrivet/ADVobfuscator • Contains • obfuscator (in source) • examples • Whitepaper • BSD 3-clauses license 48