C# code auditing by Ilja van Sprundel Principal Security Consultant at IOActive, Inc



Similar documents
Auditing WebObjects applications. Ilja van Sprundel

Creating Form Rendering ASP.NET Applications

Custom Error Pages in ASP.NET Prabhu Sunderaraman

Check list for web developers

Carlos Muñoz Application Security Engineer WhiteHat

We protect you applications! No, you don t. Digicomp Hacking Day 2013 May 16 th 2013

Webapps Vulnerability Report

Defense in Depth: Protecting Against Zero-Day Attacks

Adobe Systems Incorporated

Bypassing Memory Protections: The Future of Exploitation

3. Broken Account and Session Management. 4. Cross-Site Scripting (XSS) Flaws. Web browsers execute code sent from websites. Account Management

Bypassing Browser Memory Protections in Windows Vista

Java Web Application Security

Finding and Preventing Cross- Site Request Forgery. Tom Gallagher Security Test Lead, Microsoft

Threat Modeling/ Security Testing. Tarun Banga, Adobe 1. Agenda

Threat Modeling. Categorizing the nature and severity of system vulnerabilities. John B. Dickson, CISSP

Empirical study of performance of data binding in ASP.NET web applications

Web Application Security

Programming Flaws and How to Fix Them

Ivan Medvedev Principal Security Development Lead Microsoft Corporation

External Vulnerability Assessment. -Technical Summary- ABC ORGANIZATION

ASP.NET MVC Secure Coding 4-Day hands on Course. Course Syllabus

Crystal Reports.Net 1.1 Patch

Hack-proof Your Drupal App. Key Habits of Secure Drupal Coding

Blind Security Testing

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

Creating Stronger, Safer, Web Facing Code. JPL IT Security Mary Rivera June 17, 2011

CS 155 Final Exam. CS 155: Spring 2013 June 11, 2013

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

HACKING AUTHENTICATION CHECKS IN WEB APPLICATIONS ASHISH RAO & SIDDHARTH ANBALAHAN

Sichere Webanwendungen mit Java

2015, André Melancia (Andy.PT) 1

Emaml.NET ASP.NET Server Control for ArcIMS

Web Maniac Hacking Trust. Aditya K Sood [adi_ks [at] secniche.org] SecNiche Security

C# and Other Languages

Black Hat Briefings USA 2004 Cameron Hotchkies

Analytics Configuration Reference

Introduction to Web Application Security. Microsoft CSO Roundtable Houston, TX. September 13 th, 2006

