SQL Injection: When Data Starts Giving the Database Orders
SQL injection happens when information supplied to an application changes the database instructions the application intended to send. SQL, short for Structured Query Language, is a language for working with databases—organized stores of data. A query is a request to read or change that data.
Text that should remain a value, such as a name to search for, becomes part of the command itself.
A search value should stay a value
Suppose a library website lets a visitor search its book catalogue by author. The intended database request is conceptually simple: find books whose author matches the supplied name.
The application needs two things: the structure of that request and the name entered by the visitor. If it builds the whole request by joining pieces of text without safely separating those roles, specially formed input may change what the database is told to do.
The issue isn't that the name looks strange to a person. It is that the database interprets some of the supplied text as instructions. A security weakness has allowed an outside party to influence the command's meaning.
How One Line of Text Can Hack a Website
The Open Worldwide Application Security Project (OWASP), a software-security nonprofit, explains in its SQL injection guidance why the source needn't be a visible form. Data can arrive from another service, an email, a directory or information already stored in a database. Being stored earlier doesn't make it safe to treat as command text later.
Keep the command and its values separate
A parameterized query supplies the command structure separately from the values it will use. The values are bound to designated places in the request. The database receives the search name as data. Characters in that name don't become part of the SQL command.
For the library search, the visitor can choose which author's name to look for. They don't get to rewrite the search operation through that name.
This is different from input validation, checking whether supplied data meets an application's requirements. A name may legitimately contain punctuation. Validation can check whether it fits the form's requirements; parameter binding keeps that name from changing the database command.
Parameters are for values, not replacements for every part of a query. If an application lets someone choose whether to sort results by author or title, for example, the program should map that choice to fixed, approved options. It shouldn't turn arbitrary user text into part of the command.
Moving the code into a stored procedure, a routine kept in the database, doesn't automatically solve the problem either. The routine can still assemble unsafe SQL inside itself. OWASP favors parameter binding over manually escaping values, which means trying to make text safe to insert by changing how special characters are written.
- Learn to find the flaws in a web app before someone else does.
- Understand injection, broken logins and logic bugs well enough to fix them.
- Build the skills that web security jobs ask for.
The database account determines how far harm can go
A vulnerable query may expose records, change data or bypass an unsafe login check. MITRE's SQL injection description lists these possible consequences; the actual result depends on the query and environment.
The application's database account also matters. An account allowed only to read a limited set of records has less authority available for misuse than one allowed to change or delete unrelated data. This is least privilege: granting the access the job needs without unnecessary powers.
Restricted permissions can limit damage. They don't repair a query that confuses data with instructions. The underlying repair restores that boundary, so an author name remains something to search for rather than something the database obeys.
Input Validation Attacks: When Software Accepts the Wrong Data

Web Servers: What Happens After the Browser Asks

Vulnerabilities: The Weakness Behind a Possible Attack

QUOTE:
"Amateurs hack systems, professionals hack people."
