Static Analysis for
beginners
How you can find dragons
whoamy
●
Just another Programmer
●
Security Engineer
●
Ten years experience
●
About me: Github.com/CoolerVoid
Twitter: @Cooler_freenode
Contact: coolerlair@gmail.com
Etymology
Etymology
●
theguardian.com/technology/2011/oct/13/dennis-ritchie
●
economist.com/obituary/2011/11/05/dennis-ritchie-and-john-
mccarthy
Linux/Git creation
●
Version control
●
Manual Codereview
●
New tools for static analysis in Kernel or drivers to
make auto patch etc...
The root of study
The root of study
●
The Dragon Book
●
Flex
●
Bison
●
AST
●
Trees
●
Tokenizer
Security focus
●
Find Pitfalls
●
Fix each point
●
Mitigate
●
Sometimes its hard…
●
Education ???
Security focus
●
Find Pitfalls
●
Fix each point
●
Mitigate
●
Sometimes its hard…
●
Education ???
Security focus
●
Find Pitfalls
●
Fix each point
●
Mitigate
●
Sometimes its hard…
●
Education ???
Security focus
●
Mitre
●
OWASP
●
Other resources…
●
Owasp.org
●
cve.mitre.org
File system Pitfalls
●
File system problems
●
Call Open() but not call close()
●
Load config file, but don’t have lock…
●
Don’t check permissions to open file
●
Don’t check existence of file
●
Race condition (TOCTOU)
●
Mistake in permissions
Pitfall example 1
●
File system problems
●
Call Open() but not call close()
●
Load config file, but don’t have lock…
●
Don’t check permissions to open file
●
Don’t check existence of file
●
Race condition (TOCTOU)
●
Mistake in permissions
Pitfall example 1
●
This code example is noncompliant because the file opened by the
call to fopen() is not closed before function func() returns:
Pitfall example 1
●
This code example is right way, because open and close file:
Detection
●
Do you remember the dragon book ?
●
You can use DFA(Deterministic Finite Automaton) to solve this with
rank points.
●
You can tokenize each word and save in nodes, you can load data
structure and walk to collect each rule, the data structure you can
use Tree, AST, graph(this is common but more complex).
●
You can use Flex+Bison to generate input extractor and parser…
●
You can use regex(regular expression), but don’t have a good
performance! Its not better path!
●
Relax here! have other paths to following…
Detection
●
Do you remember the dragon book ?
●
You can use DFA(Deterministic Finite Automaton) to solve
this with rank points.
●
You can tokenize each word ans save in nodes, you can load data
structure and walk to collect each rule, the data structure you can
use Tree, AST, graph(this is common but more complex).
●
You can use Flex+Bison to generate input extractor and parse
rules…
●
You can use regex(regular expression), but don’t have a good
performance! Its not better path!
●
Relax here! have other paths to following…
Detection Ex 1
●
Its OK my choice is use Re2c to solve the problem!
●
Re2c is a free and open-source lexer generator for C, C++ and Go. It
compiles regular expressions to determinisitic finite automata
and encodes the automata in the form of a program in the target
language.
●
The main advantages of re2c are speed of the generated code
and a flexible user interface that allows one to adapt the generated
lexer to a particular environment and input model.
●
Re2c supports fast and lightweight submatch extraction with either
POSIX or leftmost greedy semantics.
Detection Ex 1
●
github.com/CoolerVoid/heap_detective/tree/master/doc/PoC1
●
doc/PoC1/rule.c
●
Rule to generate
●
Need Re2c tool
●
Need GCC
Detection Ex 1
●
github.com/CoolerVoid/heap_detective/tree/master/doc/PoC1
●
doc/PoC1/gen_re2.c
●
Rule to generate
●
Need Re2c tool
●
Need GCC
Detection Ex 1
●
github.com/CoolerVoid/heap_detective/tree/master/doc/PoC1
Detection Ex 1
●
github.com/CoolerVoid/heap_detective/tree/master/doc/PoC1/extest.c
Detection Ex 1
●
github.com/CoolerVoid/heap_detective/tree/master/doc/PoC1/test.sh
●
Note need re2c and gcc! (apt-get install re2c gcc)
Detection Ex 1
The logic its simple !
Detection Ex 1
●
github.com/CoolerVoid/heap_detective/tree/master/doc/PoC1/test.sh
●
Note need re2c and gcc! (apt-get install re2c gcc)
Detection
●
Do you remember the dragon book ?
●
You can use DFA(Deterministic Finite Automaton) to solve this with
rank points.
●
You can tokenize each word ans save in nodes, you can load data
structure and walk to collect each rule, the data structure you can
use Tree, AST, graph(this is common but more complex).
●
You can use Flex+Bison to generate input extractor and parse
rules…
●
You can use regex(regular expression), but don’t have a good
performance! Its not better path!
●
Relax here! have other paths to following…
●
Do you remember the dragon book ?
●
You can use DFA(Deterministic Finite Automaton) to solve this with
rank points.
●
You can tokenize each word ans save in nodes, you can load
data structure and walk to collect each rule, the data
structure you can use Tree, AST, graph(this is common but
more complex).
●
You can use Flex+Bison to generate input extractor and parse rules…
●
You can use regex(regular expression), but don’t have a good
performance! Its not better path!
●
Relax here! have other paths to following…
Detection
Heap detective
●
All languages uses heap memory
●
In C its commom when you use functions like malloc(), calloc(),
realloc(), strdup() etc…
●
In C++ its common when you use “new”.
●
Heap use can have a lot pitfalls if you not follow good practices.
●
Memory leak, double free, use after free, wild pointer, heap overflow,
crash(DoS) other pitfalls…
●
Some languages like Java have garbage collector to clean the heap
memory to manage this, but if programmer don’t know good
practices the problem with memory leak or crash can be found.
Heap detective
●
How you can map heap memory use ?
Heap detective
●
How you can map heap memory usage in static analysis ?
●
Use my Tool heap detective
●
https://github.com/CoolerVoid/heap_detective
Heap detective
●
How you can map heap memory use ?
●
List the functions that use heap memory
●
List functions to liberate heap
Overview
Heap detective
●
Heap detective
●
Heap detective
Heap detective
Cool stuff
●
My Tree library in C
●
Ice n-ary tree based on glib tree functions
●
https://github.com/CoolerVoid/icenarytree
Other projects
●
Code walk, code search, regex with rule based…
●
github.com/CoolerVoid/codewarrior
●
github.com/CoolerVoid/codecat
●
https://semgrep.dev (very cool)
Questions ?
Thank you!
Credits: CoolerVoid
Contact: coolerlair@gmail.com

Static analysis for beginners