How To Control Asp.Net Html And Html On A Pc Or Mac Or Mac (For Mac) On A Web Browser On A Mac Or Pc Or Pc (For Pc Or Ipad) On Pc Or Microsoft Mac Or Ipa (

How to Avoid an Attack - Security Testing as Part of Your Software Testing Process

Security Awareness For Website Administrators. State of Illinois Central Management Services Security and Compliance Solutions

No no-argument constructor. No default constructor found

NoSQL, But Even Less Security Bryan Sullivan, Senior Security Researcher, Adobe Secure Software Engineering Team

Introduction to ASP.NET Web Development Instructor: Frank Stepanski

Review and Exploit Neglected Attack Surface in ios 8. Tielei Wang, Hao Xu, Xiaobo Chen of TEAM PANGU

Lab 8: ASP.NET 2.0 Configuration API and Health Monitoring

The end. Carl Nettelblad

Secure Web Application Coding Team Introductory Meeting December 1, :00 2:00PM Bits & Pieces Room, Sansom West Room 306 Agenda

Cracking the Perimeter via Web Application Hacking. Zach Grace, CISSP, CEH January 17, Mega Conference

STATE OF WASHINGTON DEPARTMENT OF SOCIAL AND HEALTH SERVICES P.O. Box 45810, Olympia, Washington October 21, 2013

Enterprise Application Security Workshop Series

D. Best Practices D.1. Assurance The 5 th A

Is Drupal secure? A high-level perspective on web vulnerabilities, Drupal s solutions, and how to maintain site security

Evolution of the Major Programming Languages

Bypassing Internet Explorer s XSS Filter

Web Application Guidelines

Sandy. The Malicious Exploit Analysis. Static Analysis and Dynamic exploit analysis. Garage4Hackers

Out of the Fire - Adding Layers of Protection When Deploying Oracle EBS to the Internet

Project 2: Web Security Pitfalls

PREVENTING ZERO-DAY ATTACKS IN MOBILE DEVICES

ASP.NET Forms Authentication Best Practices for Software Developers

Penetration Testing Lessons Learned. Security Research

SECURITY B-SIDES: ATLANTA STRATEGIC PENETRATION TESTING. Presented by: Dave Kennedy Eric Smith

IOActive Security Advisory

The purpose of this report is to educate our prospective clients about capabilities of Hackers Locked.

Usable Crypto: Introducing minilock. Nadim Kobeissi HOPE X, NYC, 2014

External Network & Web Application Assessment. For The XXX Group LLC October 2012

Static Analysis Techniques for Testing Application Security. OWASP San Antonio January 31 st, 2008

Application security testing: Protecting your application and data

Auditing mailing scripts for web app pentesters

Windows Presentation Foundation: What, Why and When

Securing ios Applications. Dr. Bruce Sams, OPTIMAbit GmbH

Zing Vision. Answering your toughest production Java performance questions

Bug hunting. Vulnerability finding methods in Windows 32 environments compared. FX of Phenoelit

Intrusion detection for web applications

Penetration from application down to OS

5 Mistakes to Avoid on Your Drupal Website

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

METHODS OF QUICK EXPLOITATION OF BLIND SQL INJECTION DMITRY EVTEEV

REDCap General Security Overview

Static Analysis Techniques for Testing Application Security. OWASP Austin March 25 th, 2008

Web Application Security

WINDOWS AZURE EXECUTION MODELS

An Introduction to Application Security In ASP.NET Environments. Houston.NET User Group. February 23 rd, 2006

SQuAD: Application Security Testing

Using Nessus In Web Application Vulnerability Assessments

TO HACK AN ASP.NET WEBSITE?

Smart (and safe) Lighting:

Perl In Secure Web Development

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Client vs. Server Implementations of Mitigating XSS Security Threats on Web Applications

A deeper look at Inline functions

Application Security Testing. Erez Metula (CISSP), Founder Application Security Expert

Web application testing

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Monday, April 8, 13. Creating Successful Magento ERP Integrations

Application. Application Layer Security. Protocols. Some Essentials. Attacking the Application Layer. SQL Injection

Table of Contents. Introduction: 2. Settings: 6. Archive 9. Search Browse Schedule Archiving: 18

Bug Report. Date: March 19, 2011 Reporter: Chris Jarabek

Thomas Röthlisberger IT Security Analyst

Transcription:

C# code auditing by Ilja van Sprundel Principal Security Consultant at IOActive, Inc OWASP London July 2009 Using our past to secure your future London Chicago San Francisco Seattle www.ioactive.co.uk July 9, 2009 2009 Copyright IOActive, All 1 Rights

Who am I Ilja van Sprundel Principal Security Consultant at IOActive Europe Top Security Bug Finder on the Final Security Review for Microsoft Vista and Microsoft Windows 2008 Presented at BlackHat, EuSec, Ruxcon, CCC Congress, BlueHat, Cansec, Pacsec, IT Underground, HiTB, What the Hack Published security vulnerabilities on OpenBSD, FreeBSD, Linux, MacOSX, Windows, NetBSD, complete with remote code execution on networking stack Netric Blog: http://blogs.23.nu/ilja 2

intro Strong background in unmanaged languages Performed managed code review on behalf of Microsoft for Geneva Framework Will present what I learned about c# code auditing Some emphasis on webapp c# development With asp.net Most of this can apply to non-webapps written in c# aswell 3

agenda Entrypoints Xss Arbitrary redirects Sql Xml Unsafe/unmanaged/Pinvoke/Object Exception handling GC issues Integer types/rules 4

Entry points Webapps in c# are usually stored in 2 files File.aspx Contains the html code and controls that you can steer from the.cs file Can include code in there by doing <% code %> File.aspx.cs Code behind the html in.aspx file Contains functions that are entrypoints 5

Entry points functions Page_Init Where initialization is done This is before the page actually gets loaded Page_Load This is where you re supposed to do most of your work Executes when the requested page is loaded Page_PreRender Is called just before controls are rendered Should only use this to do some last-minute control changes 6

Entry points Standard functions visual studio declares and you can use You can register your own By doing something like: this.load += new System.EventHandler(this.my_handler); In Page_Init() for example 7

Entry points They look like: private void Page_Load(object sender, System.EventArgs e) private void Page_Init(object sender, EventArgs e) private void Page_PreRender(object sender, System.EventArgs e) 8

Entry points Variables Request.QueryString[ param ] fields in.aspx file: Aspx file: <asp:label id= MyLabel" runat="server"></ asp:label> Cs file: MyLabel.Text = QueryString[ blah ]; 9

xss Assume you know what xss is and what you can do with it asp.net has a default xss filter Isn t really input validation It s exploit mitigation Seems like a small difference But it s not 10

xss Exploit mitigation tries to make exploitation harder Input validation should validate all input for correct input. asp.net has no idea what kinda input your app wants, hence it can t do input validation properly All it does is try to detect html injection (without breaking functionality) and stop it if it detects html 11

xss Sometimes it does break functionality If you have a webapp that sends xml data back and forth between client and server Need to turn off xss filter for that <%@ Page validaterequest="false" %> 12

xss Fairly trivial to break it looks for < Followed by a-z, A-Z or space Find anything else to inject after the < but before the tagname That a browser will still accept Bypasses the input filter 13

xss Can have input validators bound to a single variable Regex s are quite difficult to get right Chances are the regex is too tolerant 14

xss HtmlEncode() HtmlAttributeEncode() much more lightweight than HtmlEncode(), only use this on html attributes!) UrlEncode() 15

xss Anti-XSS Same one s as asp.net offers XmlEncode() XmlAttributeEncode() Only use on xml attributes, should be more lightweight than XmlEncode() JavaScriptEncode() Only encodes strings! Does not work if you want to put some piece of userdata in a variable name VisualBasicScriptEncode() Only encodes strings! Does not work if you want to put some piece of userdata in a variable name 16

