Skip to content

Commit ec7eab4

Browse files
authored
Merge pull request #14 from wiseaidev/chapter-6
add chapter 6: sql injection exploitation sections
2 parents e50d7fe + caaa8c3 commit ec7eab4

File tree

8 files changed

+862
-1
lines changed

8 files changed

+862
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ Cargo.lock
1414
*.pdb
1515

1616
# These are files generated by Jupyter
17-
.ipynb_checkpoints
17+
.ipynb_checkpoints
18+
19+
**/*.sqlite

chapter-6/README.md

Lines changed: 319 additions & 0 deletions
Large diffs are not rendered by default.

chapter-6/chapter-6.ipynb

Lines changed: 430 additions & 0 deletions
Large diffs are not rendered by default.

chapter-6/chapter-6.pdf

192 KB
Binary file not shown.

chapter-6/sql-injection/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "sql-injection"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rocket = "0.5.0"
10+
rocket_db_pools = { version = "0.1.0", features = ["sqlx_sqlite"] }

chapter-6/sql-injection/Rocket.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[default.databases.sqlite_db]
2+
url = "db.sqlite"
3+
4+
# only `url` is required. the rest have defaults and are thus optional
5+
min_connections = 64
6+
max_connections = 1024
7+
connect_timeout = 5
8+
idle_timeout = 120

chapter-6/sql-injection/db.sqlite

8 KB
Binary file not shown.

chapter-6/sql-injection/src/main.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#[macro_use]
2+
extern crate rocket;
3+
use rocket::Error;
4+
5+
use rocket::form::Form;
6+
use rocket_db_pools::sqlx::{self, Row};
7+
use rocket_db_pools::{Connection, Database};
8+
9+
#[derive(Database)]
10+
#[database("sqlite_db")]
11+
struct DbConn(sqlx::SqlitePool);
12+
13+
#[derive(Debug, FromForm)]
14+
struct UserData {
15+
username: String,
16+
password: String,
17+
}
18+
19+
#[post("/login", data = "<user_data>")]
20+
async fn login(mut conn: Connection<DbConn>, user_data: Form<UserData>) -> Result<String, String> {
21+
sqlx::query(
22+
r#"
23+
CREATE TABLE IF NOT EXISTS users (
24+
username TEXT,
25+
password TEXT
26+
);"#,
27+
)
28+
.execute(&mut **conn)
29+
.await
30+
.unwrap();
31+
32+
let username = &user_data.username;
33+
let password = &user_data.password;
34+
35+
let query_result = sqlx::query(&format!(
36+
"SELECT * FROM users WHERE username = '{}' AND password = '{}'",
37+
username, password
38+
))
39+
.fetch_one(&mut **conn)
40+
.await
41+
.and_then(|r| {
42+
let username: Result<String, _> = Ok::<String, Error>(r.get::<String, _>(0));
43+
let password: Result<String, _> = Ok::<String, Error>(r.get::<String, _>(1));
44+
Ok((username, password))
45+
})
46+
.ok();
47+
48+
match query_result {
49+
Some((username, password)) => Ok(format!(
50+
"username: {}, password: {}",
51+
username.unwrap(),
52+
password.unwrap()
53+
)),
54+
None => Err("User not found".into()),
55+
}
56+
}
57+
58+
#[post("/register", data = "<user_data>")]
59+
async fn register(
60+
mut conn: Connection<DbConn>,
61+
user_data: Form<UserData>,
62+
) -> Result<String, String> {
63+
sqlx::query(
64+
r#"
65+
CREATE TABLE IF NOT EXISTS users (
66+
username TEXT,
67+
password TEXT
68+
);"#,
69+
)
70+
.execute(&mut **conn)
71+
.await
72+
.unwrap();
73+
74+
let username = &user_data.username;
75+
let password = &user_data.password;
76+
77+
sqlx::query("INSERT INTO users (username, password) VALUES (?, ?)")
78+
.bind(username)
79+
.bind(password)
80+
.execute(&mut **conn)
81+
.await
82+
.unwrap();
83+
84+
Ok("Signed up successfully!".to_string())
85+
}
86+
87+
#[launch]
88+
fn rocket() -> _ {
89+
rocket::build()
90+
.attach(DbConn::init())
91+
.mount("/", routes![register, login])
92+
}

0 commit comments

Comments
 (0)