Unit 3.2
Unit 3.2
platforms
1. Obfuscation
Technique: Attackers often obfuscate code to make it harder for security tools to
recognize malicious patterns. For instance, they may encode scripts using Base64 or
deeply nest JavaScript code to hide its true purpose.
Example:
o JavaScript code like
eval(atob("ZG9jdW1lbnQuY29va2llPSJhdGsiOw==")); is obfuscated using
Base64. When decoded, it becomes document.cookie="atk";, setting a
cookie for further malicious activities.
Countermeasures:
o Real-time De-obfuscation: Tools like JSDetox and Malzilla can de-obfuscate
malicious JavaScript and identify hidden functionality.
o Content Security Policy (CSP): Implement CSP to block inline scripts and
restrict the execution of JavaScript from unauthorized sources.
o Static and Dynamic Analysis: Use static code analysis tools to inspect code
before execution, and dynamic analysis to monitor code during runtime.
Technique: Attackers inject extra HTTP parameters into URLs to modify the
request's behavior or evade filters.
Example:
http://example.com/search?q=test&admin=true&q=malicious_query
If the server only expects a single q parameter, it might only process the first one and
overlook the second, which could modify query logic or security checks.
Countermeasures:
o Input Validation: Validate parameters on both client and server-side,
ensuring that unexpected or duplicate parameters are not accepted.
o Request Normalization: Sanitize requests before they reach the server. For
instance, normalize multiple instances of the same parameter to avoid
ambiguity.
o WAF Configuration: Configure the Web Application Firewall (WAF) to
detect and block HTTP Parameter Pollution attempts.
3. User-Agent Spoofing
Technique: Attackers craft SQL queries that bypass security mechanisms by splitting
payloads or using encoding to avoid detection.
Example:
o A basic SQL injection attack: SELECT * FROM users WHERE username =
'admin' OR 1=1--'; could be split into multiple requests or encoded using
URL encoding to evade WAF filters.
Countermeasures:
o Parameterized Queries: Use prepared statements and parameterized queries
to ensure user inputs are treated as data rather than executable code.
o Web Application Firewall (WAF) Rules: Regularly update your WAF to
handle new SQL injection patterns. Make sure your WAF can detect both
encoded and decoded forms of the payload.
o Input Sanitization: Validate and sanitize all input data to ensure no untrusted
input is executed by the database.
SQL Injection Risk: SQL injection attacks occur when attackers inject
malicious SQL code into a web application’s input fields (e.g., search
boxes, login forms). Without sanitization, this malicious code could be
executed directly by the database.
Untrusted Inputs: Any data that comes from a user (e.g., form data, URL
parameters, cookies, HTTP headers) should be considered untrusted and
must be validated and sanitized before processing or executing it against
the database.
Data Integrity and Security: By sanitizing input, you ensure that only
clean, expected data is processed, thereby maintaining the integrity of the
application and its data.
To properly sanitize and validate input data, follow these best practices:
a. Input Validation
Input validation involves checking the incoming data against predefined rules and patterns
to ensure it conforms to what the application expects. Invalid or unexpected inputs should be
rejected or handled appropriately.
Whitelisting: Allow only certain values or patterns that are known to be valid. For
example, if a field only accepts numbers, reject any input containing letters or
symbols.
Field Length: Restrict the length of input fields to prevent buffer overflows or
excessively large SQL injection payloads. For example, limit a username field to 50
characters.
Data Type Checking: Ensure that the input matches the expected data type. If a field
is supposed to accept a number, reject any input that contains letters or special
characters.
o Example:
For a username field:
if (!/^[a-zA-Z0-9_]{3,30}$/.test(username)) {
throw new Error("Invalid username");
}
Range Checking: For numeric inputs, ensure that the value falls within a specific
range. This prevents attackers from injecting extreme values to trigger database
errors.
Escape Special Characters: Special characters like ', ", ;, --, %, and
others should be escaped or removed when input is used in SQL
queries.
SQL Escaping Example: In PHP, use
mysqli_real_escape_string() to escape special characters.
javascript
Example in Python:
python
import re
user_input = re.sub(r'[\'";--]', '', user_input) # Remove potentially
harmful characters
php
python
java
Instead of allowing all inputs and trying to detect bad ones, follow a
default deny approach. This means rejecting any input that doesn't
specifically match allowed patterns.
e. Sanitize Output as Well
php
php
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username = '$username'
AND password = '$password'";
$result = mysqli_query($conn, $query);
sql
To prevent this SQL injection attack, the input should be validated and
sanitized, or better yet, parameterized queries should be used:
php
This ensures that user input is treated strictly as a string and not
executable SQL code, thereby preventing SQL injection.
Technique: Attackers inject encoded or hidden scripts into web pages, often using
uncommon input fields or HTTP headers.
Example:
o A hidden XSS payload could be injected through an HTTP header: User-
Agent: <script>alert('XSS');</script>, bypassing basic XSS filters.
Countermeasures:
o Input Validation and Output Encoding: Ensure all user input is properly
validated, and use functions like htmlspecialchars() to encode potentially
dangerous characters.
o CSP: Implement a Content Security Policy that prevents scripts from
untrusted domains from executing on your platform.
o Auto-sanitizing Frameworks: Use frameworks like React or Angular that
automatically sanitize inputs to prevent XSS.
7. Encrypted Communication
Technique: Attackers use encrypted communication channels (e.g., HTTPS, SSH) to
mask malicious activities from network monitoring systems.
Example:
Countermeasures:
o SSL/TLS Inspection: Deploy SSL/TLS interception tools that decrypt and
inspect encrypted traffic, detecting any malicious activities within the
encrypted session.
o Endpoint Detection: Use endpoint protection tools that can analyze behaviors
after traffic has been decrypted, allowing detection of malicious behavior on
the endpoint itself.
8. Fileless Malware
Initial Access: The attacker gains access to the system through phishing or
exploiting a vulnerability.
Command:
powershell
The script is fetched from a remote server and executed in memory, leaving
no trace on the hard drive.
Data Exfiltration: The extracted credentials are then sent to the attacker over
an encrypted channel.
Countermeasures:
o Behavior-based Detection: Use tools like Sysmon or CrowdStrike that
monitor in-memory activities and detect abnormal process behaviors.
Sysmon (System Monitor): A Windows system utility that logs and detects
system activity, including process creations, network connections, and memory
injections.
Technique: Attackers insert delays in malicious code execution (e.g., making the
code "sleep" for hours or days) to evade dynamic analysis tools that stop monitoring
after a short time.
Example:
o A malware script could include a sleep function (sleep(86400)) that delays
execution for a day, tricking automated sandboxes that typically monitor for a
few minutes.
Countermeasures:
o Long-Term Monitoring: Use sandbox environments capable of long-duration
analysis to detect time-delayed payloads.
o Event-based Triggers: Implement event-driven analysis that triggers alarms
based on suspicious activities (e.g., sleeping functions) rather than a fixed time
window.
Technique: Malware that continuously modifies its code structure to evade signature-
based detection. Polymorphic malware changes its appearance, while metamorphic
malware rewrites its own code entirely.
Example:
o A polymorphic virus might encrypt itself with a different key each time it
infects a new system, rendering traditional signature-based antivirus tools
ineffective.
Countermeasures:
o Machine Learning Detection: Use machine learning-based detection tools
that focus on behavioral anomalies rather than static signatures.
o Heuristic Analysis: Deploy heuristic analysis tools that identify suspicious
behaviors regardless of code appearance.
14. Steganography
Technique: Attackers hide malicious payloads in seemingly benign files (e.g.,
images, audio) to evade detection.
Example:
o An attacker embeds malicious code within the pixel data of an image file. The
image appears normal to users but contains hidden instructions.
Countermeasures:
o Steganalysis Tools: Use steganalysis tools like StegExpose to detect hidden
data within images or audio files.
o Traffic Monitoring: Monitor file upload/download traffic for unusual
patterns, such as an abnormally high number of media files being transferred.