Chapter 4 - Defensive Programming
Chapter 4 - Defensive Programming
• The easiest way to prevent these vulnerabilities is to simply use a language that
does not allow for them
– C allows these vulnerabilities
– Java, Python, and .NET, among other languages and platforms, don’t require special
checks
• Above code can cause over print for full_buffer. This happened because of null
buffer at normal_buffer .
Buffer Overflows - Continue
if ((y > 0 && x > INT_MAX - y) || (y < 0 && x < INT_MIN - y))
{
printf(“Integer Overflow”);
}
else {
sum = x + y;
}
SQL injection
• injection attack wherein an attacker can execute
malicious SQL statements (also commonly referred to
as a malicious payload) that control a web
application’s database server
• QL Injection vulnerability is the oldest, most
prevalent and most dangerous of web application
vulnerabilities.
Example
SQL
PHP
XSS vulnerability
• Cross-Site Scripting (XSS) - web-based attack performed on vulnerable
web applications.
• In XSS attacks, the victim is the user and not the application.
• In XSS attacks, malicious content is delivered to users using JavaScript.
• XSS - can have consequences such as tampering and sensitive data theft
• Attack XSS:
• Hijack an account.
• Spread web worms.
• Access browser history and clipboard contents.
• Control the browser remotely.
• Scan and exploit intranet appliances and applications.
Race Condition
• Race Condition occurs when a second thread
modifies the state of one
Race Condition - Prevention
• make sure that the critical section is executed as an atomic
instruction.
– That means that once a single thread is executing it, no
other threads can execute it until the first thread has left
the critical section.
• using proper thread synchronization in critical sections.
– using a synchronized block of Java code.
– other synchronization constructs like locks or atomic
variable like java.util.concurrent.atomic.AtomicInteger.
Correct usage of third-party components
• Have clear policy
• monitor security patches
• Know that components used may not rely on other
components that may not be secure.
• Frequently check for third library used.
Choice of programming language and type-safe languages