SlideShare a Scribd company logo
Dynamic
Binary
Instrumentation
Using Intel’s PIN
What is Instrumentation
● Inserting extra lines of code into the processes memory.
● Intel’s PIN, Google’s Address Sanitizer, DynamoRIO, Valgrind, GDB.
● Useful for reverse engineering and malware analysis.
Why DBI
…...
if (size < sizeof(min_buf)) {
iov_to_buf(iov, iovcnt, 0, min_buf, size);
memset(&min_buf[size], 0, sizeof(min_buf) - size);
}
else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
/* This is very unlikely, but may happen. */
iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
filter_buf = min_buf;
}
…...
….
if (size < sizeof(min_buf)) {
printf(“Good size branchn”);
iov_to_buf(iov, iovcnt, 0, min_buf, size);
memset(&min_buf[size], 0, sizeof(min_buf) - size);
}
else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
/* This is very unlikely, but may happen. */
printf(“Got a rare casen”);
iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
filter_buf = min_buf;
}
….
Installation guidelines
● https://labs.portcullis.co.uk/blog/an-introduction-to-binary-dynamic-
analysis/
● https://software.intel.
com/sites/landingpage/pintool/docs/76991/Pin/html/
Where’s the code?
● Comes with pre-existing scripts.
● Feature to add custom scripts.
● Written in C or C++.
● Examples in ~/pin/source/tools/SimpleExamples/
#include <iostream>
#include "pin.H"
UINT64 icount = 0;
VOID IncCounter() {
icount++;}
VOID Instruction(INS ins, VOID *v) {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)IncCounter, IARG_END);}
VOID Fini(INT32 code, VOID *v) {
std::cerr << "Count " << icount << endl;}
int main(int argc, char * argv[]) {
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(Fini, 0);
PIN_StartProgram();
return 0;}
Analysis
Instrumentation
Execution
$ pin -t inscount.so -- /bin/ls
inscount.cpp inscount.so inscount.o
Count 422838 Output of inscount
$
Detecting Heap bugs
● Keep list of used and free chunks.
● If input is read to any of these chunks, check sizes and number of bytes
being read.
● In case of structure objects, check for input being read to an address
inside a chunk.
● If input is read to free chunk => UAF.
● If number of bytes read > size of chunk => heap overflow.
● If chunk_start + size - address < number of bytes => heap overflow.
The code
● Heap_trace.cpp
● Need to check for other functions like scanf(), strncpy(), memcpy() etc.
● Alerts:
○ When the same chunk is returned by malloc more than once.
○ When the same chunk is going to be freed more than once.
○ When input crosses chunk boundaries.
○ When input is copied to free chunks.
● Around 200 lines of code (excluding the nice comments).
● Let’s have a look.
● Demo1
C or C++ ? That’s it?
● Kudos to the owners.
● Blankwall - Python Pin.
● A python wrapper to PIN.
● Not yet complete.
import sys, pin
total = 0
info = file("inscount.out", "w")
def counter(trace_addr):
global total
x = pin.TRACE_BblHead(trace_addr)
y = pin.BBL_Address(x)
instrucs = pin.BBL_NumIns(x)
total += instrucs
info.write("Basic Block @ %x SIZE: %x NUM INS= IN BLOCK: %x TOTAL: %xn" % (y, pin.BBL_Size
(x), instrucs, total ))
pin.TRACE_AddInstrumentFunction(counter)
$ pin -t obj-intel64/Python_Pin.so -m ins_count.py -- /bin/ls
$ cat inscount.out|head
Basic Block @ 7ffff7ddb2d0 SIZE: 8 NUM INS= IN BLOCK: 2 TOTAL: 2
Basic Block @ 7ffff7ddea40 SIZE: 55 NUM INS= IN BLOCK: 16 TOTAL: 18
Basic Block @ 7ffff7ddeaef SIZE: 6 NUM INS= IN BLOCK: 2 TOTAL: 1a
Basic Block @ 7ffff7ddead8 SIZE: 17 NUM INS= IN BLOCK: 6 TOTAL: 20
Heap_Tracer.py ?
● 90 lines of code. ( Hurray! )
● Let’s take a look.
● Demo2

More Related Content

What's hot (20)

PPTX
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
securityxploded
 
PPTX
Advanced malware analysis training session5 reversing automation
Cysinfo Cyber Security Community
 
PPT
Buffer Overflows
Sumit Kumar
 
PPTX
Control hijacking
Prachi Gulihar
 
PPTX
Buffer overflow attacks
Japneet Singh
 
