Secure coding in C and C++

Your application written in C and C++ works as intended, so you are done, right? But did you consider feeding in incorrect values? 16Gbs of data? A null? An apostrophe? Negative numbers, or specifically -2*32? Because that’s what the bad guys will do – and the list is far from complete. Handling security needs a healthy level of paranoia, and this is what this course provides: a strong emotional engagement by lots of hand on labs and stories from real life, all to substantially improve code hygiene. Mistakes, consequences and best practices are our blood, sweat and tears.

All this is put in the context of C and C++, and extended by core programming issues, discussing security pitfalls of code written in these languages So that you are prepared for the forces of the dark side. So that nothing unexpected happens. Nothing.

Outline:

  • Cybersecurity basics
  • Buffer overflow
  • Common software security weaknesses
  • Wrap up

Objective list:

  • Handling security challenges in your C and C++ code
  • Identify vulnerabilities and their consequences
  • Learn the security best practices in C and C++

Audience:

C/C++ developers

Prerequisites:

General C/C++ development

Course outline:

Cybersecurity basics

  • What is security?
  • Threat and risk
  • Cybersecurity threat types
  • Consequences of insecure software
  • Constraints and the market
  • The dark side

 

Buffer overflow

  • Assembly basics and calling conventions
    • ARM assembly essentials
    • Registers and addressing
    • Basic ARM64 instructions
    • ARM calling conventions
      • Calling convention – what it is all about
      • Calling conventions on ARM64
      • The stack frame
      • Stacked function calls
    • Memory management vulnerabilities
      • Memory management and security
      • Vulnerabilities in the real world
    • Buffer security issues
      • Buffer overflow on the stack
        • Buffer overflow on the stack – stack smashing
        • Exploitation – Hijacking the control flow
        • Lab – Buffer overflow 101, code reuse
        • Exploitation – Injecting shellcode
        • Lab – Code injection, BoF exploitation with a shellcode
      • Buffer overflow on the heap
        • An example exploitation
        • Case study – Heartbleed
      • Pointer manipulation
        • Modification of jump tables
        • Overwriting function pointers
      • Some typical mistakes leading to BoF
        • Off-by-one errors
        • Allocating nothing
        • String length calculation mistakes
        • String termination confusion
        • Other typical causes of BoF weaknesses
      • BoF protection best practices
        • Safe and unsafe functions
        • base_string and std::string
        • Some less-known dangerous functions
        • Lab – Fixing buffer overflow
        • Securing the toolchain
          • Securing the toolchain in C and C++
        • Compiler options and instrumentation
          • Compiler warnings and security
          • Using FORTIFY_SOURCE
          • Lab – Effects of FORTIFY
          • Using AddressSanitizer (ASan)
          • Hijacking GOT and RELRO protection
          • Heap overflow best practices
        • Stack smashing protection
          • Detecting BoF with a stack canary
          • Argument cloning
          • Stack smashing protection on various platforms
          • SSP changes to the prologue and epilogue
          • Lab – Effects of stack smashing protection
          • Bypassing stack smashing protection
        • Runtime protection
          • Runtime instrumentation
        • Address Space Layout Randomization (ASLR)
          • ASLR on various platforms
          • Lab – Effects of ASLR
          • Circumventing ASLR – NOP sleds
          • Heap spraying
        • Non-executable memory areas
          • The NX bit
          • Write XOR Execute (W^X)
          • NX on various platforms
          • Lab – Effects of NX
          • NX circumvention – Code reuse attacks
            • Return-to-libc / arc injection
            • Chained return-to-libc
            • Return Oriented Programming (ROP)
            • Lab – ROP demonstration
            • Whatever Oriented Programming
            • Protection against ROP

Buffer overflow

    • Return-to-libc attack – Circumventing the NX bit protection
      • Circumventing memory execution protection
      • Return-to-libc attack
    • Return oriented programming (ROP)
      • Exploiting with ROP
      • ROP gadgets
      • ROP mitigation
        • Mitigation techniques of ROP attack
    • Heap overflow
      • Memory allocation managed by a doubly-linked list
      • Buffer overflow on the heap
      • Steps of freeing and joining memory blocks
      • Freeing allocated memory blocks
      • Case study – Heartbleed
        • TLS Heartbeat Extension
        • Heartbleed – information leakage in OpenSSL
        • Heartbleed – fix in v1.0.1g
      • Protection against heap overflow