Arbitrary redirects Redirection looks like: Response.Redirect(Request.QueryString[ redir ]); Arbitrary redirect Could be used to spam search engines for example Need to do input validation so it only points to your webapp 17

Sql injection Sql injection usually looks something like sqlq="select * from bands where id=" + Request.QueryString["id"]; rs=conn.execute(sqlq); A hacky solution is to do input validation for this yourself. not really recommended, too easy to make mistakes 18

Sql injection A better solution is to use parameterized queries: sql = "select * from bands where id =@id"; cmd.parameters.add(new SQLParameter("@id", Request.QueryString["id"])); This is usually a lot safer It s still possible to screw things up sql = "select * from bands where id =@id and id2 = + Request.QueryString["id"]; cmd.parameters.add(new SQLParameter("@id", Request.QueryString["id"])); 19

xml XmlTextReader (get pwned) Resolves external entitities DTD, schema Can handle local file:// Has no length limits XmlReader (DoS) No length limits in place XmlDictionaryReader This one is sane. Should be used iso other 2 20

Unsafe/unmanaged code Marshal class is used to do unmanaged things in c# code. Such as manual memory allocation Suffers from same security issues as c/c++ 21

Unsafe/unmanaged code IntPtr hglobal = Marshal.AllocHGlobal(100); int size; Marshal.copy(inputfromnetwork, 0, hglobal, 100); size = Marshal.ReadInt32(hglobal); IntPtr data = Marshal.AllocHGlobal(size + 2); Marshal.copy(inputfromnetwork, 4, data, size); Classic integer overflow in memory allocation Leads to heap overflow 22

Unsafe/unmanaged code Blocks of code can be marked as unsafe Scoping then unsafe Basically allows you to include something that looks very much like c/c++ Allows for unmanaged memory allocations, pointers,. 23

Unsafe/unmanaged code int to = 0x41414141; byte what = 0x42; unsafe { byte *ptr = (byte *) to; *ptr++ = what; } Assigns 0x41414141 to ptr; Derefs pointer and writes 0x42 to it!.net does nothing for you when using unsafe It all looks like c/c++ again! 24

PInvoke PInvoke is used to call functions in dll s Usually code around these exported dll s will use unmanaged or unsafe code Only way to really use functions in dll s (assuming.net doesn t export the functionality you want) 25

Object Server.CreateObject() Is used to access com objects from within asp(.net) A lot of 3 rd party IIS com objects (email, file download/upload,.) Using it for these 3 rd party objects is usually not a great idea 3 rd party com objects are usually not really secure at all 26

Exception handling issues Almost everything in c# uses exceptions Exceptions make it way too easy to leak stuff It s all to common to see a buch of code wrapped under 1 single try, even those more than one api can throw a multitude of exceptions 27

Exception handling issues One of two things usually go wrong You miss an exception (and app dies for example) You don t but forget to clean something up in the exception handler 28

Exception handling issues... myclass b = new myclass; // need to call dispose on this one try { byte a[] = new byte[100];... } catch () { In case bailout(); of exception // myclass throw would some be leaked exception } myclass.dispose;... 29

GC issues C# s GC (garbage collector) kinda sucks at times It can be quite slow It can take minutes (or even hours!) before it cleans something up Just not acceptable in some cases Mysql example 30

GC issues: mysql example System.Data.OleDb.OleDbConnection con; con=new System.Data.OleDb.OleDbConnection(""); con.connectionstring="provider=mysqlprov;data Source=mysql;"; Try { con.open();... do stuff...; con.close(); } catch(exception ex) { bailout(); } Con.Close() not called in case exception is thrown 31

GC issues: mysql example GC will call con.close() when con is garbage collected Can take minutes (hours sometimes!) Meanwhile connection is still open! Now assume you have a very busy webapp Let s say 200 connections a minute That s 1000 connections in 5 minutes that still need to get GC ed! Totally screws up mysql s connection pool! 32

Integer types Long is always 64 bits long (unlike vc++ where they re always 32 bits long) there is an int64 type in c# No unsigned int64! 33

Integer rules Integer rules are slightly different from c/c++ Most are the same, however There is no int promotion when doing comparisons 34

Integer rules unsigned int maxlen = 256; BinaryReader binreader = new BinaryReader(File.Open(fileName, FileMode.Open)); int len = binreader.readint32(); if (len > maxlen) { bailout(); } byte a[] = new byte[len]; No int promotion to unsigned done! Signed comparison, bypasses maxlen check Does July a massive 9, 2009 allocation (since 2009 Copyright new _WILL_ IOActive, see All it Rights as unsigned) 35

Integer rules Int overflows can throw an exception Not on by default /checked compiler option Can also use checked() to make it throw an exception 36

Integer rules size = Marshal.ReadInt32(hglobal); try { IntPtr data = Marshal.AllocHGlobal(checked(size + 2)); } catch checked() (System.OverflowException will throw an exception on int overflow e) { Very useful bailout();! Should be used whenever int overflow might be possible! } 37

Questions? 38