PPTX
Anatomy of a Buffer Overflow Attack
Rob Gillen
 
PPTX
Advanced malwareanalysis training session2 botnet analysis part1
Cysinfo Cyber Security Community
 
PDF
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
Tyler Shields
 
PPT
6 buffer overflows
drewz lin
 
PPTX
CodeChecker summary 21062021
Olivera Milenkovic
 
PPT
Buffer Overflow Attacks
harshal kshatriya
 
PPTX
Reversing malware analysis training part3 windows pefile formatbasics
Cysinfo Cyber Security Community
 
PDF
Dynamic PHP web-application analysis
ax330d
 
PPTX
Reversing malware analysis training part6 practical reversing
Cysinfo Cyber Security Community
 
PPTX
08 - Return Oriented Programming, the chosen one
Alexandre Moneger
 
PDF
Course lecture - An introduction to the Return Oriented Programming
Jonathan Salwan
 
PPTX
CodeChecker Overview Nov 2019
Olivera Milenkovic
 
PPTX
Advanced malware analysis training session4 anti-analysis techniques
Cysinfo Cyber Security Community
 
PDF
Possibility of arbitrary code execution by Step-Oriented Programming
kozossakai
 
PPTX
Return oriented programming (ROP)
Pipat Methavanitpong
 
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
securityxploded
 
Advanced malware analysis training session5 reversing automation
Cysinfo Cyber Security Community
 
Buffer Overflows
Sumit Kumar
 
Control hijacking
Prachi Gulihar
 
Buffer overflow attacks
Japneet Singh
 
Anatomy of a Buffer Overflow Attack
Rob Gillen
 
Advanced malwareanalysis training session2 botnet analysis part1
Cysinfo Cyber Security Community
 
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
Tyler Shields
 
6 buffer overflows
drewz lin
 
CodeChecker summary 21062021
Olivera Milenkovic
 
Buffer Overflow Attacks
harshal kshatriya
 
Reversing malware analysis training part3 windows pefile formatbasics
Cysinfo Cyber Security Community
 
Dynamic PHP web-application analysis
ax330d
 
Reversing malware analysis training part6 practical reversing
Cysinfo Cyber Security Community
 
08 - Return Oriented Programming, the chosen one
Alexandre Moneger
 
Course lecture - An introduction to the Return Oriented Programming
Jonathan Salwan
 
CodeChecker Overview Nov 2019
Olivera Milenkovic
 
Advanced malware analysis training session4 anti-analysis techniques
Cysinfo Cyber Security Community
 
Possibility of arbitrary code execution by Step-Oriented Programming
kozossakai
 
Return oriented programming (ROP)
Pipat Methavanitpong
 

Viewers also liked (20)

PDF
Format string vunerability
Cysinfo Cyber Security Community
 
PDF
Buffer overflow Attacks
Cysinfo Cyber Security Community
 
PPTX
Dll preloading-attack
Cysinfo Cyber Security Community
 
PPTX
Watering hole attacks case study analysis
Cysinfo Cyber Security Community
 
PPTX
Dissecting Android APK
Cysinfo Cyber Security Community
 
PPTX
Homomorphic encryption
Cysinfo Cyber Security Community
 
PPTX
Reversing malware analysis training part11 exploit development advanced
Cysinfo Cyber Security Community
 
PPTX
Advanced malware analysis training session11 part2 dissecting the heart beat ...
Cysinfo Cyber Security Community
 
PPTX
Investigating Malware using Memory Forensics
Cysinfo Cyber Security Community
 
PPTX
Advanced malware analysis training session3 botnet analysis part2
Cysinfo Cyber Security Community
 
PPTX
Reversing malware analysis training part10 exploit development basics
Cysinfo Cyber Security Community
 
ODP
Introduction to Binary Exploitation
Cysinfo Cyber Security Community
 
PPTX
Exploits & Mitigations - Memory Corruption Techniques
Cysinfo Cyber Security Community
 
PDF
POS Malware: Is your Debit/Credit Transcations Secure?
Cysinfo Cyber Security Community
 
PPTX
Introduction to ICS/SCADA security
Cysinfo Cyber Security Community
 
PDF
Understanding APT1 malware techniques using malware analysis and reverse engi...
Cysinfo Cyber Security Community
 
PPTX
Hunting rootkit from dark corners of memory
Cysinfo Cyber Security Community
 
PPTX
XXE - XML External Entity Attack
Cysinfo Cyber Security Community
 
PDF
Linux Malware Analysis
Cysinfo Cyber Security Community
 