Practical cryptography

    • Rule #1 of implementing cryptography
    • Cryptosystems
      • Elements of a cryptosystem
    • Symmetric-key cryptography
      • Providing confidentiality with symmetric cryptography
      • Symmetric encryption algorithms
      • Modes of operation
    • Other cryptographic algorithms
      • Hash or message digest
      • Hash algorithms
      • SHAttered
      • Message Authentication Code (MAC)
      • Providing integrity and authenticity with a symmetric key
      • Random numbers and cryptography
      • Cryptographically-strong PRNGs
      • Hardware-based TRNGs
    • Asymmetric (public-key) cryptography
      • Providing confidentiality with public-key encryption
      • Rule of thumb – possession of private key
      • The RSA algorithm
        • Introduction to RSA algorithm
        • Encrypting with RSA
        • Combining symmetric and asymmetric algorithms
        • Digital signing with RSA
    • Public Key Infrastructure (PKI)
      • Man-in-the-Middle (MitM) attack
      • Digital certificates against MitM attack
      • Certificate Authorities in Public Key Infrastructure
      • X.509 digital certificate

XML security

    • XML injection
      • (Ab)using CDATA to store XSS payload in XML
      • Exercise – XML injection
      • Protection through sanitization and XML validation
    • Abusing XML Entity
      • XML Entity introduction
      • XML bomb
      • Exercise – XML bomb
      • XML external entity attack (XXE) – resource inclusion
      • XML external entity attack – URL invocation
      • XML external entity attack – parameter entities
      • Exercise – XXE attack
      • Preventing entity-related attacks
      • Case study – XXE in Google Toolbar

Common coding errors and vulnerabilities

    • Improper error and exception handling
      • Typical problems with error and exception handling
      • Empty catch block
      • Overly broad catch
      • Exercise ErrorHandling – spot the bug!
      • Exercise – Error handling
      • Case study – "e;#iamroot"e; authentication bypass in macOS
        • Authentication process in macOS (High Sierra)
        • Incorrect error handling in opendirectoryd
        • The #iamroot vulnerability (CVE-2017-13872)
    • Code quality problems
      • Dangers arising from poor code quality
      • Poor code quality – spot the bug!
      • Unreleased resources
      • Type mismatch – Spot the bug!
      • Exercise TypeMismatch
      • Memory allocation problems
        • Smart pointers
        • Zero length allocation
        • Double free
        • Mixing delete and delete[]
      • Use after free
        • Use after free – Instance of a class
        • Spot the bug
        • Use after free – Dangling pointers
      • Case study - WannaCry
        • The WannaCry ransomware
        • The vulnerability behind WannaCry – spot the bug!
        • Lessons learned

Common coding errors and vulnerabilities

    • Input validation
      • Input validation concepts
      • Integer problems
        • Representation of negative integers
        • Integer ranges
        • Integer overflow
        • Integer problems in C/C++
        • The integer promotion rule in C/C++
        • Arithmetic overflow – spot the bug!
        • Exercise IntOverflow
        • What is the value of abs(INT_MIN)?
        • Signedness bug – spot the bug!
        • Integer truncation – spot the bug!
        • Integer problem – best practices
        • Case study – Android Stagefright
      • Printf format string bug
        • Printf format strings
        • Printf format string bug – exploitation
        • Exercise Printf
        • Printf format string exploit – overwriting the return address
      • Mitigation of printf format string problem
      • Some other input validation problems
        • Array indexing – spot the bug!
        • Off-by-one and other null termination errors
        • The Unicode bug
      • Path traversal vulnerability
        • Path traversal – best practices
      • Log forging
        • Some other typical problems with log files
      • Case study - Shellshock
        • Shellshock – basics of using functions in bash
        • Shellshock – vulnerability in bash
        • Exercise - Shellshock
        • Shellshock fix and counterattacks
        • Exercise – Command override with environment variables
    • Improper use of security features
      • Typical problems related to the use of security features
      • Insecure randomness
        • Weak PRNGs in C and C++
        • Stronger PRNGs in C
      • Password management
        • Exercise – Weakness of hashed passwords
        • Password management and storage
        • Special purpose hash algorithms for password storage
        • Argon2 and PBKDF2 implementations in C/C++
        • bcrypt and scrypt implementations in C/C++
      • Case study – the Ashley Madison data breach
        • The loginkey token
        • Revealing the passwords with brute forcing
      • Typical mistakes in password management
      • Exercise – Hard coded passwords
    • Time and state problems
      • Time and state related problems
      • Serialization errors (TOCTTOU)
      • Attacks with symbolic links
      • Exercise TOCTTOU

Principles of security and secure coding

    • Matt Bishop’s principles of robust programming
    • The security principles of Saltzer and Schroeder

Knowledge sources

    • Secure coding sources – a starter kit
    • Vulnerability databases
    • Recommended books – C/C++

Other relevant courses

25. November
5 days
Classroom Virtual
2. December
5 days
Classroom Virtual
9. December
5 days
Classroom Virtual
16. December
3 days
Classroom Virtual