Web Cybersecurity
Web Cybersecurity
Web Cybersecurity
2. Web Security
Site A cannot steal data from your device, install malware, access camera, etc.
Network Attacker
Attack Models
Malicious Website Malicious External Resource
http://cs155.stanford.edu:80/lectures?lecture=08#slides
scheme domain port path query string fragment id
Anatomy of Request
HTTP Request
body
(empty)
HTTP Response
HTTP Response
status
HTTP/1.0 200 OK code
Date: Sun, 21 Apr 1996 02:20:42 GMT
Server: Microsoft-Internet-Information-Server/5.0
Content-Type: text/html
headers
Last-Modified: Thu, 18 Apr 1996 17:39:05 GMT
Content-Length: 2543
🙅 Don’t do…
GET http://bank.com/transfer?fromAccount=X&toAccount=Y&amount=1000
HTTP → Website
When you load a site, your web browser sends a GET request to that website
stanford.edu
http://example.com
GET /index.html
stanford.edu
Loading Resources
Root HTML page can include additional resources like images, videos, fonts
After parsing page HTML, your browser requests those additional resources
stanford.edu
http://example.com
GET /img/usr.jpg
<img src=“/imc/usr.jpg”></img>
stanford.edu
External Resources
There are no restrictions on where you can load resources like images
http://example.com
GET /img/usr.jpg
<img src=“/imc/user.jpg”></img>
<img src=“https://bank.com/img/usr.jpg”>
</img>
bank.com
(i)Frames
Beyond loading individual resources, https://a.com
c.com
• iFrame: floating inline frame a.com
<p id=“demo"></p>
<script>
document.getElementById(‘demo').innerHTML = Date()
</script>
Basic Execution Model
Each browser window….
- Loads content of root page
- Parses HTML and runs included Javascript
- Fetches sub resources (e.g., images, CSS, Javascript, iframes)
- Responds to events like onClick, onMouseover, onLoad, setTimeout
HTTP/2
Major revision of HTTP released in 2015
- Header Compression
- Server push
Cookies + Sessions
HTTP is Stateless
HTTP Request
GET /index.html HTTP/1.1
HTTP Response
HTTP/1.0 200 OK
Content-Type: text/html
<html>Some data... </html>
The browser may store and send back in future requests to that site
Session Management
Logins, shopping carts, game scores, or any other session state
Personalization
User preferences, themes, and other settings
Tracking
Recording and analyzing user behavior
Setting Cookie
HTTP Response
HTTP/1.0 200 OK
Date: Sun, 21 Apr 1996 02:20:42 GMT
Server: Microsoft-Internet-Information-Server/5.0
Connection: keep-alive
Content-Type: text/html
Set-Cookie: trackingID=3272923427328234
Set-Cookie: userID=F3D947C2
Content-Length: 2543
http://example.com
GET /img/usr.jpg
<img src=“/imc/user.jpg”></img>
<img src=“https://bank.com/img/usr.jpg”>
</img>
bank.com
…for better or worse…
Cookies set be a domain are always sent for any request to that domain
http://example.com
GET /transfer?…
<img src=“https://bank.com/transfer?
fromAccount=X
&toAccount=Y
&amount=1000”></img>
bank.com
Modern Website
Modern Website
Objects (What?)
- Files, directories
Objects
DOM tree, DOM storage, cookies, javascript namespace, HW permission
bank.com
http://example.com
attacker.com
http://example.com
Bounding Origins — Windows
Every Window and Frame has an origin
bank.com
http://example.com
attacker.com
http://example.com
attacker.com cannot…
bank.com
http://example.com
bank.com
http://example.com
If Tab 1 logins into bank.com, then Tab 2’s requests also send the cookies
received by Tab 1 to bank.com.
Both tabs share the same origin and have access to each others cookies
BroadcastChannel API
The BroadcastChannel API allows same-origin scripts to send messages
to other browsing contexts. Simple pub/sub message bus between
windows/tabs, iframes, web workers, and service workers.
attacker.com
http://example.com
attacker.com cannot…
Sender:
targetWindow.postMessage(message, targetOrigin, [transfer]);
targetWindow: ref to window (e.g., window.parent, window.frames)
Receiver:
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event){
alert(“message received”)
}
Same Origin Policy
(HTTP Responses)
SOP for HTTP Responses
Pages can make requests across origins
GET /img/usr.jpg
attacker.com
http://example.com
<img src=“https://bank.com/img/usr.jpg”>
</img>
bank.com
attacker.com
https://a.com
✗ bank.com
attacker.com
bank.com
Script Execution
Scripts can be loaded from other origins. Scripts execute with the privileges
of their parent frame/window’s origin. Cannot view source, but can call FNs
bank.com
❌ If you load a malicious
library, it can also steal
<script src="jquery.com/jquery.min.js"></script> your data (e.g., cookie)
Domain Relaxation
facebook.com
http://example.com
Frame A
Origin: cdn.facebook.com
Domain Relaxation
You can change your document.domain to be a super-domain
a.domain.com → domain.com OK
b.domain.com → domain.com OK
Frame: cdn.facebook.com
<script>
document.domain = facebook.com
</script>
Domain Relaxation Attacks
cs155.stanford.edu
http://example.com
Frame: stanford.edu
<script>
document.domain = stanford.edu
</script>
Relaxation Attacks
Solution:
Both sides must set document.domain to share data
Same Origin Policy
(Javascript)
Javascript XMLHttpRequests
Javascript can make network requests to load additional content or submit forms
You can only read responses if they’re from the same origin (or you’re given
permission by the destination origin to read their data)
$.post({url: “api.c.com/x“,
Origin:
success: function(r){
api.c.com
$("#div1").html(r); Header:
} Access-Control-Allow-Origin:
}); http://app.c.com
POST /x
DATA
Wildcard Origins
POST /x OPTIONS /x
Origin: app.c.com
$.post({url: “api.c.com/x“,
Origin:
success: function(r){
api.c.com
$("#div1").html(r);
Header:
}
Access-Control-Allow-Origin: *
});
POST /x
DATA
CORS Failure
POST /x OPTIONS /x
Origin: app.c.com
$.post({url: “api.c.com/x“,
Origin:
success: function(r){
api.c.com
$("#div1").html(r); Header:
} Access-Control-Allow-Origin:
}); https://www.c.com
ERROR
*Usually: Simple Requests
⚠ Not all requests result in a Pre-Fetch trip. “Simple” requests do not. Must
meet all of the following criteria:
These mimic the types of requests that could be made without Javascript
e.g., submitting form, loading image, or page
Simple CORS Success
GET /x GET /x
Origin: app.c.com
$.ajax({url: “api.c.com/x“,
Origin:
success: function(r){
api.c.com
$("#div1").html(r); Header:
} Access-Control-Allow-Origin:
}); http://app.c.com
Simple CORS Failure
GET /x GET /x
Origin: app.c.com
$.ajax({url: “api.c.com/x“,
Origin:
success: function(r){
api.c.com
$("#div1").html(r); Header:
} Access-Control-Allow-Origin:
}); https://www.c.com
ERROR
!reading != !attack
GET /x
Origin: attacker.com http://bank.com/transfer?
fromAccount=X
$.ajax({url: “bank.com/t",
success: function(r){
&toAccount\=Y
Bank
$("#div1").html(r); &amount\=1000
}
});
Header:
Access-Control-Allow-Origin:
https://bank.com
ERROR
Same Origin Policy
(Cookies)
Cookie Same Origin Policy
Cookies use a different origin definition:
cookie domain is suffix of URL domain ∧ cookie path is a prefix of URL path
checkout.site.com No Yes No
login.site.com Yes Yes No
login.site.com/my/home Yes Yes Yes
site.com/account No Yes No
Setting Cookie Scope
Websites can set a scope to be any prefix of domain and prefix of path
No Domain Cookies
Most websites do not set Domain. In this situation, cookie is scoped to the
hostname the cookie was received over and is not sent to subdomains
site.com
❌
subdomain.site.com
Policy Collisions
Cookie SOP Policy
cs.stanford.edu/zakir cannot see cookies for cs.stanford.edu/dabo
Yes!
Yes!
domain: bank.com
name: authID
value: auth
Problem with HTTP Cookies
Network Attacker
Can Observe/Alter/Drop Traffic
HTTPS Connection
bank.com
domain: bank.com
name: authID
value: auth
domain: bank.com
name: authID
value: auth
bank.com
domain: bank.com
name: authID
value: auth
Secure Cookies
Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure;
A secure cookie is only sent to the server with an encrypted request over the
HTTPS protocol.
Web Security Model
CS155 Computer and Network Security