Skip to content
SQL injection Write-up Beginner

The Castle’s Latrine

Joe Gregg Follow

Published on blog.infiltrateops.io

Château Gaillard was a castle in Normandy, France. It was built by Richard I of England in 1196 and was one of the most technologically advanced castles of its time.

redteamer, on blog.infiltrateops.io

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

This is a story from a previous employer, told by a red teamer who performs penetration tests during the day and bug bounty work and security research at night. The target was not a named product or a public bug bounty programme: it was a server in the author's own employer's infrastructure, found while performing OSINT on the organisation. The report does not name the company, the application, the endpoint or the parameter, and the author says they are unable to show screenshots of the app itself. What the report does describe is an unauthenticated SQL injection reached through a parameter discovered in a JavaScript file.

The author opens with Château Gaillard, a castle in Normandy built by Richard I of England in 1196 and one of the most technologically advanced castles of its time. It fell in 1204 during the Siege of Château Gaillard when a man climbed up the latrine and opened the gates from the inside. The point of the analogy is that a system can be advanced everywhere that was designed to be defended and still be defeated through the part nobody looked at. In the same way, the author argues, organisations under deadline pressure push development code into production and skip security testing, leaving overlooked weaknesses in their own infrastructure.

Setup

The only tooling the report names is:

  • Shodan, used for the initial discovery.
  • LinkFinder, used to parse JavaScript files and extract potential endpoints.
  • sqlmap, used to confirm and exploit the injection.

No accounts or roles were needed: the injection was reachable without authentication. The report gives no versions for any of the tools and no details about the server, framework or database.

Finding it

The author started with a Shodan query for the organisation's SSL certificate:

Text
ssl:companyname

The query returned a few results, and one of them was a server the author had not encountered before while performing OSINT on the organisation. Browsing to that site presented a login prompt.

With a web application in front of them, the first step was to run LinkFinder against it. LinkFinder parses the JavaScript files embedded in the page it is pointed at and extracts potential endpoints from them. The author then greps the output for results that start with a slash and appends them to a results file:

One line in the results file stood out:

Text
/some/endpoint?SomeVariable=

Browsing to https://someplace.com/some/endpoint?SomeVariable= returned only the number 0. An endpoint that answers an unauthenticated request with a bare value and takes a parameter is exactly the kind of small, unglamorous surface the castle analogy is about.

Exploitation

Step 1: break the query with an apostrophe

The next step was to assign an apostrophe to the variable, in the hope of breaking a SQL query. This produced an error similar to the message in the image below.

SQL error

An error on a single quote suggests the parameter is being placed into a query rather than handled as data.

Step 2: confirm and exploit with sqlmap

Following up on the error, the author used sqlmap to obtain SQL injection within the application. The command was similar to the one below:

Bash
sqlmap -u "https://someplace.com/some/endpoint?SomeVariable=TEST"

The -u flag points sqlmap at the full URL with a placeholder value in the parameter, and sqlmap takes it from there. The report does not include sqlmap's output, the database type, or what data was retrieved.

Root cause

The report does not analyse the application's code. What it shows is a parameter where a single apostrophe produces a database error and sqlmap can drive the injection. The author frames the underlying cause as process rather than syntax: deadlines, features that need to go live, development code pushed into production, and security testing skipped in the rush. The endpoint was reachable without authentication, so an outsider could reach the query without logging in. The report does not say what defences, if any, were in place.

Impact

The author states that SQL injection was obtained within part of the application in an unauthenticated fashion. The impact should be read in those terms: an unauthenticated injection point in a production system, with the extent of access left unstated.

Fix

Its recommendation is procedural: security testing during the development process, and before a release is put into production.

Variants to test