Format string vunerability
Cysinfo Cyber Security Community
 
Buffer overflow Attacks
Cysinfo Cyber Security Community
 
Dll preloading-attack
Cysinfo Cyber Security Community
 
Watering hole attacks case study analysis
Cysinfo Cyber Security Community
 
Dissecting Android APK
Cysinfo Cyber Security Community
 
Homomorphic encryption
Cysinfo Cyber Security Community
 
Reversing malware analysis training part11 exploit development advanced
Cysinfo Cyber Security Community
 
Advanced malware analysis training session11 part2 dissecting the heart beat ...
Cysinfo Cyber Security Community
 
Investigating Malware using Memory Forensics
Cysinfo Cyber Security Community
 
Advanced malware analysis training session3 botnet analysis part2
Cysinfo Cyber Security Community
 
Reversing malware analysis training part10 exploit development basics
Cysinfo Cyber Security Community
 
Introduction to Binary Exploitation
Cysinfo Cyber Security Community
 
Exploits & Mitigations - Memory Corruption Techniques
Cysinfo Cyber Security Community
 
POS Malware: Is your Debit/Credit Transcations Secure?
Cysinfo Cyber Security Community
 
Introduction to ICS/SCADA security
Cysinfo Cyber Security Community
 
Understanding APT1 malware techniques using malware analysis and reverse engi...
Cysinfo Cyber Security Community
 
Hunting rootkit from dark corners of memory
Cysinfo Cyber Security Community
 
XXE - XML External Entity Attack
Cysinfo Cyber Security Community
 
Linux Malware Analysis
Cysinfo Cyber Security Community
 
Ad

Similar to Dynamic Binary Instrumentation (20)

PPTX
Using Pin++ to Author Highly Configurable Pintools for Pin
James Hill
 
PPTX
Изучаем миллиард состояний программы на уровне профи. Как разработать быстрый...
Positive Hack Days
 
PPTX
Exploring billion states of a program like a pro. How to cook your own fast a...
Maksim Shudrak
 
PDF
HES 2011 - Gal Diskin - Binary instrumentation for hackers - Lightning-talk
Hackito Ergo Sum
 
PDF
Dmitriy D1g1 Evdokimov - DBI Intro
DefconRussia
 
PDF
WCTF 2018 binja Editorial
Charo_IT
 
PDF
Effective Memory Protection Using Dynamic Tainting (ASE 2007)
James Clause
 
PDF
printf tricks
Shaun Colley
 
PDF
Appsec obfuscator reloaded
Cyber Security Alliance
 
PDF
Leakpoint: Pinpointing the Causes of Memory Leaks (ICSE 2010)
James Clause
 
PPTX
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
Andrei Alexandrescu
 
PDF
The walking 0xDEAD
Carlos Garcia Prado
 
PDF
Given below is the completed code along with comments. Output of the.pdf
aparnacollection
 
PDF
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
RootedCON
 
PDF
Kernel Recipes 2019 - Hunting and fixing bugs all over the Linux kernel
Anne Nicolas
 
PDF
CNIT 127: Ch 8: Windows overflows (Part 2)
Sam Bowne
 
PPT
Security Applications For Emulation
Silvio Cesare
 
PDF
Heap Base Exploitation
UTD Computer Security Group
 
PPT
Buffer OverFlow
Rambabu Duddukuri
 
PDF
Davide Berardi - Linux hardening and security measures against Memory corruption
linuxlab_conf
 
Using Pin++ to Author Highly Configurable Pintools for Pin
James Hill
 
Изучаем миллиард состояний программы на уровне профи. Как разработать быстрый...
Positive Hack Days
 
Exploring billion states of a program like a pro. How to cook your own fast a...
Maksim Shudrak
 
HES 2011 - Gal Diskin - Binary instrumentation for hackers - Lightning-talk
Hackito Ergo Sum
 
Dmitriy D1g1 Evdokimov - DBI Intro
DefconRussia
 
WCTF 2018 binja Editorial
Charo_IT
 
Effective Memory Protection Using Dynamic Tainting (ASE 2007)
James Clause
 
printf tricks
Shaun Colley
 
Appsec obfuscator reloaded
Cyber Security Alliance
 
Leakpoint: Pinpointing the Causes of Memory Leaks (ICSE 2010)
James Clause
 
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
Andrei Alexandrescu
 
The walking 0xDEAD
Carlos Garcia Prado
 
Given below is the completed code along with comments. Output of the.pdf
aparnacollection
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
RootedCON
 
