source code analysis and application security - owasp › images › f › fc ›...

29
Source Code Analysis and Application Security Maty SIMAN, CISSP Founder, CTO Checkmarx [email protected]

Upload: others

Post on 25-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Source Code Analysis and Application Security

Maty SIMAN, CISSPFounder, CTOCheckmarx

[email protected]

Page 2: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Agenda

• Source Code Analysis• SCA and Security• Demo

http://oeronline.com/kk-cartoon/images/april_2006bg.jpg

Page 3: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Source Code Analysis

“Static code analysis is the analysis of computer software that is performed without actually executing programs…”

Wikipedia

Page 4: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Source code analysis

Two types• Simple – code as text• Complex – intermediate representation

http://web.uvic.ca/~mcindoe/simple.jpg

Page 5: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Source Code Analysis

• Text– Finding using RegEx– Some metrics“When you can measureWhen you can measure what you are speaking about,

and express it into numbers, you know somethingyou know somethingabout it; but when you cannot measure it, when you cannot express it in numbers, your knowledge is of a meager and unsatisfactory kind: It may be the beginning of knowledge, but you have scarcely in your thoughts advanced to the stage of science.”

Lord Kelvin (1824-1904)

Page 6: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Source Code Analysis

• Text– Finding using RegEx– Some metrics– Coding standards

• Microsoft style:if (2 == a) ….

Page 7: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Intermediate representation

• Call Graph• CFG• CDG • DFG• Slicing

Page 8: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Call graph

• Directed graph• Each vertex represents either calling or called

element (class, method, page)

• Vertex A is connected to vertex B using the directed edge E iff A calls B

void A (){

B();}

void B (){

B();}

A B

Page 9: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Control Flow Graph

• Each vertex in the graph represents a basic block – a straight line of code without any jumps or jumps targets (it is guaranteed that all commands within a block are executed in their order).

• Directed edges are used to represent jumps in the control flow.

printf (“%d”, a)printf (“%d”, b)printf (“Max: %d”, c)

c = b;

a = 3;b = 5;c = a;If (b > a)

Page 10: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Intermediate representation

• CFG

void main(){

int var1 = 5;int var2 = 6;goto label;int deadVar1 = 1;

label:var1 = 6;if (var1 > var2){

--var1;}else{

++var1;}goto label;

}

Page 11: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Control Dependence Graph

• Enhances CFG.• Each LOC has its own vertex• Edge B is directed by edge A iff the

execution if B depends on the execution of A

Example

Page 12: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Control Dependence Graph (cont.)

void main(){

int j = 0; int i = 0;

while (i < 10){if (i == 3){

j=j*2;}j = j + i;i = i + 1;

}

printf ("%d\n", j);printf ("%d,n", i);

}

Page 13: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Data Flow Graph

• Represents the flow of data through code.• Each LOC has its own vertex (like CDG).• Edge represents direct influence of data

in the source vertex on the data in the destination vertex (therefore, assignment statements are source vertexes)

Example

Page 14: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Control Dependence Graph (cont.)

void main(){

int j = 0; int i = 0;

while (i < 10){if (i == 3){

j=j*2;}j = j + i;i = i + 1;

}

printf ("%d\n", j);printf ("%d,n", i);

}

Page 15: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Slicing

• Finding a relevant subset of the application

void main() {

int sum = 0;int i = 1;while (i < 11) {

sum = sum + i;i = i + 1;

}printf (“%d\n”, sum);printf (“%d\n”, i);

}

Page 16: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Slicing (cont.)

• Finding a relevant subset of the application

void main() void main() {{

int sum = 0;intint i = 1;i = 1;while (i < 11) while (i < 11) {{

sum = sum + i;i = i + 1;i = i + 1;

}}printf (“%d\n”, sum);printfprintf ((““%%dd\\nn””, i);, i);

}}

Page 17: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

CDG

StartSum = 0

i = 1

While (i<11) Printf(sum) Printf(i)

++iSum +=i

Page 18: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

DFG

Sum = 0

i = 1

While (i<11) Printf(sum) Printf(i)

++iSum +=i

Page 19: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

(DFG+CDG)’

Sum = 0

i = 1

While (i<11) Printf(sum) Printf(i)

++iSum +=i

Page 20: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

(DFG+CDG)’

Sum = 0

i = 1

While (i<11) Printf(sum) Printf(i)

++iSum +=i

Page 21: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Virtual machine

• Static ?!

http://www.cartoonstock.com/lowres/iba0013l.jpg

Page 22: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Limitations

Page 23: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

More

• SAT Solvers• Parallel• Optimization• Migrations• .• ..• …

Page 24: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Related techniques

• Code review• Dynamic analysis• Testing• Debugging

Page 25: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Security

Page 26: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Securitypros/cons

• Penetration testing• Code review• WAF

Page 27: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some
Page 28: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some
Page 29: Source Code Analysis and Application Security - OWASP › images › f › fc › OWASP_IL_Source_Code_Analysis… · Source Code Analysis • Text – Finding using RegEx – Some

Man vs. Machine“Brain” intensive Time intensive