Lecture 9 - Secure Implementation 2 - Coding 2023
Lecture 9 - Secure Implementation 2 - Coding 2023
ICT3103/ICT3203:
Secure Software Development
Secure Software Implementation
Part 2: References for Secure Coding
SIT Internal
SQLite?
2
SIT Internal
Leap seconds?
3
SIT Internal
4
SIT Internal
Leap seconds
Are you using Regular Expression (Regex) for checking time input?
5
SIT Internal
MS Teams…
7
SIT Internal
Problem details
8
SIT Internal
Problem details
• Additionally, the analysts discovered that the "Cookies" folder also contained valid
authentication tokens, along with account information, session data, and marketing
tags.
• Vectra developed an exploit by abusing an API call that allows sending messages to
oneself. Using SQLite engine to read the Cookies database, the researchers received
the authentication tokens as a message in their chat window.
“The technique described does not meet our bar for immediate servicing
as it requires an attacker to first gain access to a target network.
We appreciate Vectra Protect’s partnership in identifying and responsibly
disclosing this issue and will consider addressing in a future product
release.”
10
SIT Internal
Open-source vs Closed-source
11
SIT Internal
Agenda
OWASP
• Top 10 Proactive Controls
• [Link]
• Cheat Sheet Series
• [Link]
• Security Knowledge Framework
• [Link]
• Secure Coding Practices Quick Reference Guide
• [Link] practices-quick- reference-
guide/migrated_content
And more …
12
SIT Internal
Vulnerabilities Prevented:
• OWASP Top 10 – A06:2021-Vulnerable and Outdated Components
Tools:
• OWASP Dependency Check – a Software Component Analysis (SCA) tool
to detect publicly disclosed vulnerabilities in dependencies
• [Link] – scanner for JavaScript libraries
16
SIT Internal
17
SIT Internal
[Link]
18
SIT Internal
19
SIT Internal
References:
• OWASP Cheat Sheet Series:
• [Link]
ml
• [Link]
ml
• [Link]
• Bobby Tables – [Link]
20
SIT Internal
• Encode and validate any dangerous characters before logging to prevent log
injection attacks.
• Do not log sensitive information. For example, do not log password, session
ID, credit cards, or social security numbers.
• Protect log integrity. An attacker may attempt to tamper with the logs.
Therefore, the permission of log files and log changes audit should be
considered.
• Forward logs from distributed systems to a central, secure logging service.
This will sure log data cannot be lost if one node is compromised. This also
allows for centralized monitoring.
• PDPA!
22
SIT Internal
• Log4J?
• winston
• Pino
And more…
23
SIT Internal
25
SIT Internal
26
SIT Internal
The primary means of input validation for free-form text input should be:
• Normalization: Ensure canonical encoding is used across all the text and no invalid
characters are present.
• Character category allow-listing: Unicode allows listing categories such as "decimal
digits" or "letters" which not only covers the Latin alphabet but also various other
scripts used globally (e.g. Arabic, Cyrillic, CJK ideographs etc).
• Individual character allow-listing: If you allow letters and ideographs in names and
also want to allow apostrophe ' for Irish names, but don't want to allow the whole
punctuation category.
Your application support emoji?
30
SIT Internal
Regular expressions
• Developing regular expressions can be complicated, and is well beyond the scope of this
cheat sheet.
• There are lots of resources on the internet about how to write regular expressions,
including the cheatsheet and the OWASP Validation Regex Repository.
• When designing regular expression, be aware of RegEx Denial of Service (ReDoS) attacks.
These attacks cause a program using a poorly designed Regular Expression to operate very
slowly and utilize CPU resources for a very long time.
31
SIT Internal
32
SIT Internal
33
SIT Internal
For example, the Regex pattern or quantifier ^(a+)+$ is represented by the following non-deterministic finite
automata (NFA):
• For the input aaaaX there are 16 possible paths in the above graph. But
for aaaaaaaaaaaaaaaaX there are 65536 possible paths, and the number is double for each
additional a. This is an extreme case where the naïve algorithm is problematic, because it must pass
on many paths to find a non-matching input.
• The root-cause of the above example is in a Regex engine feature called backtracking. Simply, if the
input (token) fails to match, the engine goes back to previous positions where it could take a
different path. The engine tries this many times until it explores all possible paths. In the above
example, this feature create a long running loop because there were many paths to explore due to
inefficient Regex pattern.
34
SIT Internal
Evil Regex
A Regex pattern is called Evil Regex if it can get stuck on crafted input.
Evil Regex contains:
• Grouping with repetition
• Inside the repeated group:
– Repetition
– Alternation with overlapping
Examples of Evil Regex:
• (a+)+
• ([a-zA-Z]+)*
• (a|aa)+
• (a|a?)+
• (.*a){x} for x \> 10
• All the above are susceptible to the input aaaaaaaaaaaaaaaaaaaaaaaa! (The minimum input length might
change slightly, when using faster or slower machines).
35
SIT Internal
36
SIT Internal
37
SIT Internal
Primary Defenses:
• Option 1: Use of Prepared Statements (with Parameterized Queries)
• Option 2: Use of Properly Constructed Stored Procedures
• Option 3: Allow-list Input Validation
• Option 4: Escaping All User Supplied Input
Additional Defenses:
• Also: Enforcing Least Privilege
• Also: Performing Allow-list Input Validation as a Secondary Defense
38
SIT Internal
Bobby Tables
39
SIT Internal
Bobby Tables
40
SIT Internal
41
SIT Internal
42
SIT Internal
It defines a set of security coding practices in a checklist format that can be adopted into building secure
software.
• Input validation
• Output encoding
• Authentication and password management
• Session management
• Access control
• Cryptographic practices
• Error handling and logging
• Data protection
• Communication security
• Database security
• File management
• General coding practices
43
SIT Internal
44
SIT Internal
45
SIT Internal
Question time
46
SIT Internal
(Maybe) Solution
47
SIT Internal
Jenkins
48
SIT Internal
Dockerfile
docker-compose
51
SIT Internal
Practical Test
52
SIT Internal
Reference
• [Link]
lastpass-password-manager-data-breach
• [Link]
• [Link]
community/attacks/Regular_expression_Denial_of_Service_-_ReDoS
• [Link]
auth-tokens-as-cleartext-in-windows-linux-macs/
• [Link]
us/dotnet/architecture/microservices/docker-application-development-
process/docker-app-development-workflow
54