CyberAttack
SQL Injection
Principle
Attackers inject malicious SQL statment into database queries, altering the original query logic.
Example
`' OR '1'='1'
Defense
Parameterized queries, ORM frameworks & input validation
Cross-Site Scripting (XSS)
- Allow attackers to inject mailcious scripts into web pages viewd by other users
- These script execute in the victim's broswer to steal their sensitive data.
Principle
- Hacker inject malicious script onto the webpage
- User opens the website
- User's broswer downloads the website from the broswer
- Malicious script runs, could be:
- Steal cookies or session tokens
- Redirect users to malicious websites
Type
- Stored XSS: Malicious script is stored in the server's database (e.g., in the comment session of the forum) and executed whenever a user visits the affected webpage.
- Reflected XSS: Malicious script is embedded in a URL or request (e.g., in a query parameter) and executed when the user clicks a crafted link.
- DOM-based XSS: The vulnerability lies in client-side scripts that dynamically manipulate the Document Object Model (DOM) with untrusted input, without server interaction.
Defense
- Input Sanitation: reject script or HTML tag
- Use secure frame, Vue/React
- Output encoding
- Implement Content Security Policy (CSP) to restrict which scripts can run on a page, For example:
Content-Security-Policy: default-src 'self'; script-src 'self' trusted.com
Cross-Site Request Forgery (CSRF)
Attacker use the victim's active session to execute unauthorized requests, such as changing account details and transfering funds.
How the Attack Works
- The user is logged into bank.com, and their browser has a valid session cookie.
- The attacker lures the user to visit attacker.com (e.g., by clicking a link in a phishing email)
- The malicious webpage loads, and the hidden form automatically sends a POST request to
http://bank.com/transfer
with the attacker’s parameters (amount=1000
,to_account=99999
). - The browser includes the user’s session cookie with the request because it’s sent to bank.com.
- The bank server, unaware that the request is malicious, processes the transfer.
Defense
- Use CSRF Token
- A unique, unpredictable value generated by a server-side application and sent to the client. (usually embedded in header)
- This token acts as a secret key that the server verifies to ensure that requests are coming from the user's own browser and not from a malicious site.
- Verify SameSite Cookie
- Prevent the cookie from being sent with cross-site requests.
- Verify Referer/Origin
- The server checks the
Referer
orOrigin
header to ensure the request comes frombank.com
. - The attacker's request from
attacker.com
would have a missing header, causing the server to reject it.
- The server checks the
- Need one extra step for user to confirm
- Use MFA for high-risk actions.
Denial-of-Service (DoS)
What is DoS
- Aims to disrupt the availbility of a service and system to prevent legitimate users from accessing the targeted resource
- By overwhelming it with a flood of illegitimate requests
Principle
- Work by exhausting the target's resources to make the service slow and completely unavailable.
Example
- The attackers uses a botnet to send millions of HTTP requests per second to a page.
Defense
- Rate Limiting: restrict the number of requests a single IP can make in a given time period.
- Web Application Firewalls: To filter malicious traffic based on patterns or ruless.
- Load Balancers: Distribute traffic across multiple servers to absorb or mitigate attack traffic
- DDoS Protection Services: use services like AWS Shield, Cloudflare to detect and migigate large-scale attacks
Man-in-the-Middle (MitM)
What is MitM
- The attacker intercepts data transmitted between the client and server without their knowledge
Priniciple
- Exploit the vulnerabilities in network portocols or configurations to intercept data transmitted between the client and server.
- The attack can gain access to sensitive information
- Common methods include DNS spoofing, or compromising unsecured WiFi networks
Example
- A user connects to a public WiFi network and logs into their banking website over HTTP.
Defense
- Use HTTPS to encrypt data in transit
- Avoid public WiFi for sensitive transactions or use VPN to encrypt traffic.