Web Security Web Application Security - Spring 2020 Franziska (Franzi) Roesner

Page created by Shirley Miller
 
CONTINUE READING
Web Security Web Application Security - Spring 2020 Franziska (Franzi) Roesner
CSE 484 / CSE M 584: Computer Security and Privacy

                              Web Security
                   [Web Application Security]

                                      Spring 2020

                       Franziska (Franzi) Roesner
                       franzi@cs.washington.edu

Thanks to Dan Boneh, Dieter Gollmann, Dan Halperin, Yoshi Kohno, Ada Lerner, John Manferdelli, John
Mitchell, Vitaly Shmatikov, Bennet Yee, and many others for sample slides and materials ...
Web Security Web Application Security - Spring 2020 Franziska (Franzi) Roesner
Admin
• HW 2 due today
• Lab 2 out
      – Highly recommend the readings on the course schedule
• Late Days +2
      – This quarter is tough
      – Max of 3 per assignment
      – No late days for last final project deadline

5/4/2018                  CSE 484 / CSE M 584 - Spring 2020    2
Web Security Web Application Security - Spring 2020 Franziska (Franzi) Roesner
XSS Recap
 Search results
You have searched for …
Web Security Web Application Security - Spring 2020 Franziska (Franzi) Roesner
Preventing Cross-Site Scripting
• Any user input and client-side data must be
  preprocessed before it is used inside HTML
• Remove / encode HTML special characters
      – Use a good escaping library
           • OWASP ESAPI (Enterprise Security API)
           • Mic o of AntiXSS
      – In PHP, htmlspecialchars(string) will replace all
        special characters with their HTML codes
           • ‘ becomes ' “ becomes " & becomes &
      – In ASP.NET, Server.HtmlEncode(string)

5/4/2018                   CSE 484 / CSE M 584 - Spring 2020     4
Web Security Web Application Security - Spring 2020 Franziska (Franzi) Roesner
Evading XSS Filters
• Preventing injection of scripts into HTML is hard!
      – Blocking “” is not enough
      – Event handlers, stylesheets, encoded inputs (%3C), etc.
      – phpBB allowed simple HTML tags like 
           ” onmouseover=“script” x=“Hello
• Beware of filter evasion tricks (XSS Cheat Sheet)
      – If filter allows quoting (of , etc.), beware of
        malformed quoting: 
      – Long UTF-8 encoding
      – Scripts are not only in :
https://samy.pl/myspace/tech.html

                MySpace Worm (1)
• Users can post HTML on their MySpace pages
• M Space doe no allo c ip in e HTML
      – No , , onclick, 
•          b   doe allo          di            ag fo CSS
      – 
• B        M Space ill             ip o               javascript
      – Use “javascript” instead
• But MySpace will strip out quotes
      – Convert from decimal instead:
       alert('double quote: ' + String.fromCharCode(34))

5/4/2018                  CSE 484 / CSE M 584 - Spring 2020                              6
https://samy.pl/myspace/tech.html

                                  MySpace Worm (2)
                                                      Resulting code:
https://samy.pl/myspace/tech.html

                MySpace Worm (3)
•    The e e e a fe o he com lica ion and hing o ge a o nd
    This was not by any means a straight forward process, and none of
    this was meant to cause any damage or piss anyone off. This was in
    the interest of..interest I a in e e ing and f n
• S a ed on samy M Space page
• Everybody who visits an infected page, becomes
  infec ed and add samy a a f iend and he o
• ho la e samy ha                   f iend
      – Was adding 1,000 friends
        per second at its peak

5/4/2018                   CSE 484 / CSE M 584 - Spring 2020                              8
SQL Injection

5/8/2020    CSE 484 / CSE M 584 - Spring 2019   9
Typical Login Prompt

5/8/2020        CSE 484 / CSE M 584 - Spring 2019   10
Typical Query Generation Code

  $selecteduser = $_GET['user'];
  $sql = "SELECT Username, Key FROM Key " .
        "WHERE Username='$selecteduser'";
  $rs = $db->executeQuery($sql);

What if ser is a malicious string that changes the
meaning of the query?

5/8/2020           CSE 484 / CSE M 584 - Spring 2019   11
User Input Becomes Part of Query

                        Enter                         SELECT passwd
                      Username                         FROM USERS
                          &                           WHERE uname
            Web       Password                          IS ‘$user’
                                   Web
           browser                                                    DB
                                  server
           (Client)

5/8/2020                     CSE 484 / CSE M 584 - Spring 2019             12
Normal Login

                        Enter                         SELECT passwd
                      Username                         FROM USERS
                          &                           WHERE uname
            Web       Password                          IS ‘franzi’
                                   Web
           browser                                                    DB
                                  server
           (Client)

5/8/2020                     CSE 484 / CSE M 584 - Spring 2019             13
Malicious User Input

5/8/2020        CSE 484 / CSE M 584 - Spring 2019   14
SQL Injection Attack

                                                      SELECT passwd
                         Enter                         FROM USERS
                       Username                       WHERE uname
                           &                        IS ‘’; DROP TABLE
            Web        Password                         USERS; -- ’
                                    Web
           browser                                                           DB
                                   server
           (Client)

                                                            Eliminates all user
                                                                accounts

