charlesreid1.com blog

SQL Injection Fundamentals: Four Attack Shapes

Posted in Security

permalink

This is part 2 of our series on working through the PortSwigger Web Security Academy. This one covers SQL injection at a bird's-eye view - the four main attack shapes that show up over and over, with the smallest possible example of each. Later posts will drill into specific attack types.

Full notes on our wiki: SQL Injection.

What SQL Injection Is

SQL injection is a web security bug that lets attackers execute their own SQL against your database, by taking advantage of user inputs that are not sanitized before being pasted into a SQL query.

The reason it is worth caring about is impact vs. effort. SQL injection is one of the highest-impact web vulnerabilities (attacker can potentially read or modify anything in the database), and one of the lowest-effort to actually pull off. That combination is why it has been in the OWASP Top 10 since forever.

The Four Shapes

Our notes group SQL injection into four attack shapes:

  1. Retrieving hidden data
  2. Subverting application logic
  3. UNION attacks
  4. Blind SQL injection

The rest of this post walks through the smallest example of each.

Shape 1: Retrieving Hidden Data

Suppose a shopping site has this URL for showing product listings:

https://insecure-website.com/products?category=Gifts

Behind the scenes, that URL runs this SQL query:

SELECT * FROM products WHERE category = 'Gifts' AND released = 1

The released = 1 clause hides unreleased products from the public. If the category parameter is not sanitized, we can smuggle in SQL that comments out the rest of the query:

https://insecure-website.com/products?category=Gifts'--

The -- starts a SQL comment. Everything after it is ignored, including the AND released = 1 check. Result: all products, including unreleased ones, get returned.

Same trick with an unconditional truthy clause:

https://insecure-website.com/products?category=Gifts'+OR+1=1--

Shape 2: Subverting Application Logic

Same idea, applied to authentication. If a login form runs

SELECT * FROM users WHERE username = 'user' AND password = 'nopass'

and doesn't sanitize the username field, you can log in as anyone by supplying the username:

administrator'--

Everything after the -- (including the entire password check) becomes a comment. The query returns the administrator row. You are logged in.

You have to guess the username - admin, administrator, root, superuser - so this is not a one-shot attack, but it is not a hard attack either. If you see a login page throwing internal server errors when you put a single quote in the username field, that is your signal.

Shape 3: UNION Attacks

UNION attacks use SQL's UNION operator to piggyback data from other tables into the results of the original query. If a shopping site runs

SELECT name, description FROM products WHERE category = 'Gifts'

and the category parameter is injectable, we can attach a UNION:

' UNION SELECT username, password FROM users--

The final query becomes

SELECT name, description FROM products WHERE category = ''
UNION SELECT username, password FROM users--'

which returns product listings and every username/password pair. Whatever UI was going to display product name and description now also displays usernames and passwords.

The oversimplified version above works out cleanly, but in practice you need to figure out the column count of the outer query first, then make sure the columns you union in are type-compatible. We covered that in the next post.

Shape 4: Blind SQL Injection

Blind SQLi is what you do when the application is vulnerable but the query results don't come back to you in the HTTP response. Cookie tracking IDs are the classic example - the ID gets fed to a SQL query on every request, but the query result is never rendered on the page.

Even without seeing the result directly, you can often infer it from how the page behaves. Ship one of these two requests:

xyz' AND '1'='1
xyz' AND '1'='2

