Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Secure Web Applications

Building Secure Web Applications

Presentation for TechnoWise meetup in Pune on 28th April 2018

Shirish Padalkar

April 28, 2018
Tweet

More Decks by Shirish Padalkar

Other Decks in Programming

Transcript

  1. AGENDA ▫︎Why should I care? ▫︎Vocabulary ▫︎OWASP Top 10 ▫︎

    Break ▫︎Building secure web applications ▫︎How do I get started? 3
  2. MEDIA COVERAGE OF MAJOR HACKS ▫︎Office of Personnel Management data

    breach ▫︎Uber Covered Up Hack On 57 Million People ▫︎Airline outage causes massive delays for British Airways travelers ▫︎Equifax Hack: Who Is At Risk? ▫︎Target Identity Theft Case Three Times Bigger Than Reported 9
  3. Vulnerability refers to the inability of a system to withstand

    the effects of a hostile environment. 21
  4. In computer security: A vulnerability is a weakness which allows

    an attacker to reduce a system's information assurance. 22
  5. Exploit means to take advantage of something for one's own

    end, especially unethically or unjustifiably. 24
  6. An exploit is a piece of software that takes advantage

    of a bug or vulnerability in order to cause unintended behaviour to occur on computer software or hardware 25
  7. 26

  8. OWASP ▫︎Open Web Application Security Project ▫︎Not-for-profit charitable organisation ▫︎Focused

    on improving the security of software ▫︎All materials are available under a FOSS license ▫︎Currently has around 133 active projects 27
  9. OWASP TOP 10 ▫︎List of the 10 most critical web

    application security risks ▫︎A powerful awareness document ▫︎Reference document for project security analysis ▫︎Published at regular intervals ▫︎Approximately once in 3 years ▫︎Last published in 2017 29
  10. OWASP TOP 10, 2017 1. Injection 2. Broken authentication 3.

    Sensitive Data Exposure 4. XML External Entities (XXE) 5. Broken Access Control 6. Security Misconfiguration 7. Cross Site Scripting (XSS) 8. Insecure Deserialization 9. Using components with known vulnerability 10.Insufficient Logging & Monitoring 30
  11. DELTA FROM OWASP TOP 10 (2013) ▫︎New issues, supported by

    data: ▫︎A4:2017 - XML External Entities (XXE) ▫︎New issues, supported by the community: ▫︎A8:2017 - Insecure Deserialization ▫︎A10:2017 - Insufficient Logging and Monitoring ▫︎Merged or retired, but not forgotten: ▫︎A4 - Insecure Direct Object References and A7 - Missing Function Level Access Control merged into A5:2017 - Broken Access Control ▫︎A8 - Cross Site Request Forgery (CSRF) ▫︎A10 - Unvalidated Redirects and Forwards 31
  12. INJECTION ▫︎SQL Injection ▫︎Most prevalent ▫︎Databases like Oracle, MySQL ▫︎NoSQL

    Injection ▫︎Comparatively recent ▫︎Databases like MongoDB ▫︎Command Injection ▫︎LDAP Injection 34
  13. PROTECTION AGAINST SQL INJECTION ▫︎Use of Prepared Statements (with Parameterized

    Queries) ▫︎Use Stored Procedures if possible ▫︎White List input validation ▫︎Escape all user supplied input ▫︎ESAPI ▫︎https://www.owasp.org/index.php/ SQL_Injection_Prevention_Cheat_Sheet ▫︎https://www.owasp.org/index.php/ Category:OWASP_Enterprise_Security_API 38
  14. BROKEN AUTHENTICATION ▫︎Weak passwords / no defined password policy ▫︎Allows

    brute force or other automated attacks ▫︎Default, weak, or well-known passwords ▫︎"Password1" or “admin/admin” ▫︎Weak or ineffective credential recovery ▫︎Recovery questions like birthday, first car, … ▫︎Session IDs in the URL ▫︎PHPSESSID ▫︎JSESSIONID 40
  15. BROKEN AUTHENTICATION ▫︎Unencrypted passwords in storage or transit ▫︎Login over

    HTTP ▫︎Email password in plain text ▫︎Predictable session IDs ▫︎Reusing same session IDs 41
  16. TOOLS FOR BRUTE FORCE ATTACK ▫︎Aircrack-ng ▫︎Cain and Abel ▫︎Crack

    ▫︎DaveGrohl ▫︎Hashcat ▫︎John the Ripper ▫︎L0phtCrack ▫︎Ophcrack ▫︎RainbowCrack 42
  17. SENSITIVE DATA EXPOSURE ▫︎Allowing man-in-the-middle attacks ▫︎Use of weak encryption

    algorithms ▫︎Hashing vs Encryption ▫︎MD5 is NOT encryption ▫︎Not verifying certificates / signatures ▫︎Transparent db encryption is still dangerous ▫︎The most common flaw is simply not encrypting sensitive data 44
  18. BROKEN ACCESS CONTROL ▫︎Elevation of privilege. ▫︎Tampering with a JWT

    / SessionID or hidden field ▫︎Using actual name or key of an object when generating web pages ▫︎Don’t verify the user is authorised for the target object ▫︎Attackers can easily manipulate parameter values to access another object ▫︎http://photos.com/download.php?file=personal.jpg ▫︎http://mybank.com/accountInfo?accNumber=123456 46
  19. SECURITY MISCONFIGURATION ▫︎Running the application with debug enabled in production.

    ▫︎Directory listing enabled on the server ▫︎Running outdated versions of software / dependencies ▫︎Equifax was running older version of struts ▫︎Unnecessary services running on the machine ▫︎Not changing default keys and passwords 48
  20. SECURITY MISCONFIGURATION ▫︎Disabled latest security features ▫︎Real developers don’t use

    password! ▫︎Public AWS S3 buckets ▫︎Revealing error handling information to the attackers, such as stack traces. ▫︎Default accounts and passwords enabled and unchanged. 49
  21. CROSS SITE SCRIPTING (XSS) ▫︎Inject client-side script into pages viewed

    by other users ▫︎No HTML or Javascript escaping ▫︎Can steal cookies, change page location, etc. ▫︎Script executes with same permission as current page 52
  22. XSS TYPES ▫︎Reflected ▫︎Non-persistent ▫︎The most common type ▫︎Is typically

    delivered via email or a neutral web site ▫︎Display a page of results for a user, without properly sanitising the request. ▫︎Ex. Search result with search term without sanitisation 53
  23. XSS TYPES ▫︎Stored ▫︎Persistent ▫︎A more devastating variant ▫︎Permanently displayed

    on "normal" pages returned to other users ▫︎Example: Online message boards / Forums, Post on Facebook wall 54
  24. OWASP TOP 10, 2017 1. Injection 2. Broken authentication 3.

    Sensitive Data Exposure 4. XML External Entities (XXE) 5. Broken Access Control 6. Security Misconfiguration 7. Cross Site Scripting (XSS) 8. Insecure Deserialization 9. Using components with known vulnerability 10.Insufficient Logging & Monitoring 57
  25. WHAT WILL WE LEARN? ▫︎What’s in the Development Mode ▫︎Encryption

    in transit ▫︎Browser’s Origin-based Threat Model ▫︎Access Control ▫︎Input / Output ▫︎Business Logic Security 59
  26. DEVELOPMENT MODE ▫︎Dev endpoints can leak information ▫︎Express: x-powered-by ▫︎.NET

    IIS: x-aspnet-version ▫︎Spring Boot: /mappings, /configprops, … ▫︎Dev endpoints can be used for attacks ▫︎Spring Boot: /shutdown ▫︎Tomcat: 8005 shutdown port, manager webapp ▫︎Find out what your server does and disable it. 60
  27. TRANSPORT LAYER SECURITY (TLS) ▫︎Provides ▫︎Encryption in transit ▫︎Message integrity,

    ▫︎The “S” in https ▫︎SSL vs. TLS ▫︎Certificates are issued by Certificate Authorities (CAs) ▫︎Cipher suites & negotiation 61
  28. HTTP STRICT TRANSPORT SECURITY (HSTS) ▫︎Sent as HTTP header ▫︎Deploy

    with caution ▫︎ Once received, the browser will prevent any communications from being sent over HTTP to the specified domain and will instead send all communications over HTTPS ▫︎If you get this wrong, the impact can be catastrophic ▫︎HSTS not an option? Use HTTP 301 to forward to https 62
  29. BROWSER’S THREAT MODEL ▫︎Browser’s threat model is based on origin.

    ▫︎Browser represents the origin’s interests. ▫︎Same Origin Policy - If a request shouldn’t be read, the browser would not let you read it. ▫︎Cross Origin Resource Sharing - If a server won’t accept a request, the browser won’t send it. ▫︎A browser does not necessarily secure the user. ▫︎Browsers try to not be instruments of abuse to servers. 63
  30. SAME ORIGIN POLICY ▫︎Same-Origin Policy prevents scripts from reading content

    from a location that the script does not originate from 64 Scheme Domain Subdomain Port * depends checked checked ignored XHR depends checked checked checked Cookies depends checked depends ignored Web Sockets ignored ignored ignored ignored
  31. CROSS ORIGIN RESOURCE SHARING (CORS) ▫︎Go to https://xyz.com ▫︎Make a

    POST request to https://google.com ▫︎Browser does pre-flight check whether the server will accept the request via OPTION request. ▫︎If server accepts from current origin, browser will do the request, otherwise drops the request. 65
  32. SESSIONS ▫︎HTTP is stateless ▫︎Typically stored in cookies ▫︎Referenced by

    IDs ▫︎Sufficient length and entropy to prevent guessing and brute-force ▫︎Never include PII ▫︎Don’t accept user-initialised session ▫︎Re-authenticate on privilege level change ▫︎Expire sessions on client and server-side 66
  33. REST ▫︎Use the proper verbs ▫︎ Read: GET, Create: POST,

    Update: PATCH, Replace: PUT, Delete: DELETE ▫︎Restrict unused HTTP verbs ▫︎ Not Allowed: 405 Method Not Allowed ▫︎Validate content-type of submitted data as you accept ▫︎Don’t trust user-submitted MIME-types ▫︎Validate your JSON schema ▫︎Avoid insecure direct object references 68
  34. USER INPUT ▫︎Never execute anything you receive ▫︎Escape whatever you

    use, e.g. in SQL statements ▫︎eval() in JS is EVIL ▫︎Avoid XSS and other injection attacks ▫︎Be wary of encodings and charsets ▫︎Make sure you understand what your frameworks are doing! 69
  35. CROSS SITE SCRIPTING (XSS) ▫︎Never allow execution for third-party data

    ▫︎HTML entity-encode when not executed ▫︎Sanitise ▫︎Sandbox ▫︎Use the Content Security Policy 70
  36. CONTENT SECURITY POLICY (CSP) ▫︎Instruct the browser from which location

    and/or which type of resources are allowed to be loaded. ▫︎Define a loading behaviour in CSP "directive": ▫︎ default-src: All resources type (fallback) ▫︎ script-src: specifies valid sources for JavaScript ▫︎ object-src: specifies valid sources for <object>, <embed>, <applet> elements. ▫︎ style-src: specifies valid sources for sources for stylesheets ▫︎ img-src, media-src, child-src, frame-src, font-src, connect-src, form- action, sandbox, … ▫︎ https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP 71
  37. MISCELLANEOUS ▫︎Avoid MIME-type confusion ▫︎Validate content-type w.r.t. Accept header ▫︎Don’t

    reflect user-submitted MIME-types ▫︎X-Content-Type-Options: nosniff ▫︎Avoid clickjacking: X-Frame-Options: deny ▫︎Return the proper status codes ▫︎Do not leak information in response ▫︎Stack traces! 72
  38. API SECURITY ▫︎Avoid denial of service by thinking about how

    you handle inputs ▫︎Never block your endpoint ▫︎Offload expensive computation to workers or actors ▫︎Impose maximum input length on expensive operations ▫︎Impose maximum input length on expensive operations ▫︎Limit maximum password length to avoid DoS ▫︎Use caching, rate limiting where possible ▫︎Be a good citizen and don’t DoS others. 74
  39. DEPENDENCIES ▫︎Know the versions of all components you use ▫︎Know

    if software is vulnerable, unsupported, or out of date ▫︎Includes OS, runtime, web/application server, DBMS, applications, APIs and components, and libraries. ▫︎Subscribe to security advisories for your tech stack ▫︎Automate vulnerability scanning for your dependencies ▫︎Automate version updates: Branch, test, merge ▫︎Test compatibility of upgraded/patched libraries ▫︎Secure the component configuration 75
  40. WHY MANAGING SECRETS IS HARD? ▫︎Automated distribution of secrets ▫︎Automated

    revocation of access ▫︎Automated detection of secrets ▫︎Continuous delivery pipelines ▫︎Promoting secrets to infrastructure components 77
  41. 6 STAGES OF SECRET MANAGEMENT ▫︎Denial: No Solution ▫︎Reluctance: Emailing

    encrypted containers ▫︎Bargaining: Team Password Managers ▫︎Acceptance: Encrypted DVCs ▫︎Progress: Orchestration ▫︎Mastery: Secret Service 78
  42. AUTOMATED SECRET DETECTION ▫︎Don’t commit passwords and other sensitive information

    ▫︎Tools: ▫︎https://github.com/michenriksen/gitrob ▫︎https://github.com/thoughtworks/talisman ▫︎https://github.com/awslabs/git-secrets ▫︎https://github.com/Stono/hawkeye 79