Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking ASP.NET MVC SoftUni Team Technical Trainers Software University.

Slides:



Advertisements
Similar presentations
Nick Feamster CS 6262 Spring 2009
Advertisements

HI-TEC 2011 SQL Injection. Client’s Browser HTTP or HTTPS Web Server Apache or IIS HTML Forms CGI Scripts Database SQL Server or Oracle or MySQL ODBC.
©2009 Justin C. Klein Keane PHP Code Auditing Session 5 XSS & XSRF Justin C. Klein Keane
9/9/2005 Developing "Secure" Web Applications 1 Methods & Concepts for Developing “Secure” Web Applications Peter Y. Hammond, Developer Wasatch Front Regional.
It’s always better live. MSDN Events Security Best Practices Part 2 of 2 Reducing Vulnerabilities using Visual Studio 2008.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
It’s always better live. MSDN Events Securing Web Applications Part 1 of 2 Understanding Threats and Attacks.
CROSS SITE SCRIPTING..! (XSS). Overview What is XSS? Types of XSS Real world Example Impact of XSS How to protect against XSS?
Handling Security Threats in Kentico CMS Karol Jarkovsky Sr. Solution Architect Kentico Software
Understanding SharePoint 2013 Add-In Security Vulnerabilities
OWASP Zed Attack Proxy Project Lead
* ASP.NET Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking ASP.NET MVC Telerik Software Academy
Software Quality Assurance QA Engineering, Testing, Bug Tracking, Test Automation Software University Technical Trainers SoftUni Team.
Prevent Cross-Site Scripting (XSS) attack
+ Websites Vulnerabilities. + Content Expand of The Internet Use of the Internet Examples Importance of the Internet How to find Security Vulnerabilities.
CSCI 6962: Server-side Design and Programming Secure Web Programming.
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
November 13, 2008 Ohio Information Security Forum Attack Surface of Web Applications James Walden Northern Kentucky University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
Web Application Security ECE ECE Internetwork Security What is a Web Application? An application generally comprised of a collection of scripts.
Security Scanners Mark Shtern. Popular attack targets Web – Web platform – Web application Windows OS Mac OS Linux OS Smartphone.
Top Five Web Application Vulnerabilities Vebjørn Moen Selmersenteret/NoWires.org Norsk Kryptoseminar Trondheim
School of Computing and Information Systems CS 371 Web Application Programming Security Avoiding and Preventing Attacks.
Svetlin Nakov Technical Trainer Software University
October 3, 2008IMI Security Symposium Application Security through a Hacker’s Eyes James Walden Northern Kentucky University
Security Attacks CS 795. Buffer Overflow Problem Buffer overflows can be triggered by inputs that are designed to execute code, or alter the way the program.
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Building Secure Web Applications With ASP.Net MVC.
By Sean Rose and Erik Hazzard.  SQL Injection is a technique that exploits security weaknesses of the database layer of an application in order to gain.
Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking SoftUni Team Technical Trainers Software University
Trainers Team Ivan Yonkov Rated in the top 7% at Stack Overflow
ASP.NET MVC Architecture Layouts, Filters, Sections, Helpers, Partial Views, Areas… SoftUni Team Technical Trainers Software University
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Controls, Widgets, Grid…
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
AJAX in ASP.NET MVC AJAX, Partial Page Rendering, jQuery AJAX, MVC AJAX Helpers SoftUni Team Technical Trainers Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Web Security Lesson Summary ●Overview of Web and security vulnerabilities ●Cross Site Scripting ●Cross Site Request Forgery ●SQL Injection.
Security Attacks CS 795. Buffer Overflow Problem Buffer overflow Analysis of Buffer Overflow Attacks.
ASP.NET SignalR SoftUni Team Technical Trainers Software University
Working with Forms in PHP HTTP GET / POST, Validation, Escaping, Input Types, Submitting Arrays, URL Redirecting, PHP Superglobals Svetlin Nakov Technical.
Module: Software Engineering of Web Applications Chapter 3 (Cont.): user-input-validation testing of web applications 1.
CSRF Attacks Daniel Chen 11/18/15. What is CSRF?  Cross Site Request Forgery (Sea-Surf)  AKA XSRF/ One Click / Sidejacking / Session Riding  Exploits.
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
ASP.NET Identity System
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Do not try any of the techniques discussed in this presentation on a system you do not own. It is illegal and you will get caught.
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
SlideSet #20: Input Validation and Cross-site Scripting Attacks (XSS) SY306 Web and Databases for Cyber Operations.
Building Secure ColdFusion Applications
An Introduction to Web Application Security
State Management Cookies, Sessions SoftUni Team State Management
World Wide Web policy.
Cross-Site Forgery
JavaScript: ExpressJS Overview
Riding Someone Else’s Wave with CSRF
CSC 495/583 Topics of Software Security Intro to Web Security
WWW安全 國立暨南國際大學 資訊管理學系 陳彥錚.
Presentation transcript:

Web Security SQL Injection, XSS, CSRF, Parameter Tampering, DoS Attacks, Session Hijacking ASP.NET MVC SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents Web Security Main Concepts * Table of Contents Web Security Main Concepts Main Security Problems with Examples SQL Injection Cross Site Scripting (XSS) Cross-Site Request Forgery (CSRF) Parameter Tampering Other Threats (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Main Web Security Concepts Golden Security Rules and Guidelines

Feature or Bug? Is Software Security a Feature? Most people consider software security as a necessary feature of a product Is Security Vulnerability a Bug? If the software "failed" and allowed a hacker to see personal info, most users would consider that a software bug Advanced Software Testing Vol. 1

Reasons for Failures Software failures usually happen spontaneously Without intentional mischief Failures can be result of malicious attacks For the Challenge / Prestige Curiosity-driven Aiming to use resources Vandalizing Stealing Software Security Testing, Gary McGraw + Software Testing, Ron Patton

Golden Rules Maximum Simplicity Secure the Weakest Link More complicated – greater chance for mistakes Secure the Weakest Link Hackers attack where the weakest link is Limit the Publicly Available Resources Incorrect Until Proven Correct Consider each user input as incorrect The Principle of the "Weakest Privilege" Security in Errors (Remain stable) Provide Constant Defense (also use backups)

What is SQL Injection and How to Prevent It?

What is SQL Injection? Try the following queries: '  crashes '; INSERT INTO Messages(MessageText, MessageDate) VALUES ('Hacked!!!', '1.1.1980')  injects a message protected void ButtonSearch_Click(object sender, EventArgs e) { string searchString = this.TextBoxSearch.Text; string searchSql = "SELECT * FROM Messages WHERE MessageText LIKE '%" + searchString + "%'"; MessagesDbContext dbContext = new MessagesDbContext(); var matchingMessages = dbContext.Database.SqlQuery<Message>(searchSql).ToList(); this.ListViewMessages.DataSource = matchingMessages; this.DataBind(); }

How Does SQL Injection Work? The following SQL commands are executed: Usual search (no SQL injection): SQL-injected search (matches all records): SQL-injected INSERT command: SELECT * FROM Messages WHERE MessageText LIKE '%nakov%'" SELECT * FROM Messages WHERE MessageText LIKE '%%%%'" SELECT * FROM Messages WHERE MessageText LIKE '%' or 1=1 --%'" SELECT * FROM Messages WHERE MessageText LIKE '%'; INSERT INTO Messages(MessageText, MessageDate) VALUES ('Hacked!!!', '1.1.1980') --%'"

Another SQL Injection Example Original SQL Query: Setting username to John & password to ' OR '1'= '1 produces The result: If a user Admin exists – he is logged in without password string sqlQuery = "SELECT * FROM user WHERE name = '" + username + "' AND pass='" + password + "'" string sqlQuery = SELECT * FROM user WHERE name = 'Admin' AND pass='' OR '1'='1'

Preventing SQL Injection Ways to prevent the SQL injection: SQL-escape all data coming from the user: Not recommended: use as last resort only! Preferred approach: Use ORM (e.g. Entity Framework) Use parameterized queries string searchSql = @"SELECT * FROM Messages WHERE MessageText LIKE {0} ESCAPE '~'"; string searchString = "%" + TextBoxSearch.Text.Replace("~", "~~").Replace("%", "~%") + "%"; MessagesDbContext dbContext = new MessagesDbContext(); var matchingMessages = dbContext.Database.SqlQuery<Message>(searchSql, searchString);

SQL Injection and Prevention Live Demo

Cross Site Scripting (XSS) What is XSS and How to Prevent It?

XSS Attack Cross-site scripting (XSS) is a common security vulnerability in Web applications The web application displays a JavaScript code that is executed at the client's browser Crackers could take control over sessions, cookies, passwords, and other private data How to prevent XSS? Validate the user input (built-in in ASP.NET) Perform HTML escaping when displaying text data in a Web control

XSS Attack (2) Cross-site scripting attack Cookie theft Account hijacking Modify content Modify user settings Download malware Submit CRSF attack Password prompt Execute the script on visiting the page Submits script on an unsafe form

Automatic Request Validation ASP.NET applies automatic request validation Controlled by the ValidateRequest attribute of Page directive Checks all input data against a hard-coded list of potentially dangerous values The default is true Using it could harm the normal work on most applications E.g. a user posts JavaScript code in a forum Escaping is a better way to handle the problem 500 Internal Server Error: A potentially dangerous Request.Form value was detected from the client (…)

Disable Request Validation ASP.NET WebForms Disable the HTTP request validation for all pages in Web.config (in <system.web>): ASP.NET MVC Using the ValidateInput filter we can disable validation for an action or entire controller <httpRuntime requestValidationMode="2.0" /> <pages validateRequest="false" /> [ValidateInput(false)] public ActionResult XssMvc(string someInput) { … }

What is HTML Escaping? HTML escaping is the act of replacing special characters with their HTML entities Escaped characters are interpreted as character data instead of markup Typical characters to escape <, > – start / end of HTML tag & – start of character entity reference ', " – text in single / double quotes …

HTML Character Escaping Each character could be presented as HTML entity escaping sequence Numeric character references: 'λ' is λ, λ or λ Named HTML entities: 'λ' is λ '<' is < '>' is > '&' is & " (double quote) is "

How to Encode HTML Entities? HttpServerUtility.HtmlEncode HTML encodes a string and returns the encoded (html-safe) string Example (in ASPX): HTML Output: Web browser renders the following: <%: "The image tag: <img>" %> <%Response.Write(Server.HtmlEncode("The image tag: <img>"))%> The image tag: <img> The image tag: <img>

Preventing XSS in ASP.NET MVC The Razor template engine in ASP.NET MVC escapes everything by default: To render un-escaped HTML in MVC view use: @{ ViewBag.SomeText = "<script>alert('hi')</script>"; } @ViewBag.SomeText <script>alert('hi')</script> @{ ViewBag.SomeText = "<script>alert('hi')</script>"; } @Html.Raw(ViewBag.SomeText) <script>alert('hi')</script>

HTML Escaping in Web Forms and MVC Apps * * 4/25/201707/16/96 4/25/201707/16/96 HTML Escaping in Web Forms and MVC Apps Live Demo (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* 22##

Cross-Site Request Forgery What is CSRF and How to Prevent It?

What is CSRF? Cross-Site Request Forgery (CSRF / XSRF) is a web security attack over the HTTP protocol Allows executing unauthorized commands on behalf of some authenticated user E.g. to transfer some money in a bank system The user has valid permissions to execute the requested command The attacker uses these permissions to send a forged HTTP request without the user knowing Through a link / site / web form that the user is allured to open

CSRF Explained How does CSRF work? The user has a valid authentication cookie for the site http://victim.org (remembered in the browser) The attacker asks the user to visit some evil site, e.g. http://evilsite.com The evil site sends HTTP GET / POST to http://victim.org and does something evil Through a JavaScript AJAX request Using the browser's authentication cookie The http://victim.org performs the unauthorized command on behalf of the authenticated user

CSRF Cross-site request forgery attack Evil.com MySite.com User <form action=“mysite.com/ChangePassword”> MySite.com Authentication cookie Login Submit data on behalf of User User

Cross-Site Request Forgery Live Demo

Prevent CSRF in ASP.NET MVC To prevent CSRF attacks in MVC apps use anti-forgery tokens Put the anti-CSRF token in the HTML forms: Verify the anti-CSRF token in each controller action that should be protected: @using (@Html.BeginForm("Action", "Controller")) { … @Html.AntiForgeryToken() } [ValidateAntiForgeryToken] public ActionResult Action(…) { … }

Prevent CSRF in AJAX Requests In jQuery AJAX requests use code like this: Send the token in the AJAX requests: <%-- used for ajax in AddAntiForgeryToken() --%> <form id="__AjaxAntiForgeryForm" action="#" method="post"><%= Html.AntiForgeryToken()%></form> $.ajax({ type: "post", dataType: "html", url: …, data: AddAntiForgeryToken({ some-data }) });

Anti-CSRF in MVC Apps Live Demo

Prevent CSRF in Web Forms In Web Forms just add the following code in your Site.Master.cs: It changes the VIEWSTATE encryption key for all pages when there is a logged-in user In the VS 2013 / 2015 Web Forms app template, CSRF protection in Site.master.cs is already implemented protected override void OnInit(EventArgs e) { base.OnInit(e); if (Page.User.Identity.IsAuthenticated) { Page.ViewStateUserKey = Session.SessionID; }

What is Parameter Tampering and How to Prevent It?

What is Parameter Tampering? Malicious user alters the HTTP request parameters in unexpected way Altered query string (in GET requests) Altered request body (form fields in POST requests) Altered cookies (e.g. authentication cookie) Skipped data validation at the client-side Injected parameter in MVC apps

Parameter Tampering Live Demo

Other Threats Semantic URL attacks Man in the Middle (MiTM) URL Manipulation Man in the Middle (MiTM) Session Hijacking (easy if the session is part of the URL) Always use SSL when sending sensitive data! Insufficient Access Control Error messages can reveal information Denial of Service (DoS and DDoS) Brute force (use CAPTCHA!) Phishing Security flaws in other software you are using Social Engineering

ASP.NET Web Security https://softuni.bg/trainings/1230/asp-net-mvc-october-2015

SoftUni Diamond Partners

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "ASP.NET MVC" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.