5/8/2020                      CSE 484 / CSE M 584 - Spring 2019                   15
Exploits of a Mom

                http://xkcd.com/327/

5/8/2020       CSE 484 / CSE M 584 - Spring 2019   16
SQL Injection: Basic Idea
                                                                Victim server

     Attacker     1

                                                                                2
                       3 receive data from DB                                   unintended
                                                                                query

• This is an input validation vulnerability
    • Unsanitized user input in SQL query to back-end
      database changes the meaning of query
• Special case of command injection
                                                                      Victim SQL DB
  5/8/2020                  CSE 484 / CSE M 584 - Spring 2019                           17
Authentication with Backend DB
set UserFound = execute(
    “SELECT * FROM UserTable WHERE
    username=‘ ” & form(“user”) & “ ′ AND
    password= ‘ ” & form(“pwd”) & “ ′ ” );

      User supplies username and password, this SQL query checks if
      user/password combination is in the database

If not UserFound.EOF
    Authentication correct                  Only true if the result of SQL
                                            query is not empty, i.e.,
  else Fail                                 user/pwd is in the database

5/8/2020                     CSE 484 / CSE M 584 - Spring 2019               18
Using SQL Injection to Log In
• User gives username OR    --
• Web server executes query
  set UserFound=execute(
    SELECT * FROM UserTable WHERE
      sername     OR 1=1 --
                  Always true!       Everything after -- is ignored!

• Now all records match the query, so the result
  is not empty co ec a hen ica ion !

5/8/2020                 CSE 484 / CSE M 584 - Spring 2019             19
Preventing SQL Injection
• Validate all inputs
      – Filter out any character that has special meaning
           • Apo ophe emicolon pe cen h phen nde co e
           • Use escape characters to prevent special characters form
             becoming part of the query code
               – E g e cape O Conno          O\ Conno
      – Check the data type (e.g., input must be an integer)

5/8/2020                     CSE 484 / CSE M 584 - Spring 2019          20
Prepared Statements
PreparedStatement ps =
  db.prepareStatement("SELECT pizza, toppings, quantity, order_day "
              + "FROM orders WHERE userid=? AND order_month=?");
ps.setInt(1, session.getCurrentUserId());
ps.setInt(2, Integer.parseInt(request.getParamenter("month")));
ResultSet res = ps.executeQuery();
                                                Bind variable (data
                                                   placeholder)

• Bind variables: placeholders guaranteed to be data (not code)
• Query is parsed without data parameters
• Bind variables are typed (int   ing
           http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

5/8/2020                       CSE 484 / CSE M 584 - Spring 2019               21
http://www.owasp.org

    OWASP Top 10 Web Vulnerabilities
1.     Injection
2.     Broken Authentication & Session Management
3.     Cross-Site Scripting
4.     Insecure Direct Object References
5.     Security Misconfiguration
6.     Sensitive Data Exposure
7.     Missing Function Level Access Control
8.     Cross-Site Request Forgery
9.     Using Known Vulnerable Components
10.    Unvalidated Redirects and Forwards

5/8/2020                CSE 484 / CSE M 584 - Fall 2016                  22
Cross-Site Request Forgery
                  (CSRF/XSRF)

5/8/2020           CSE 484 / CSE M 584 - Spring 2019   23
Cookie-Based Authentication Redux
           Browser                                       Server

5/8/2020             CSE 484 / CSE M 584 - Spring 2019            24
Browser Sandbox Redux
• Based on the same origin policy (SOP)
• Active content (scripts) can send anywhere!
      – For example, can submit a POST request
      – Some ports inaccessible -- e.g., SMTP (email)
• Can only read response from the same origin
      –    b    o can do a lo           i hj            ending

5/8/2020                 CSE 484 / CSE M 584 - Spring 2019       25
Cross-Site Request Forgery
• Users logs into bank.com, forgets to sign off
      – Session cookie remains in browser state
• User then visits a malicious website containing
   
     document.BillPayForm.submit(); 
• Browser sends cookie, payment request fulfilled!
• Lesson: cookie authentication is not sufficient
  when side effects can happen
5/8/2020                CSE 484 / CSE M 584 - Spring 2019   26
Cookies in Forged Requests

                                      Cookie: SessionID=523FA4cd2E

                                      User credentials automatically
                                      sent by browser
5/8/2020           CSE 484 / CSE M 584 - Spring 2019                   27
Impact
• Hijack any ongoing session (if no protection)
      – Netflix: change account settings, Gmail: steal
        contacts, Amazon: one-click purchase
• Rep og am he e home o e
• Login to the a acke account

5/8/2020                                                 29
XSRF True Story                            [Alex Stamos]

                                                                                CyberVillians.com

Internet Exploder                                             GET news.html

 www.cybervillians.com/news.html
                                                              HTML and JS
     Bernanke Really an Alien?

                     script
                                                         HTML Form POSTs         StockBroker.com

                                              Hidden iframes bmi ed fo m ha
                                              • Changed e email no ifica ion e ing
                                              • Linked a new checking account
             ticker.stockbroker.com           • Transferred out $5,000
   Java                                       • Unlinked the account
                                              • Restored email notifications

  5/8/2020                            CSE 484 / CSE M 584 - Spring 2019                             30
You can also read