mykhailo zarai "be careful when dealing with c++" at rivne it talks

33
Be careful when dealing with C/C++ Think Twice, Code Once Mykhailo Zarai (April 2017)

Upload: vadym-muliavka

Post on 15-Apr-2017

174 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Be careful when dealing with C/C++

Think Twice, Code Once

Mykhailo Zarai (April 2017)

Page 2: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Why do we care?Almost every day we hear about :•vulnerabilities•data breech

Page 3: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Vulnerabilities examples•Windows Remote Code execution (MS15-115)•NDIS Privilege of Elevation (MS15-17)•Kernel-Mode Drivers Privilege (MS15-135)

Page 4: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Data breach 2016•Apple Health Medicaid•Central Coast Credit Union•Commission on Elections•Department of Homeland Security

Page 5: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

What we are going to do?•Talk about secure programming•Programming toolbox•Some references and recommendations

Page 6: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Common Vulnerabilities•Buffer overflow• Integers•Null pointer dereferencingHomework:• Strings•Arrays• Exceptions

Page 7: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Look inside buffer overflow problem

Page 8: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Return Address

ESP - Extended Stack Pointer (topo)

Parent Routine Stack

EBP - Extended Base Pointer (base)

Char *bar

Char c[12]

Stac

k Gr

owth

Mem

ory Addresses

The data is put on reverse order onto buffer

Page 9: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Return Address

ESP - Extended Stack Pointer (topo)

Parent Routine Stack

EBP - Extended Base Pointer (base)

Char *bar

Char c[12]

Stac

k Gr

owth

Mem

ory AddressesH E L L O

H E L L O

H E L L O

H E L L O

H E L L O

BOOM!Buffer Overflow!

H E L L O

Page 10: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Return Address

ESP - Extended Stack Pointer (topo)

Parent Routine Stack

EBP - Extended Base Pointer (base)

Char *bar

Char c[12]

Stac

k Gr

owth

Mem

ory Addresses

Canary Word

Page 11: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Integers – Unsigned integer WrapMust not be allowed to wrap:• Integer operands of any point arithmetic and

array indexing• The assignment expression for declaration of a

variable length array• The postfix expression preceding square

brackets []• Function arguments of type size_t or rsize_t• In security-critical code

Page 12: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Integers – Unsigned integer WrapOperator

Wrap Operator

Wrap Operator

Wrap Operator

Wrap

+ Yes -= Yes << Yes < No

- Yes *= Yes >> No > No

* Yes /= No & No >= No

/ No %= No | No <= No

% No <<= Yes ^ No == No

++ Yes >>= No ~ No != No

-- Yes &= No ! No && No

= No |= No un + No || No

+= Yes ^= No un - Yes ?: No

Page 13: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Unsigned integer operation shouldn't wrap

Page 14: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Heap Buffer overflow in Mozilla SVGMultiplication of the signed int pen->num_vertices and the size_t value:

Page 15: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Heap Buffer overflow in Mozilla SVGCompliant solution:

Page 16: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Converting a pointer to integer or integer to pointerDo not convert a pointer type to an integer type if the result cannot be represented in the integer type (undefined behavior)

Page 17: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Converting a pointer to integer or integer to pointerCompliant solution: any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value.

Page 18: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Null pointer dereferencing (CWE-476)

Page 19: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

std::string::c_str() is being called on a temporary std::string object. The resulting pointer will point to released memory at the end of the assignment expression. Result is undefined behavior when accessing elements on that pointer

Page 20: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

In the compliant solution, a local copy of the string returned by str_func() is made to ensure that string str will be valid when the call display_string() is made.

Page 21: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

null pointer dereferencing

The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and not declared with the register storage-class specifier.

Page 22: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

MS C++ Security Features• /guard (Enable Control Flow Guard)• /GS (Buffer Security Check)• /SAFESEH (Image has Safe Exception

Handlers)• /NXCOMPAT (Data execution prevention

support)• /DYNAMICBASE (Use address space layout

randomization)(ASLR)

Page 23: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

GCC & Clang Security Features

Page 24: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Universal solution?

Page 25: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Toolbox• External code analysis tools:• PVS Studio• Cpp-Check• clang

•Windows application verifier•Reversing:• Radare2• IDA Pro

Page 26: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Application Verifier• Exceptions Stop Details - Ensures that applications do not hide access violations

using structured exception handling• Handles Stop Details - Tests to ensure the application is not attempting to use

invalid handles• Heaps Stop Details - Checks for memory corruptions issues in the heap• Input/Output Stop Details - Monitors the execution of asynchronous IO, and

performs various validations• Leak Stop Details - Detects leaks by tracking the resources made by a dll that

are not freed by the time the dll was unloaded• Locks Stop Details - Verifies the correct usage for critical sections• Memory Stop Details - Ensures APIs for virtual space manipulations are used

correctly (for example, VirtualAlloc, MapViewOfFile)• TLS Stop Details - Ensures that Thread Local Storage APIs are used correctly• Threadpool Stop Details - Ensures correct usage of threadpool APIs and

enforces consistency checks on worker-thread-states after a callback

Page 27: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

References - Double Agent• Attacking Antivirus & Next Generation Antivirus – Taking full control of any

antivirus by injecting code into it while bypassing all of its self-protection mechanism. The attack has been verified and works on all the major antiviruses including but not limited to: Avast, AVG, Avira, Bitdefender, Comodo, ESET, F-Secure, Kaspersky, Malwarebytes, McAfee, Norton, Panda, Quick Heal and Trend Micro.

• Installing Persistent Malware – Installing malware that can “survive” reboots and are automatically executed once the operating system boots.

• Hijacking Permissions – Hijacking the permissions of an existing trusted process to perform malicious operations in disguise of the trusted process. e.g. Exfiltrating data, C&C communication, lateral movement, stealing and encrypting sensitive data.

• Altering Process Behavior – Modifying the behavior of the process. e.g. Installing backdoors, weakening encryption algorithms, etc.

• Attacking Other Users/Sessions – Injecting code to processes of other users/sessions (SYSTEM/Admin/etc.).

Page 28: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

Application Verifier - Double AgentZero-Day Code Injection and Persistence Technique

https://cybellum.com/doubleagentzero-day-code-injection-and-persistence-technique/

Page 29: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

ReferencesSEI CERT C++ Coding Standardhttps://www.securecoding.cert.org

Page 30: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

ReferencesSecure Programming Cookbook for C and C++ Recipes for Cryptography, Authentication, Input Validation & MoreBy John Viega, Matt Messier

Page 31: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

ReferencesSecure Coding in C and C++ (2nd Edition) (SEI Series in Software Engineering) 2nd Edition by Robert C. Seacord

Page 32: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks

You can avoid all this painAsk this guy how to do it

Page 33: Mykhailo Zarai "Be careful when dealing with C++" at Rivne IT Talks