CS 161 Final Review
May 6, 2020
CS 161 Course Staff
Announcements
Table of contents
Networking
Layering
Layering
OSI Layer 1: Physical layer
OSI Layer 1: Physical layer
OSI Layer 2: Link layer
OSI Layer 2: Link layer
MAC addresses
Attacker Definitions
(for the purposes of this class)
Remember all attackers can send whatever packets they want!
Think about what fields an attacker would need to guess correctly!
ARP: Address Resolution Protocol
ARP: Address Resolution Protocol
Converts IP addresses to MAC addresses.
(If the X is outside the local network, then the gateway responds with their MAC address)
ARP: Spoofing
The ARP request is public. What happens if
ARP: Spoofing
Alice now believes Mallory’s MAC address is Bob’s and will send her packets intended for Bob.
DHCP: Dynamic Host Configuration Protocol
DHCP: Dynamic Host Configuration Protocol
When you first connect to a network, you need a few things:
DHCP: Handshake
Client Discover: “I need configuration information!”
Server Offer: “Here’s some possible configurations: A, B, C”
Client Request: “I want to use configuration C”
Server Acknowledge: “Okay, I have given you config C”
Client messages are broadcast across the local network
DHCP: Spoofing
What happens when Alice accepts Mallory’s configuration?
Mallory now controls Alice’s:
What can she do with this?
OSI Layer 3: (Inter)network Layer
OSI Layer 3: (Inter)network Layer
Headers: IP
0 | Version | IHL | DSCP | ECN | Total Length | |||||||||||||||||||||||||||
32 | Identification | Flags | Fragmentation Offset | |||||||||||||||||||||||||||||
64 | Time to Live | Protocol | Header Checksum | |||||||||||||||||||||||||||||
96 | Source IP Address | |||||||||||||||||||||||||||||||
128 | Destination IP Address | |||||||||||||||||||||||||||||||
160 | Payload: arbitrary data | |||||||||||||||||||||||||||||||
OSI Layer 4: Transport Layer
OSI Layer 4: Transport Layer
Headers: UDP
0 | 16-bit source port | 16-bit destination port |
32 | 16-bit length field | 16-bit checksum |
64 | Payload: arbitrary data | |
Quick detour: Ports
Quick detour: Ports
TCP: Transmission Control Protocol
Three-way handshake:
The SEQ and ACK values swap depending on who’s sending the message. (You ACK the SEQ you receive.)
Headers: TCP
0 | 16-bit source port | 16-bit destination port | ||||||||||||||||||||||||||||||
32 | Sequence number | |||||||||||||||||||||||||||||||
64 | Acknowledgment number (if ACK set) | |||||||||||||||||||||||||||||||
96 | Data Offset | Reserved | Flags | Window size | ||||||||||||||||||||||||||||
128 | Checksum | Urgent pointer | ||||||||||||||||||||||||||||||
160 | Payload: arbitrary data | |||||||||||||||||||||||||||||||
TCP: Vulnerabilities
Layer 6.5: Secure communication
Layer 6.5: Secure communication
TLS
HTTPS Connection (SSL / TLS)
Browser
Amazon
Server
Hello. My rnd # = RB. I support
(TLS+RSA+AES128+SHA1) or
(SSL+RSA+3DES+MD5) or …
My rnd # = RS. Let’s use
TLS+RSA+AES128+SHA1
Here’s my cert
~2-3 KB of data
PS via RSA
Browser
Here’s my cert
~2-3 KB of data
{PS}PKAmazon
PS
PS
Amazon
Server
Q: Forward secrecy?
A: No forward secrecy because attacker can decrypt PS and knows RB, and RS and computes secrets
PS via Diffie-Hellman
Browser
Here’s my cert
~2-3 KB of data
gb mod p
PS
PS
{g, p, ga mod p} SKAmazon
Amazon
Server
Q: Forward secrecy?
A: Has forward secrecy because shared secret never sent over the network! If attacker as SKAmazon, cannot find a.
Symmetric Key Generation
Browser
PS
PS
Amazon
Server
{M1, MAC(M1,IB)}CB
MAC(dialog,IS)
MAC(dialog,IB)
…
TLS
Nice property of TLS: End-to-end security
TLS Limitations/Issues
OSI Layer 7: Application Layer (aka L5)
If you receive an HTTP Packet it might look like this
IEEE 802.2 Headers |
IP Headers |
TCP Headers |
HTTP Headers |
HTTP Body |
What is the TCP payload?
What is the IP payload?
If you receive an HTTP Packet it might look like this
IEEE 802.2 Headers |
IP Headers |
TCP Headers |
HTTP Headers |
HTTP Body |
What is the TCP payload?
What is the IP payload?
If you receive an HTTP Packet it might look like this
IEEE 802.2 Headers |
IP Headers |
TCP Headers |
HTTP Headers |
HTTP Body |
What is the TCP payload?
What is the IP payload?
DNS
DNS: Domain Name System
Protocol for translating human-friendly domain names to a numerical address: www.berkeley.edu -> 52.10.99.247
Nameservers at least responsible for pointing to you in the right direction: .edu’s name server might not know how to get to www.berkeley.edu, but it knows that berkeley.edu’s name server knows and that name server’s IP address.
DNS: Example (borrowed from cs168)
DNS client�(me.cs.berkeley.edu)
DNS server
.org NS
wikipedia.org servers
Where is www.wikipedia.org?
Where is www.wikipedia.org?
Where is www.wikipedia.org?
Where is www.wikipedia.org?
Go ask .org nameserver at 1.2.3.4
Go ask wikipedia.org server at 5.6.7.8
Answer record: 9.10.11.12
ROOT
DNS: Resource Record (RR) Types
Each RR has a type and TTL (How long the record should be cached)
berkeley.edu 600 A 35.163.72.93
edu. 172800 NS a.edu-servers.net
A typical DNS response: “Hey I found what you’re asking for”
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39459
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION: (What domain was queried for)
;berkeley.edu. IN A
;; ANSWER SECTION: (What the address of the queried domain is)
berkeley.edu. 16 IN A 35.163.72.93
;; AUTHORITY SECTION: (What the name servers responsible for berkeley.edu are)
berkeley.edu. 43860 IN NS adns2.berkeley.edu.
berkeley.edu. 43860 IN NS adns1.berkeley.edu.
;; ADDITIONAL SECTION: (Possibly helpful additional information: Where the nameservers are)
adns1.berkeley.edu. 90569 IN AAAA 2607:f140:ffff:fffe::3
adns2.berkeley.edu. 693 IN A 128.32.136.14
adns2.berkeley.edu. 1598 IN AAAA 2607:f140:ffff:fffe::e
Another typical DNS response: “I don’t know, but I can tell you who does and where they are”
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53577
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 2, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION: (What domain was queried for)
;berkeley.edu. IN A
;; ANSWER SECTION: (What the address of the queried domain is. Note the lack of value)
;; AUTHORITY SECTION: (What the name servers responsible for .edu are)
edu. 172800 IN NS a.edu-servers.net.
edu. 172800 IN NS b.edu-servers.net.
;; ADDITIONAL SECTION: (Possibly helpful additional information: Where the nameservers are)
a.edu-servers.net. 172800 IN A 192.5.6.30
b.edu-servers.net. 172800 IN A 192.33.14.30
Headers: DNS
0 | Identification | |||||||||||||||
16 | QR | Opcode | AA | TC | RD | RA | (Just zeroes) | Response Code | ||||||||
32 | QDCOUNT: Number of resource records in question section | |||||||||||||||
48 | ANCOUNT: Number of resource records in answer section | |||||||||||||||
64 | NSCOUNT: Number of resource records in authority section | |||||||||||||||
80 | ARCOUNT: Number of resource records in authority section | |||||||||||||||
96 | Arbitrary Data | |||||||||||||||
DNS: Vulnerabilities
What do spoofers need to guess correctly to be accepted by the victim?
DNS: Off-path spoofing
Eve
DNS resolver
wikipedia.org NS
Where is www.wikipedia.org?
www.wikipedia.org 1200 A 198.35.26.96
www.wikipedia.org 1200 A 6.6.6.6
Eve can’t see the source UDP port or the DNS ID field in this scenario.
DNS: Kaminsky Attack
DNS: Kaminsky attack
Eve
DNS resolver
wikipedia.org NS
Where is aaaaaaa.wikipedia.org?
aaaaaaa.wikipedia.org does not exist
wikipedia.org 1200 NS ns1.wikipedia.org
ns1.wikipedia.org A 6.6.6.6
If this attack succeeds, then any future query for any domain in wikipedia.org will be sent to Eve.
If it doesn’t, it doesn’t matter. The cache has not been altered. Just ask on another random subdomain.
DNSSEC
Why DNSSEC?
DNS provides poor security, so DNSSEC was introduced as an alternative, guaranteeing these properties:
DNSSEC
I’m not making DNSSEC slides. Let’s go to https://cs161.org/assets/lectures/lec21_dnssec.pdf
Start at slide 11.
NSEC
NSEC
berkeley.edu
berkeley.edu
a.berkeley.edu
ab.berkeley.edu
cd.berkeley.edu
Give me an NSEC for bc.berkeley.edu.
(Prove to me that no such domain bc.berkeley.edu exists.)
ab.berkeley.edu. NSEC cd.berkeley.edu.
ab.berkeley.edu. RRSIG NSEC <signature>
Enumerating NSEC
DoS, Firewall, and Detection
Denial of Service
Denial of Service Techniques
Problem - Unequal Playing Field
Denial of Service Techniques
Denial of Service Mitigation
Firewalls
Stateless vs Stateful
Firewalls cont.
Firewall Disadvantages
Firewall Advantages
Detection
Base Rate Fallacy
Detection Strategies
See lecture 25 for more details
End of networking.
Cryptography
Cryptography: Symmetric Keys
Chosen Plaintext Attack (IND-CPA)
Adversary (Attacker)
Challenger (Benign)
“Hello”, “World”
Ek(“Hello”) OR Ek(“World”)
Choose a message
M
Ek(M)
polynomially many
Adversary wins if they guess correctly with probability > ½
You encrypted: “Hello”/“World”
ECB
Why is this bad?
ECB
If P1 = P2, C1 = C2!
Ask challenger to re-encrypt both messages and see which one matches!
AES
Design
Patterns
OTP-like
Examples: OFB, CFB, CTR
Properties:
Plaintext does not need to be padded
Use encryption block on decrypt
Decrypt looks similar to encrypt
*On non OTP-like schemes,
decrypt looks like a mirror image
of encrypt.
Identification:
Plaintext xored with AES output
CTR
Identification: Plaintext xored with AES output
Properties:
Plaintext does not need to be padded
Use encryption block on decrypt
Decrypt looks similar to encrypt
Feedback
Examples: OFB, CFB, CBC
Properties:
Plaintext feedback:
Decryption not parallelizable
Ciphertext/output feedback:
Encryption not parallelizable
Identification:
Arrow from one block to the next.
CBC
Identification:
Arrow from one block to the next.
Properties:
Plaintext feedback:
Decryption not parallelizable
Ciphertext/output feedback:
Encryption not parallelizable
IV reuse litmus test
Three source of entropy: key, IV, plaintext
Default:
OTP reuse on entire ciphertext.
Plaintext-incorporated (plaintext or ciphertext) feedback:
OTP reuse only up to but NOT including block containing first difference.
OTP-like:
+ OTP reuse on block containing first difference.
Asymmetric Cryptography
Diffie-Hellman Key Exchange
Public parameters g and p
The secrets a and b
Disguise your secret with the common paint.
g^a mod p, g^b mod p
Add in the missing piece.
(g^a)^b mod p
(g^b)^a mod p
g^ab mod p
Swap disguised secrets.
MITM Diffie-Hellman
ElGamal
Hashing / PRNG
Properties of Hash Functions
Pseudorandom Number Generators
Deterministic way of generating bits which look random (pseudo-random) given some actually random bits
Initialize(entropy): Given some truly random bits "entropy" initializes the internal state of the RNG
Generate(n): Generate n pseudorandom bits
Reseed(entropy): Add more entropy to the PRNG
Properties of pRNG
End of cryptography.
Web Security
What can you do with Javascript?
Change HTML content, images, style of elements, hide elements, unhide elements, change cursor, read and change cookies...
Read cookie with JS:
var x = document.cookie;
Change cookie with JS:
document.cookie = "username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
Outer page can specify only sizing and placement of the frame in the outer page.
We can load frames from different websites!
Frame isolation: Outer page cannot change contents of inner page. Inner page cannot change contents of outer page.
This is an important exception to the same-origin policy!
Can Access?
Originating | Trying to Access |
http://wikipedia.org/a/ | http://wikipedia.org/b/ |
http://wikipedia.org/ | http://www.wikipedia.org/ |
http://wikipedia.org/ | https://wikipedia.org/ |
http://wikipedia.org:81/ | http://wikipedia.org:82/ |
http://wikipedia.org:81/ | http://wikipedia.org/ |
Can Access?
Originating | Trying to Access |
http://wikipedia.org/a/ | http://wikipedia.org/b/ |
http://wikipedia.org/ | http://www.wikipedia.org/ |
http://wikipedia.org/ | https://wikipedia.org/ |
http://wikipedia.org:81/ | http://wikipedia.org:82/ |
http://wikipedia.org:81/ | http://wikipedia.org/ |
Cookies
Session management is like wristbands...
Cookies are also like wristbands...
URL breakdown
https://box.unicornbox.com/files/evanbot/doodle-ai.go
URL breakdown
https://box.unicornbox.com/files/evanbot/doodle-ai.go
When breaking down a URL, make a split at the double-slash...
then another split at the first slash after that.
URL breakdown
https://box.unicornbox.com/files/evanbot/doodle-ai.go
Part 1: Protocol
URL breakdown
https://box.unicornbox.com/files/evanbot/doodle-ai.go
Part 2: Domain
URL breakdown
https://box.unicornbox.com/files/evanbot/doodle-ai.go
Part 3: Path
Cookie policy
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
Use setting rules if the server is setting the cookie (handing out a wristband).
Use sending rules if the browser is sending the cookie (showing a wristband).
Use domain rules on the domain part of the URL.
Use path rules on the path part of the URL.
Cookie policy rule 1
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the domain part of the URL.
X can set a cookie with domain Y if X ends with Y and Y is not a TLD.
X = box.unicornbox.com
Y = admin.box.unicornbox.com
X ends in Y? False. Cookie is not set.
Cookie policy rule 1
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the domain part of the URL.
X can set a cookie with domain Y if X ends with Y and Y is not a TLD.
X = box.unicornbox.com
Y = box.unicornbox.com
X ends in Y? True. Cookie is set.
Cookie policy rule 1
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the domain part of the URL.
X can set a cookie with domain Y if X ends with Y and Y is not a TLD.
X = box.unicornbox.com
Y = evanbot.unicornbox.com
X ends in Y? False. Cookie is not set.
Cookie policy rule 1
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the domain part of the URL.
X can set a cookie with domain Y if X ends with Y and Y is not a TLD.
X = box.unicornbox.com
Y = unicornbox.com
X ends in Y? True. Cookie is set.
Cookie policy rule 1
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the domain part of the URL.
X can set a cookie with domain Y if X ends with Y and Y is not a TLD.
X = box.unicornbox.com
Y = cornbox.com
X ends in Y? False. Cookie is not set.
Note that “ends with” is domain matching, not string matching. Everything between the dots must match.
Cookie policy rule 1
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the domain part of the URL.
X can set a cookie with domain Y if X ends with Y and Y is not a TLD.
X = box.unicornbox.com
Y =
X ends in Y? True. Cookie is set.
Cookie policy rule 1
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the domain part of the URL.
X can set a cookie with domain Y if X ends with Y and Y is not a TLD.
X = box.unicornbox.com
Y = .com
X ends in Y? True. But cookie is not set due to exception.
TLDs include: .com, .org, .edu.
Cookie policy rule 2
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the path part of the URL.
X can set a cookie with path Y always.
X = box.unicornbox.com
Y = /files
Always? Always. Cookie is set.
Cookie policy rule 2
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for servers setting cookies (handing out wristbands).
This rule applies to the path part of the URL.
X can set a cookie with path Y always.
X = box.unicornbox.com
Y = /
Always? Always. Cookie is set.
Cookie policy rule 3
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for browsers sending cookies (showing wristbands).
This rule applies to the domain part of the URL.
Browser sends X a cookie with domain Y if X ends with Y.
X = https://box.unicornbox.com/files/evanbot/doodle-ai.go
Y = unicornbox.com
X ends in Y? True. Cookie is sent.
Cookie policy rule 4
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
This rule is for browsers sending cookies (showing wristbands).
This rule applies to the path part of the URL.
Browser sends X a cookie with path Y if X begins with Y.
X = https://box.unicornbox.com/files/evanbot/doodle-ai.go
Y = /files/evanbot
X begins with Y? True. Cookie is sent.
Cookie policy rule 3-4 shortcut
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
To determine if a cookie is sent, I like to use this matching trick combining rules 3-4.
https://box.unicornbox.com/files/evanbot/doodle-ai.go
unicornbox.com/files/evanbot
(domain)
Concatenate the cookie domain and path. Line it up below the requested URL at the first single slash.
(path)
Cookie policy rule 3-4 shortcut
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
To determine if a cookie is sent, I like to use this matching trick combining rules 3-4.
https://box.unicornbox.com/files/evanbot/doodle-ai.go
unicornbox.com/files/evanbot
(domain)
Can you get everything to match? If yes, cookie is sent.
(path)
Cookie policy rule 3-4 shortcut
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
To determine if a cookie is sent, I like to use this matching trick combining rules 3-4.
https://box.unicornbox.com/files/evanbot/doodle-ai.go
unicornbox.com/files/admin
(domain)
Can you get everything to match? If no, cookie is not sent.
(path)
Cookie policy rule 3-4 shortcut
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
To determine if a cookie is sent, I like to use this matching trick combining rules 3-4.
https://box.unicornbox.com/files/evanbot/doodle-ai.go
cornbox.com/files/evanbot
(domain)
Can you get everything to match? If no, cookie is not sent.
(path)
Cookie policy: in summary
| Domain | Path |
Setting | Rule 1 | Rule 2 |
Sending | Rule 3 | Rule 4 |
Cookies
Domain | Where Cookie Would Be SENT (if set) |
(value omitted) | foo.example.com (exact) |
bar.foo.example.com | ? |
foo.example.com | foo.example.com and *.foo.example.com |
baz.example.com | ? |
example.com | ? |
ample.com | Cookie not set! Different domain. |
.com | ? |
A web server on foo.example.com tries to set a cookie with the following domains. Will the cookie be set? If so, which domains will receive the cookie? (Some rows are filled in as examples.)
Domain | Where Cookie Would Be SENT (if set) |
(value omitted) | foo.example.com (exact) |
bar.foo.example.com | Cookie not set! Domain too specific |
foo.example.com | foo.example.com and *.foo.example.com |
baz.example.com | Cookie not set! Domain mismatch |
example.com | example.com and *.example.com |
ample.com | Cookie not set! Different domain. |
.com | Cookie not set! Too broad. |
A web server on foo.example.com tries to set a cookie with the following domains. Will the cookie be set? If so, which domains will receive the cookie? (Some rows are filled in as examples.)
SQL Injection
SQL injection step 1: what does the SQL interpreter see?
SQL injection step 1: what does the SQL interpreter see?
> INSERT INTO cart (session, item)� VALUES ('____________', '____________')
sessionToken item
SQL injection step 2: what do we want the SQL interpreter to see?
SQL injection step 2: what do 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')
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')
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')
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
Prepared statements: the previous attack no longer works
We can’t do injection in sessionToken
SQL injection step 1: what does the SQL interpreter see?
> INSERT INTO cart (session, item) VALUES ('____________', '____')
sessionToken item
> SELECT item FROM cart WHERE session = '____________' LIMIT 1
sessionToken
> SELECT link FROM items WHERE item = '_______'
item
The third query is the vulnerable one, but the first query is where the malicious input happens
SQL injection step 2: what do we want the SQL interpreter to see?
The SQL interpreter sees:
> SELECT link FROM items WHERE item = '_______'
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')
SQL injection step 3: compare
The SQL interpreter sees:
> SELECT link FROM items WHERE item = '_______'
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')
Problem: the queries don’t match at the beginning
SQL injection step 3: compare
The SQL interpreter sees:
> SELECT link FROM items WHERE item = '_______'
item
We want the SQL interpreter to see:�
> SELECT link FROM items WHERE item = 'whatever';� INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper')
Solution: Add a dummy query to force the queries to match
SQL injection step 3: compare
The SQL interpreter sees:
> SELECT link FROM items WHERE item = '_______'
item
We want the SQL interpreter to see:�
> SELECT link FROM items WHERE item = 'whatever';� INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper')
Problem: the endings don’t match
SQL injection step 3: compare
The SQL interpreter sees:
> SELECT link FROM items WHERE item = '_______'
item
We want the SQL interpreter to see:�
> SELECT link FROM items WHERE item = 'whatever';� INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper') --'
Solution: use comments to force a match
SQL injection step 4: ??? step 5: profit
The SQL interpreter sees:
> SELECT link FROM items WHERE item = '_______'
item
We want the SQL interpreter to see:�
> SELECT link FROM items WHERE item = 'whatever';� INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper') --'
item = whatever';� INSERT INTO cart (session, item)� VALUES ('123', 'toilet paper'), ('123', 'toilet paper'),
('123', 'toilet paper'), ... ('123', 'toilet paper') --
Cross-Site Scripting (XSS)
Two types of XSS
Stored XSS:
Reflected XSS:
End of web security.
Good Luck!