If the page renders differently in the two cases (say, one shows a "Welcome back" banner and the other doesn't), congratulations, you have a boolean oracle. Now you can ask the database yes/no questions and get answers via page behavior. Wrap the boolean around anything you want to know:

xyz' AND SUBSTRING((SELECT Password FROM Users WHERE Username = 'Administrator'), 1, 1) > 'm

That request returns "Welcome back" if the first character of the administrator's password is greater than m, and doesn't otherwise. Do that in a binary search per character, and you have the whole password.

Blind SQLi comes in a few flavors - conditional responses (above), conditional errors, and time-based. We cover the first two in the next couple of posts.

What Ties Them Together

Every one of these attacks is a variation on the same theme: a user input is being pasted directly into a SQL query, and the attacker can supply SQL syntax that changes the meaning of the query. The fix is the same in every case: parameterized queries. Not string concatenation. Not escaping. Parameterized queries.

References

Tags:    security    sql injection    sqli    portswigger    web security    owasp   

Working Through the PortSwigger Web Security Academy

Posted in Security

permalink

This post is part 1 of a series. We spent a chunk of May and June 2023 working through the PortSwigger Web Security Academy labs, mostly the SQL injection track, and taking notes on our wiki as we went. This post is the setup - what the Academy is, why we picked it, and how it fits together. The posts that follow will cover the specific techniques.

What the Academy Is

PortSwigger makes Burp Suite, which is the standard tool for web application security testing. They also run a free online academy at portswigger.net/web-security, which consists of written lessons plus hands-on labs. The labs are hosted for you - you get a URL, you attack the app running at that URL, and the Academy tracks whether you succeeded.

The labs are organized roughly by the OWASP Top 10, which is the standard catalog of web application vulnerability categories. SQL injection, XSS, XXE, access control issues, authentication bugs, and so on.

The reason to pick this Academy over the other options is that the labs are real vulnerable web apps, not multiple-choice questions. You are using actual Burp Suite against actual HTTP requests. When you finish a lab, you have practiced the thing, not read about the thing.

Our Setup

The setup is not complicated:

  • Burp Suite Community Edition (free, sufficient for most labs, painful for a few of the automation-heavy ones)
  • A PortSwigger account (also free)
  • A browser configured to proxy through Burp on port 8080
  • A wiki page to take notes on, so you remember what you learned a week later

That last one is the whole point. The Academy is good, but if you finish a lab and move on without writing anything down, you will forget the technique inside a month. Writing up each lab in your own words - the setup, the attack, the trick that made it work - is what turns lab practice into retained knowledge.

The OWASP Top 10 as a Scaffold

We use OWASP Top 10 as the organizing scaffold for our security notes on the wiki. It is not the only way to slice web security, but it is the way that most people talk about web security, and it is what you'll see referenced in bug bounty programs and pentest reports. Learning to recognize each category is a solid foundation.

The list has changed over the years - the 2021 version reorganized things significantly - but the SQL injection, XSS, and XXE categories that we focus on in this series have been fixtures since the beginning.

What This Series Will Cover

Rough plan for the next few posts:

  • SQL injection fundamentals - the four attack shapes that cover most cases
  • UNION attacks specifically, and how to use them to dump data from tables you don't have direct access to
  • Blind SQL injection with conditional responses - inferring data from page behavior
  • Blind SQL injection with conditional errors - the more advanced Oracle-flavored variant
  • A short post on two Burp Suite extensions that are worth installing before you start

Everything is grounded in a specific PortSwigger lab. Everything has a corresponding page on our wiki. If the summary here is not enough detail, the wiki page is the deep version.

References

Tags:    security    web security    portswigger    burp suite    owasp    pentesting   

Notes on Italo Calvino's Six Memos for the Next Millennium

Posted in Reading

permalink

We spent the tail end of 2022 reading Italo Calvino's Six Memos for the Next Millennium, which is a set of Norton Lectures Calvino was preparing to give at Harvard in 1985-1986. He died before he could deliver them, and only five of the six memos were finished. The published book contains those five: Lightness, Quickness, Exactitude, Visibility, and Multiplicity.

The book is short. It is also incredibly dense. These are lectures about what Calvino thought literature needed to carry into the next thousand years, and they read less like a set of essays and more like an attempt to name and preserve a handful of properties before they slip out of the culture.

This post is a small selection of notes on the first two memos - Lightness and Quickness - which are the two that hit hardest for us on this reading.

Lightness

Lightness, as Calvino uses the word, is not the same as frivolity. He is very careful about this. It is closer to what Valéry meant when he wrote about being "light like a bird, and not like a feather" - a chosen agility of thought, as opposed to weightlessness by default.

The image Calvino returns to over and over is Perseus and the Gorgon. Perseus cannot look directly at Medusa without being turned to stone. He has to look at her reflection in his polished shield. Then, once he has beheaded her, her blood gives birth to Pegasus - the winged horse. The heaviness of stone transforms into flight. Even the winged sandals that let Perseus fly came from the world of monsters, from Medusa's sisters the Graiae, who shared one tooth and one eye between the three of them.

The point Calvino is making is that lightness is not the absence of weight. It is a response to weight. Perseus does not pretend the monster is not there. He looks at it sideways, in a mirror, and finds a way to carry it that does not destroy him.

There is a passage in the Lightness lecture that we have thought about often since reading it:

Whenever humanity seems condemned to heaviness, I think I should fly like Perseus into a different space. I don't mean escaping into dreams or into the irrational. I mean that I have to change my approach, look at the world from a different perspective, with a different logic and with fresh methods of cognition and verification.

Calvino also has a very unexpected riff on software. Remember, this is 1985. He writes about the second industrial revolution not producing the "crushing images" of rolling mills and molten steel, but bits traveling along circuits as electronic impulses:

The iron machines still exist, but they obey the orders of weightless bits.

For a set of lectures written in 1985 by an Italian novelist, that reads as unsettlingly current. Substitute "large language model" for "bits" and it barely changes.

The lecture ends with a memorable image, which we will just quote:

Were I to choose an auspicious image for the new millennium, I would choose that one: the sudden agile leap of the poet-philosopher who raises himself above the weight of the world, showing that with all his gravity he has the secret of lightness, and that what many consider to be the vitality of the times - noisy, aggressive, revving and roaring - belongs to the realm of death, like a cemetery for rusty old cars.

Quickness

The Quickness lecture is the one that made us laugh out loud a few times.

Calvino's central paradox in this memo is that quickness in literature is often produced by slowing down. He talks about Laurence Sterne's Tristram Shandy - a novel that is essentially all digression, and which Diderot then took as a model - and argues that digression is one of the main techniques literature has developed for slowing time down:

In practical life, time is a form of wealth with which we are stingy. In literature, time is a form of wealth to be spent at leisure and with detachment. We don't have to be first past a predetermined finish line. On the contrary, saving time is a good thing because the more time we save, the more we can afford to lose.

He follows this with a wonderful line about digressions as a hiding place:

If a straight line is the shortest distance between two fated and inevitable points, digressions will lengthen it; and if these digressions become so complex, so tangled and tortuous, so rapid as to hide their own tracks, who knows - perhaps death may not find us, perhaps time will lose its way, and perhaps we ourselves can remain concealed in our shifting hiding places.

And then his personal motto, which is now our personal motto too:

From my youth on, my personal motto has been the old Latin tag, festina lente, hurry slowly.

The best story in the Quickness lecture, though, is the one about Chuang-tzu and the crab. Here is Calvino's telling in full:

Among Chuang-tzu's many skills, he was an expert draftsman. The king asked him to draw a crab. Chuang-tzu replied that he needed five years, a country house, and twelve servants. Five years later, the drawing was still not begun. "I need another five years," said Chuang-tzu. The king granted them. At the end of these ten years, Chuang-tzu took up his brush and, in an instant, with a single stroke, he drew a crab, the most perfect crab ever seen.

This is the whole book in a paragraph. Quickness is the payoff of an enormous investment of slowness. The single stroke exists because of the ten years of preparation. Speed is a property that expensive preparation lets you buy, and cheap speed is not the same thing at all.

Why We Wrote This Down

Our reason for keeping notes on this book is that Calvino is doing something we recognize from our own attempts to build a zettelkasten - he is trying to name properties of thought that are useful, so they can be carried forward and returned to.

Lightness. Quickness. Exactitude. Visibility. Multiplicity. Those are five words to keep on the wall.

References

Tags:    reading    calvino    literature    notes    lightness    quickness   

November 2026

Baby Mac Crypto

March 2022

How to Read Ulysses

July 2020

Applied Gitflow

September 2019

Mocking AWS in Unit Tests

November 2017

A Hard(y) Math Problem