L3 - Secure Coding Techniques Flashcards
CWE
They help developers and security practitioners to:
*Describe and discuss software and hardware weaknesses in a common language.
* Check for weaknesses in existing software and hardware products.
* Evaluate coverage of tools targeting these weaknesses.
* Leverage a common baseline standard for weakness identification, mitigation, and prevention efforts.
* Prevent software and hardware vulnerabilities prior to deployment.
CWE 3 metric groups
Base Finding
- captures the inherent risk of the weakness, confidence in the accuracy of the finding, and strength of controls.
Attack Surface
- the barriers that an attacker must overcome in order to exploit the weakness
Environmental
- characteristics of the weakness that are specific to a particular environment or operational context
Common Weakness Scoring System (CWSS)
Three subscores are multiplied together, which produces a CWSS score between 0 and 100. The higher the score, the higher the vulnerability.
Improper input validation
When software does not validate the input properly, an attacker is able to craft the input in a format that is not expected by the rest of the application.
This will lead to
- unintended input resulting in alterted control flow or code execution
- SQL injection attack
- Buffer overflow attack
Examples:
- no .pdf or .zip extension
- file large than 100kB
- special charcters
Input validation function
Prevents improperly formed data from entering an information system
What is input validation
- Only allow specific file content extensions
- All input from all sources must be carefully validated
- Limit maximum input character length (eg: 20 characters)
- Number bounds (min & max)
- Watch out for special characters
Input validation strategies
- Classification strategy (Input Filtering)
– User input can be classified using either blacklisting or whitelisting. - Validation outcome
– User input identified as malicious can either be rejected or sanitised.
Input Filtering
Whitelisting - Allowing only the known good characters
Blacklisting - Allowing anything except the known bad characters
Whitelisting vs Blacklisting
- When building secure software, whitelisting is the
recommended minimal approach. - Blacklisting is prone to error and can be bypassed with
various evasion techniques. However, it can be useful
to help detect obvious attacks. (e.g keywords likein the input textbox)
- Usage
– whitelisting helps limit the attack surface by ensuring data
is of the right semantic validity.
– blacklisting helps detect and potentially stop obvious
attacks.
Regular expression
Regular expressions offer a way to check
whether data matches a specific pattern
(Whitelist)
Flexible Matching
- star () matches any number of instances
/abc/ => ‘a’ followed by zero or more ‘b’ followed by ‘c’ - plus (+) matches at least one instance
/ab+c/ => ‘a’ followed by 1 or more ‘b’ followed by ‘c’ - question mark (?) matches zero or one instance
/ab?c/ => ‘a’ followed by 0 or 1 ‘b’ followed by ‘c’
More flexibility
- {x} will match x number of instances.
/ab{3}c/ => abbbc - {x,y} will match between x and y instances.
/a{2,4}bc/ => aabc or aaabc or aaaabc - {x,} will match x+ instances.
/abc{3,}/ => abccc or abccccccc or abcccccccc
More flexibility
- {x} will match x number of instances.
/ab{3}c/ => abbbc - {x,y} will match between x and y instances.
/a{2,4}bc/ => aabc or aaabc or aaaabc - {x,} will match x+ instances.
/abc{3,}/ => abccc or abccccccc or abcccccccc - dot (.) is a wildcard character – matches
any character except new line (\n) - /a.c/ => ‘a’ followed by any character followed by ‘c’
- Combine metacharacters
/a.{4}c/ => ‘a’ followed 4 instances of any character followed by ‘c’ so will match - addddc
- Afgthc
- ab569c
Security Strategies (SD3 +C)
Secure by Design, Secure by Default, Secure by Deployment, Secure in Communications
Secure by Design
– This means that developers follow secure coding best
practices and implement security features in their
applications to overcome vulnerabilities.
– Ensure that the software design is secured right from
the start.
– Bad software design can make software difficult to
secure later.
– E.g: your application handles sensitive data, so you
will encrypt the data and protect it from theft and
tampering. This consideration to use cryptography in
your application is done at the design stage.