Kernel Recipes 2019 - Hunting and fixing bugs all over the Linux kernel
Anne Nicolas
 
CNIT 127: Ch 8: Windows overflows (Part 2)
Sam Bowne
 
Security Applications For Emulation
Silvio Cesare
 
Heap Base Exploitation
UTD Computer Security Group
 
Buffer OverFlow
Rambabu Duddukuri
 
Davide Berardi - Linux hardening and security measures against Memory corruption
linuxlab_conf
 
Ad

More from Cysinfo Cyber Security Community (20)

PDF
Understanding Malware Persistence Techniques by Monnappa K A
Cysinfo Cyber Security Community
 
PDF
Understanding & analyzing obfuscated malicious web scripts by Vikram Kharvi
Cysinfo Cyber Security Community
 
PDF
Getting started with cybersecurity through CTFs by Shruti Dixit & Geethna TK
Cysinfo Cyber Security Community
 
PPTX
Emerging Trends in Cybersecurity by Amar Prusty
Cysinfo Cyber Security Community
 
PDF
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
Cysinfo Cyber Security Community
 
PDF
Closer look at PHP Unserialization by Ashwin Shenoi
Cysinfo Cyber Security Community
 
PDF
Unicorn: The Ultimate CPU Emulator by Akshay Ajayan
Cysinfo Cyber Security Community
 
PDF
The Art of Executing JavaScript by Akhil Mahendra
Cysinfo Cyber Security Community
 
PDF
Reversing and Decrypting Malware Communications by Monnappa
Cysinfo Cyber Security Community
 
PPTX
DeViL - Detect Virtual Machine in Linux by Sreelakshmi
Cysinfo Cyber Security Community
 
PPTX
Analysis of android apk using adhrit by Abhishek J.M
Cysinfo Cyber Security Community
 
PDF
Understanding evasive hollow process injection techniques monnappa k a
Cysinfo Cyber Security Community
 
PPTX
Security challenges in d2d communication by ajithkumar vyasarao
Cysinfo Cyber Security Community
 
PPTX
S2 e (selective symbolic execution) -shivkrishna a
Cysinfo Cyber Security Community
 
PPTX
Dynamic binary analysis using angr siddharth muralee
Cysinfo Cyber Security Community
 
PPTX
Bit flipping attack on aes cbc - ashutosh ahelleya
Cysinfo Cyber Security Community
 
PDF
Security Analytics using ELK stack
Cysinfo Cyber Security Community
 
PDF
ATM Malware: Understanding the threat
Cysinfo Cyber Security Community
 
PPT
Image (PNG) Forensic Analysis
Cysinfo Cyber Security Community
 
PPT
Malware Detection using Machine Learning
Cysinfo Cyber Security Community
 
Understanding Malware Persistence Techniques by Monnappa K A
Cysinfo Cyber Security Community
 
Understanding & analyzing obfuscated malicious web scripts by Vikram Kharvi
Cysinfo Cyber Security Community
 
Getting started with cybersecurity through CTFs by Shruti Dixit & Geethna TK
Cysinfo Cyber Security Community
 
Emerging Trends in Cybersecurity by Amar Prusty
Cysinfo Cyber Security Community
 
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
Cysinfo Cyber Security Community
 
Closer look at PHP Unserialization by Ashwin Shenoi
Cysinfo Cyber Security Community
 
Unicorn: The Ultimate CPU Emulator by Akshay Ajayan
Cysinfo Cyber Security Community
 
The Art of Executing JavaScript by Akhil Mahendra
Cysinfo Cyber Security Community
 
Reversing and Decrypting Malware Communications by Monnappa
Cysinfo Cyber Security Community
 
DeViL - Detect Virtual Machine in Linux by Sreelakshmi
Cysinfo Cyber Security Community
 
Analysis of android apk using adhrit by Abhishek J.M
Cysinfo Cyber Security Community
 
Understanding evasive hollow process injection techniques monnappa k a
Cysinfo Cyber Security Community
 
Security challenges in d2d communication by ajithkumar vyasarao
Cysinfo Cyber Security Community
 
S2 e (selective symbolic execution) -shivkrishna a
Cysinfo Cyber Security Community
 
Dynamic binary analysis using angr siddharth muralee
Cysinfo Cyber Security Community
 
Bit flipping attack on aes cbc - ashutosh ahelleya
Cysinfo Cyber Security Community
 
Security Analytics using ELK stack
Cysinfo Cyber Security Community
 