The technique here is generic and transfers directly to other targets:

  1. Certificate-based Shodan searches. Query ssl:companyname (or the organisation's other certificate identifiers) and look for hosts you have not seen in previous OSINT. A positive result is a host that resolves to a login page or an API belonging to the organisation but absent from your asset inventory.
  2. JavaScript endpoint extraction. Run LinkFinder against every JavaScript file the application loads, then filter for paths beginning with a slash. A positive result is an endpoint path that is not linked from the visible UI.
  3. Probe extracted endpoints unauthenticated. Request each one with an empty or dummy parameter value. A positive result is a response that returns data, a status code or an error rather than a redirect to login.
  4. Single-quote test. Append an apostrophe to each parameter and compare the response with the baseline. A positive result is a database error, a stack trace or a change in behaviour that the clean request does not produce.
  5. Confirm with sqlmap. Point sqlmap at the parameter with a placeholder value, as in the command above. A positive result is sqlmap identifying an injectable parameter.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 9 min read.

What happened, in plain words

This is a story from a previous employer, told by someone who does penetration testing for a living: during the day they work on a red team, and at night they take part in bug bounty programmes and do security research. The target was not a named product and not a public bug bounty programme. It was a server in the author's own employer's infrastructure, found while performing OSINT on the organisation. The report does not name the company, the application, the endpoint or the parameter, and the author says they are unable to show screenshots of the app itself. What the report does describe is an unauthenticated SQL injection reached through a parameter discovered in a JavaScript file.

The author opens with a piece of history. Château Gaillard was a castle in Normandy, France, built by Richard I of England in 1196 and one of the most technologically advanced castles of its time. It fell in 1204 during the Siege of Château Gaillard when a man climbed up the latrine and opened the gates from the inside. The point of the analogy is that a system can be advanced everywhere that was designed to be defended and still be defeated through the part nobody looked at. In the same way, the author argues, organisations under deadline pressure push development code into production and skip security testing, leaving overlooked weaknesses in their own infrastructure.

If you are new to security, the rest of this article walks through the same story step by step, explaining every term as it comes up. Nothing here requires more than a browser and a computer to follow along in principle; the tools are described in plain words before they are used.

Setup

The only tooling the report names is:

  • Shodan, used for the initial discovery. Shodan is a search engine for internet-connected devices and servers rather than for web pages. Instead of indexing page text, it records what services answer on the public internet, so you can search for things like a particular certificate or a particular piece of software.
  • LinkFinder, used to parse JavaScript files and extract potential endpoints. JavaScript is the programming language that runs inside your browser and makes pages interactive; a JavaScript file is a text file of that code that a page loads. LinkFinder reads such files and pulls out the paths (endpoints) it finds inside them.
  • sqlmap, used to confirm and exploit the injection. sqlmap is a command-line tool that automates the work of testing whether a parameter is vulnerable to SQL injection and, if so, extracting data through it.

No accounts or roles were needed: the injection was reachable without authentication. "Unauthenticated" simply means the server answered the request without asking who you are, with no username, password or session required. The report gives no versions for any of the tools and no details about the server, framework or database.

Finding it

The author started with a Shodan query for the organisation's SSL certificate:

Text
ssl:companyname

Line by line, this is a single search expression:

  • ssl: is a Shodan filter. It tells Shodan to look at the SSL/TLS certificate a server presents, not at the page content. A certificate is the digital document a server shows to prove its identity when you connect over HTTPS, the padlocked version of the web.
  • companyname is the text to look for inside those certificates. Certificates often carry the organisation's name, so searching for it is a way to find servers that belong to that organisation.

The query returned a few results, and one of them was a server the author had not encountered before while performing OSINT on the organisation. OSINT means open-source intelligence: gathering information about a target from publicly available sources, without touching anything private. Browsing to that site presented a login prompt.

With a web application in front of them, the first step was to run LinkFinder against it. LinkFinder parses the JavaScript files embedded in the page it is pointed at and extracts potential endpoints from them. An endpoint is a specific address inside a web application that does something when you request it, like a counter window in a building: you go to that exact window to ask for that exact service. The author then greps the output for results that start with a slash. To grep is to filter text for lines matching a pattern; a slash at the start means the line looks like a path on the site, such as /some/endpoint, rather than a full external address.

Line by line:

  • linkfinder.py runs the LinkFinder script.
  • https://someplace.com/target.js is the JavaScript file being analysed. someplace.com is a placeholder for the real site, which the report does not name.
  • -o cli tells LinkFinder to print its results to the command line instead of writing them to a file.
  • | is a pipe. It takes whatever the left side printed and hands it to the command on the right as input.
  • grep '^/' filters that input and keeps only lines that begin with a slash. The ^ means "start of line".

One line in the results file stood out:

Text
/some/endpoint?SomeVariable=

This is a path plus a parameter. The part before the ? is the endpoint, the address of the counter window. The part after the ? is a parameter: a named value you hand to the endpoint so it knows what you want. Here the parameter is called SomeVariable and it is left empty, with nothing after the = sign. Parameters are how web applications receive input from you, and input is exactly where weaknesses tend to live.

Browsing to https://someplace.com/some/endpoint?SomeVariable= returned only the number 0. An endpoint that answers an unauthenticated request with a bare value and takes a parameter is exactly the kind of small, unglamorous surface the castle analogy is about: nobody designed it as a gate, but it is a way in.

Exploitation

Step 1: break the query with an apostrophe

The next step was to assign an apostrophe to the variable, in the hope of breaking a SQL query. SQL is the language applications use to talk to their database, the store where records live. A query is a sentence in that language, for example "give me the record whose name is X". If the application builds that sentence by pasting your input straight into it, then input containing a quote character can end the sentence early and leave the rest of the query malformed, which the database reports as an error.

This produced an error similar to the message in the image below.

SQL error

An error on a single quote suggests the parameter is being placed into a query rather than handled as data. Concatenated means glued together as text. The safe alternative is to treat your input strictly as a value, never as part of the query's grammar, so a quote inside it stays an ordinary character.

Step 2: confirm and exploit with sqlmap

Following up on the error, the author used sqlmap to obtain SQL injection within the application. SQL injection is the vulnerability class where input you supply changes the meaning of a database query instead of just being stored or compared as a value. The command was similar to the one below:

Bash
sqlmap -u "https://someplace.com/some/endpoint?SomeVariable=TEST"

Line by line:

  • sqlmap runs the tool.
  • -u tells it the target URL to test.
  • "https://someplace.com/some/endpoint?SomeVariable=TEST" is that URL, in quotes so the shell treats it as one piece. TEST is a placeholder value dropped into the parameter; sqlmap will vary it and watch how the server responds.

The -u flag points sqlmap at the full URL with a placeholder value in the parameter, and sqlmap takes it from there. The report does not include sqlmap's output, the database type, or what data was retrieved.

Root cause

The report does not analyse the application's code. What it shows is a parameter where a single apostrophe produces a database error and sqlmap can drive the injection. The author frames the underlying cause as process rather than syntax: deadlines, features that need to go live, development code pushed into production, and security testing skipped in the rush. The endpoint was reachable without authentication, so an outsider could reach the query without logging in.

Impact

The author states that SQL injection was obtained within part of the application in an unauthenticated fashion. The impact should be read in those terms: an unauthenticated injection point in a production system, with the extent of access left unstated.

Fix

Its recommendation is procedural: security testing during the development process, and before a release is put into production.

Variants to test

The technique here is generic and transfers directly to other targets:

  1. Certificate-based Shodan searches. Query ssl:companyname (or the organisation's other certificate identifiers) and look for hosts you have not seen in previous OSINT. A positive result is a host that resolves to a login page or an API belonging to the organisation but absent from your asset inventory. An API is the part of a service that answers requests from software rather than drawing a page for a person.
  2. JavaScript endpoint extraction. Run LinkFinder against every JavaScript file the application loads, then filter for paths beginning with a slash. A positive result is an endpoint path that is not linked from the visible UI.
  3. Probe extracted endpoints unauthenticated. Request each one with an empty or dummy parameter value. A positive result is a response that returns data, a status code or an error rather than a redirect to login.
  4. Single-quote test. Append an apostrophe to each parameter and compare the response with the baseline. A positive result is a database error, a stack trace or a change in behaviour that the clean request does not produce.
  5. Confirm with sqlmap. Point sqlmap at the parameter with a placeholder value, as in the command above. A positive result is sqlmap identifying an injectable parameter. DBMS stands for database management system, the software that stores and serves the database.

Glossary

  • API: the part of a service that answers requests from software rather than drawing a page for a person.
  • Authentication: the process of proving who you are, usually with a username and password; an unauthenticated request is one made without doing so.
  • Bug bounty programme: a scheme where an organisation rewards people for reporting security weaknesses to it.
  • Certificate (SSL/TLS): the digital document a server presents to prove its identity when you connect over HTTPS.
  • Concatenation: gluing pieces of text together, here input pasted directly into a database query.
  • Database: the store where an application keeps its records.
  • DBMS: database management system, the software that stores and serves the database.
  • Endpoint: a specific address inside a web application that does something when you request it.
  • grep: a command that filters text and keeps only lines matching a pattern.
  • JavaScript: the programming language that runs in your browser and makes pages interactive.
  • LinkFinder: a tool that reads JavaScript files and extracts potential endpoints from them.
  • OSINT: open-source intelligence, gathering information about a target from publicly available sources.
  • Parameter: a named value handed to an endpoint, usually after a ? in the URL.
  • Penetration testing: authorised testing that acts like an attacker to find weaknesses in a system.
  • Pipe (|): a shell operator that passes the output of one command as input to the next.
  • Production: the live environment where real users use the system.
  • Red team: a group that plays the attacker to test an organisation's defences.
  • Shodan: a search engine for internet-connected devices and servers rather than web pages.
  • SQL: the language applications use to talk to their database.
  • SQL injection: a vulnerability where input you supply changes the meaning of a database query instead of being treated as a value.
  • sqlmap: a command-line tool that automates testing for and exploiting SQL injection.
  • SSL: the older name for the encryption and certificate system behind HTTPS.
  • Stack trace: an error report showing the internal steps a program took before failing.
  • Status code: the number a server returns to say how a request went, such as 200 for success or 302 for a redirect.
  • Unauthenticated: reachable without proving who you are.

More by Joe Gregg

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.