ATM Malware: Understanding the threat
Cysinfo Cyber Security Community
 
Image (PNG) Forensic Analysis
Cysinfo Cyber Security Community
 
Malware Detection using Machine Learning
Cysinfo Cyber Security Community
 

Recently uploaded (20)

PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 

Dynamic Binary Instrumentation

  • 2. What is Instrumentation ● Inserting extra lines of code into the processes memory. ● Intel’s PIN, Google’s Address Sanitizer, DynamoRIO, Valgrind, GDB. ● Useful for reverse engineering and malware analysis.
  • 3. Why DBI …... if (size < sizeof(min_buf)) { iov_to_buf(iov, iovcnt, 0, min_buf, size); memset(&min_buf[size], 0, sizeof(min_buf) - size); } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { /* This is very unlikely, but may happen. */ iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); filter_buf = min_buf; } …...
  • 4. …. if (size < sizeof(min_buf)) { printf(“Good size branchn”); iov_to_buf(iov, iovcnt, 0, min_buf, size); memset(&min_buf[size], 0, sizeof(min_buf) - size); } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { /* This is very unlikely, but may happen. */ printf(“Got a rare casen”); iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); filter_buf = min_buf; } ….
  • 5. Installation guidelines ● https://labs.portcullis.co.uk/blog/an-introduction-to-binary-dynamic- analysis/ ● https://software.intel. com/sites/landingpage/pintool/docs/76991/Pin/html/
  • 6. Where’s the code? ● Comes with pre-existing scripts. ● Feature to add custom scripts. ● Written in C or C++. ● Examples in ~/pin/source/tools/SimpleExamples/
  • 7. #include <iostream> #include "pin.H" UINT64 icount = 0; VOID IncCounter() { icount++;} VOID Instruction(INS ins, VOID *v) { INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)IncCounter, IARG_END);} VOID Fini(INT32 code, VOID *v) { std::cerr << "Count " << icount << endl;} int main(int argc, char * argv[]) { PIN_Init(argc, argv); INS_AddInstrumentFunction(Instruction, 0); PIN_AddFiniFunction(Fini, 0); PIN_StartProgram(); return 0;} Analysis Instrumentation
  • 8. Execution $ pin -t inscount.so -- /bin/ls inscount.cpp inscount.so inscount.o Count 422838 Output of inscount $
  • 9. Detecting Heap bugs ● Keep list of used and free chunks. ● If input is read to any of these chunks, check sizes and number of bytes being read. ● In case of structure objects, check for input being read to an address inside a chunk. ● If input is read to free chunk => UAF. ● If number of bytes read > size of chunk => heap overflow. ● If chunk_start + size - address < number of bytes => heap overflow.
  • 10. The code ● Heap_trace.cpp ● Need to check for other functions like scanf(), strncpy(), memcpy() etc. ● Alerts: ○ When the same chunk is returned by malloc more than once. ○ When the same chunk is going to be freed more than once. ○ When input crosses chunk boundaries. ○ When input is copied to free chunks. ● Around 200 lines of code (excluding the nice comments). ● Let’s have a look. ● Demo1
  • 11. C or C++ ? That’s it? ● Kudos to the owners. ● Blankwall - Python Pin. ● A python wrapper to PIN. ● Not yet complete.
  • 12. import sys, pin total = 0 info = file("inscount.out", "w") def counter(trace_addr): global total x = pin.TRACE_BblHead(trace_addr) y = pin.BBL_Address(x) instrucs = pin.BBL_NumIns(x) total += instrucs info.write("Basic Block @ %x SIZE: %x NUM INS= IN BLOCK: %x TOTAL: %xn" % (y, pin.BBL_Size (x), instrucs, total )) pin.TRACE_AddInstrumentFunction(counter)
  • 13. $ pin -t obj-intel64/Python_Pin.so -m ins_count.py -- /bin/ls $ cat inscount.out|head Basic Block @ 7ffff7ddb2d0 SIZE: 8 NUM INS= IN BLOCK: 2 TOTAL: 2 Basic Block @ 7ffff7ddea40 SIZE: 55 NUM INS= IN BLOCK: 16 TOTAL: 18 Basic Block @ 7ffff7ddeaef SIZE: 6 NUM INS= IN BLOCK: 2 TOTAL: 1a Basic Block @ 7ffff7ddead8 SIZE: 17 NUM INS= IN BLOCK: 6 TOTAL: 20
  • 14. Heap_Tracer.py ? ● 90 lines of code. ( Hurray! ) ● Let’s take a look. ● Demo2