cyphersec A blog about Web Application Security and .NET development best practices

23Jan/100

OWASP Code Crawler 2.5 Released

OWASP Code Crawler is a .NET Windows Forms application built using Microsoft .NET C#, XML, Linq and few third parties open source components. Its development started in fall 2007 as a very simple prototype from a mail conversation between me (Alessio Marziali) and Eoin Keary (Code Review Project Leader and Board Member). Eoin spotted the hidden power of this tool and asked me if I could be interested in making it open source. Thrilled by the idea of joining OWASP, few months later Code Crawler became an official OWASP Project.

Over the years Code Crawler has substantially grown, mainly with the help of other volunteers around the world, and today I am very pleased to announce we have reached version 2.5. I personally want to thanks Tripurai Rai, Sasikumar Ganesan and Paulo Coimbra for helping me make this happen. In this release we have been focusing mainly on the UI of the application and also improved our database while introducing utilities like STRIDE, DREAD Calculator and ASP.NET ViewState Decoder. For a detailed list of features you can refer to the changelog attached at the end of this post.

License

OWASP Code Crawler 2.5 is a Creative Commons Attribution Share Alike 3.0 open source application which means you are free to copy, distribute, transmit and remix this code as you like. In this case, you must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

Download

OWASP Code Crawler 2.5 can be downloaded from http://codecrawler.codeplex.com. Please be advised that in order to run Code Crawler requires Microsoft .NET Framework 3.5. You may download it from here (link to Microsoft download website).

Changelog

  • Code Crawler Editor
    • Find (CTRL+F)
    • Mark Findings
    • Select All (CTRL+A)
    • Copy as RTF (sweet)
    • CodeFolding
    • SyntaxHighlight
    • BracketMatching
    • Unlimited Undo/Redo buffer
    • Bookmarks
    • Go to line (CTRL+G)
    • Replace
    • Breakpoints
  • Single Scan Form
    • New User Interface
    • STRIDE Classification
    • Direct links to MSDN and Google
    • Shortcuts to Notepad and Calc
    • Threats Count
    • Printing
    • RTF Report
  • Visual Studio .NET (for VS 2005 - 2008)
    • Supports ONLY C# Project files (*.csjpro)
    • Bigger fonts
  • Mainform
    • New User Interface
    • Links to OWASP content
    • WASC Threat Classification 2.0
    • Sun Java Guidelines
    • Removed OWASP Browser
    • Removed Network Scan
    • Removed Reporting Frame
  • Database
    • 286 Keywords
    • Multi STRIDE Schema
    • Refactoring
  • Utilities
    • ASP.NET ViewState Decoder
    • DREAD Calculator
19Jan/103

OWASP Code Crawler 2.5 – Screenshots

It has been a while since I have posted something about Code Crawler, the project I am developing since fall 2007. Our development team, which is now composed by three developers, is in the process of making the magic happen again.

What follows is a list of screenshots of Code Crawler 2.5. Code Crawler is more or less a new project today. We have taken the good and removed the bad. So far we have completed  STRIDE automatic classification, DREAD, Improved performances, Enchanted our database in terms of quality and quantity and most of all we said good bye to our previous UI.

SingleFile

Single File Scan new User Interface.

CsProj

Visual Studio .NET (2005/2008) Integration is now fully working.

MultiFiles

Code Crawler can now scan hundreds of files at the same time without leaving nothing behind. In this example Code Crawler has finished scanning a very busy Visual Studio Solution and an external file using single file mode. The user can switch easily between the result at any time.

DreadCalculator

Dread Calculator is an easy to use tool integrated within Code Crawler which makes Risk Analysis easy.

OwaspResources

Code Crawler provides direct links to all OWASP major contents such as Guides and tools.

SingleFilesUtilities

The single source code file form provides easy accessible options such as:

  1. Archive (for reporting purposes and further investigations)
  2. Print Source Code
  3. Notepad
  4. Calc
  5. Google
  6. MSN
  7. Threats Count.

Download links, next week, hopefully.

1Nov/090

Code Crawler for Visual Studio .NET

CodeCrawlerVisualStudio

4Oct/090

Security Best Practices : ASP.NET Applications

Last Update: 2010/01/14

Securing web applications has always been a nightmare and probably it will always be. Working hacks for all major web development frameworks are into the wild and new vulnerabilities get discovered every day if not every hour.

Fortunately, .NET framework provides a good lists of defences that, if used and managed properly will allow you to create a defence wall between something valuable (your application) and the bad guys.

What follows is a list of all the .NET/ASP.NET security features, and best practices I’ve learned to be effective while coding web application during these years.

ASP.NET Application Security Guidelines

 

  1. Security should not taken down in the name of simplicity and/or UI appeal.
  2. Avoid, at all costs, client side validation (e.g. using Ajax or all JavaScript related validation libraries). JavaScript can and will, turned off and so your protections).
  3. Validate everything that comes in. From HTTP Headers, to User Inputs (HttpHeaders, Cookies, ViewState and so on). Even if you don’t use them, keep an eye on them, bad formatted http headers could crash your web server for instance.
  4. Assume that not only good guys will be using your applications.
  5. Security through Obscurity never makes sense.
  6. Validate user inputs in the application, promote the use of Regular Expressions (and be assured that they work the way they are meant to be)
  7. If you are using AJAX, shield all your Ajax calls. Ajax hacking is a new kind of hacking into applications, be sure they are secure.
  8. When using AJAX, be careful what you send back. Do not leak information. Do not return more information than is necessary to complete the request.
  9. Use the principle of Encapsulation. Don’t abuse the public keyword. If something is marked as public there should be a valid reason for it. Promote the use of the internal and/or protected internal instead.
  10. Pages with sensitive data should not be cached: page content is easily accessed using browser’s history.
  11. Use Declarative and Imperative Security and don’t trust your own code. If your method is supposed to just read a file, use PermitOnly along with FileIOPermission.
  12. Avoid using FullTrust, which means your application can do everything not only at application level by also at CLR level. Use Medium Trust or Low Trust depending on your application needs.
     

  13. Use mature, well security tested algorithms.
  14. Never compare passwords, compare the hash. Do not use MD5 which could be hacked using Rainbow tables, use RIPEMD160 instead.
  15. Don’t rely on ViewState as a valid and secure storage. ViewState is by default base64 encoded, in a matter of seconds any clever hacker could hack it and use it against you. Encrypt it at page or application level using ViewStateEncryptionMode.
  16. Make use of the HealthMonitoring system and trace your application behaviour. Use ViewStateFailureAuditEvent and make your application respond to such events.
  17. Encrypt your connection strings using aspnet_regiis. This tool it’s so easy to use and requires simple steps to both encrypt and decrypt connection’s strings.
  18. Promote the use of Gatekeepers, and never trust your application. If something have to deal with some other piece of code, it has to be authorised and authenticated first.
  19. Don’t use Blacklists, but use Whitelists instead, teach your application what to accept not what to avoid.
  20. Don’t try to sanitize a URI, if it doesn’t fit, reject it and let the user provide a valid one.
  21. While creating a Web Service, use WSE. If you are using WCF instead, before writing an application on it read the WCF Security Guidance.
  22. Don’t tell them anything. If your application throws an exception don’t provide technical details to the user. An hackers could read through the lines and craft a better working hack.
  23. While storing a password or any sensitive string, use the SecureString object. Which is encrypted for privacy when being used, and deleted from computer memory when no longer needed.
  24. Use platform features to manage keys where possible.
  25. Do not pass sensitive data from page to page.
  26. Do not cache sensitive data.
  27. Do NOT use GET for anything that changes the server state or contains sensitive information. GET requests are logged in the web server access logs. They are also shown in the browser history.
  28. DO use POST for every action that changes the server state and reject all non-POST methods. POST prevents unintentional actions, Most search engines won’t crawl POST forms and it also helps prevent duplicate submissions.
  29. If using Cookies, mark them as HTTPOnly using System.Net.Cookie. Set the httpOnlyCookies attribute on the authentication cookie. Internet Explorer Service Pack 1 supports this attribute, which prevents client-side script from accessing the cookie from the document.cookie property.
  30. Using slidingexpiration, is not always a good idea. A hacker could be possibly be logged in and won’t ever be kick out while trying to hack what’s next.
  31. Do not echo any user input straight away. Encode it first. Do it only if required. ANY information you give to a hacker CAN and WILL be used to hack your website.
  32. Learn how to use the Microsoft Anti-Cross Site Scripting Library to prevent XSS attacks.
  33. Protect Audit and Log Files. Log files might be boring stuff to look at. From an hacker point of view, they are a goldmine as they could possible revel valuable information.
  34. Don’t use Server.MapPath use Request.MapPath instead and mark the final parameter of to false. This means that a user cannot successfully supply a path that contains “..” to traverse outside of your application’s virtual directory hierarchy. Eg:

    Request.MapPath(INPUT, HttpContext.Current.Request.ApplicationPath, false);

  35. Add safeguards to your ASP.NET application to help protect against common canonicalization issues using System.IO.GetFullPath at application level at the beginning of every request (Global.asax)

    ScreenShot002

  36. Use WindowsAuthentication instead of any custom authentication. Using WindowsAuthentication ensure you the password will never be transmitted over the network.
  37. When constructing SQL queries, use type safe SQL parameters. AKA Use stored procedures or if you can not use parameterised queries in conjunction with Prepare statement. Using stored procedures it is always the best approach, from both technical and security point of views.
  38. Robots.txt files are the first place hackers look at. Use access controls to protect them.
  39. Secure your Web Service Definition Language WSDL. Your WSDL leaks the interface to your web service.
  40. ASP.NET provides a very rich security features for protecting your pages against CSRF attacks. Using ViewStateUserKey in association with Session.SessionID as discussed here in the OWASP Cross Site Forgery Prevention Cheat. This value will be now validated in the postback and if the value provided does not match the value in the viewstate an exception is thrown. Note: This requires ViewState to be enabled and therefore cannot be used in ASP.NET MVC applications.
  41. ASP.NET 1.1 and later include a ValidateRequest page directive that stops some malicious user input that could lead to XSS exploits. Since, ValidateRequest is enabled by default, all you have to do is ensure that you don’t explicitly disable it, either with page directives or configuration files. Note that ValidateRequest blocks any request that contain HTML or XML. If your page is intended to accept HTML or XML input from the user, you need to disable ValidateRequest, but be sure to follow the input validation discussed previously.
  42. If you must use innerHTML to create elements in the document, create only those elements that are not available through the DOM (param is one example) or that are not generated by user input. To generate HTML elements, use the createElement, appendChild and setAttribute methods for greater safety.
  43. If your site is constructed with frames, you can set the SECURITY attribute on untrusted FRAME and IFRAME elements to restricted. This set the security zone of the frame in the browser to the user’s restricted zone, which does not allow any script to run.

Web Server Security Guidelines

 

  1. Deny extended URLs. Excessively long URLs can be sent to Microsoft IIS servers, causing the server to fail to log the complete request. Unless specific applications require long URLs, set a limit of 2048 characters. Microsoft IIS will process request over 4096 bytes long, but will not place the contents of the request in log files. Modify %windir%\system32\inetsrv\urlscan\urlscan.ini and ensure “MaxQueryString'=2048” is present. This requires URLScan to be installed (read below)
  2. Use URLScan v2.5 or 3.1.
    URLScan v3.1 is a security tool that restricts the types of HTTP requests that Internet Information Services (IIS) will process. By blocking specific HTTP requests, URLScan helps prevent potentially harmful requests from being processed by web applications on the server. URLScan v3.1 has feature upgrades and fixes from its predecessor (v2.5) such as the ability to scan query strings, the ability to custom tailor rules that scan parts of your HTTP requests and many others. URLScan v3.1 will install as an ISAPI filter on IIS 5.1 and later, including the latest IIS 7.0 for Windows Server 2008
  3. Disable Directory listing.
  4. Your webserver should always be patched with the last updates.
  5. Scan your webserver using tools like Nikto. Nikto is an Open Source (GPL) web scanner which performs comprehensive tests against web servers for multiple items, including over 3500 potentially dangerous files/CGIs.
  6. Use SSLs were possible, this will encrypt and protect your data while on the wire. Using SSL doesn’t necessary means you are secure. It simply means your data is encrypted while on the go. If using SSL, restrict authentication tickets to HTTPS connections only.
  7. Ensure that the application pool identity is not granted sensitive privileges or unnecessary rights to access resources.
  8. Do not use highly privileged or administrative identities for IIS application pools
  9. Consider using a lower privilege identity.
  10. Separate code with different privilege requirements into different application pools
  11. When using anonymous authentication, configure the anonymous user to be the application pool identity.
  12. Use the principle of least privileged account. Create and associate your application with a low privileges user that fills all your applications needs.

SQL Server Security Guidelines

  1. Disable access to the xp_cmdshell functions within SQL Server using EXEC sp_dropextendedproc ‘xp_cmdshell’.
  2. Choose Windows Authentication when you can. It enforces strong passwords, password policies and other interesting stuff.
  3. Use a least privileges user. Create a SQL Server login for the account. Map the login to a database user in the required database. Place the database user in a database role. Grant the database role limited permissions to only those stored procedures or table your application really needs. By using a database role, you avoid granting permissions directly to the database user. This isolate you from potential damage to the database.
  4. If you are really paranoid, or asked to be, use the last line of defence. Database Cryptography.
4Oct/090

OWASP Code Crawler 2.5

During the last two months we have been hardworking on the new upcoming release of OWASP Code Crawler 2.5. In this release, we have been busy making Code Crawler even more stable and fast.

What follows it’s a list of all the new features of Code Crawler.

The source screen has been improved and now supports threat colouring. This means that all threats found in the source code will be highlighted depending on their threat level. (See the screenshot below)

As you can also see the threat analysis box has been moved into the Source Tab. Clicking on any highlighted threat will also show details of the corresponding threat.

The reports feature has also been improved, the code is less prone to throw exceptions and we are confident marking it as “releasable”.

The loading process of a single file is now 80% faster than any previous releases (1.0 to 2.4) this because we have moved the colouring tasks to another thread and have had changed the whole loading’s logic. Just to give you an idea of how much code crawler is fast, loading a source code file of one thousand lines in Code Crawler will take less than half a second (analysis included).

Sasikumar, new developer who has joined the team, is working on the new code crawler’s engine. He has made a powerpoint presentation, available on slideshare.com, which illustrates the details of the new architecture as well as the advantages gained using it. Sasikumar is also responsible for the new code colouring feature.


Code Crawler still supports the OWASP Code Review project providing the Key Pointers scan, but no major updates will be released in the future.

The Visual Studio Integration will also replace the Remote Server Scan in this release. The application is capable of scanning an entire Visual Solution starting from a single csproj file (Visual Studio C# Project file). Code Crawler commence the process analysing the provided project file and digging in for all the source code files which are part of the project. At this stage, the engine will process all the relevant source files and will end up providing all the details while using the new engine.

Code Crawler 2.5 was supposed to be released at the end of September but, because the amount of the new features, it has been postponed to end of October.

10Sep/090

Simplicity: Self-Documenting Code, Adapter Pattern

Simplicity is the eye of the beholder. It doesn’t matter much if you think the design is simple; if the rest of your team or future maintainers of your software find it too complicated, then it is.

To avoid this problem, use idioms and patterns that are common for your language and team, It’s OK to introduce new ideas, too, but run them past other team members first. Be sure to use names that clearly reflect the intent of your variables, methods, classes, and other entities.

Pair programming will help you create simple code. If you have trouble understanding something your partner wrote, discuss the situation and try to find a better way to express the concept. Before you use a comment to explain something, ask your partner how to make the code express its intent without needing a comment.

Adapter Pattern, Isolating third party components

A hidden source of duplication lies in calls to third party components. When you have these calls spread throughout your code, replacing or augmenting that component becomes difficult. If you discover that the component isn’t sufficient for your needs, you could be in trouble.

To prevent this problem, isolate your third party component behind an interface that you control. Ask yourself “When I need to upgrade or change this component, how hard will it be?”. In object oriented languages, consider using the “Adapter Pattern rather that instantiating third party classes directly. For frameworks that requires that you extend their classes, create your own base classes that extend the framework classes, rather than extending the classes directly.

Instead of supporting every feature of the component in your adapter, support only what you need today. Write the adapter’s interface to match your needs, not the interface of the component. This will make it easier to use and to replace when necessary.

Isolating third party components reduces duplication at the cost of making your code slightly more complex.

11Feb/090

WebService Security : Filter clients in a WebService scenario using an encrypted storage

    (Pseudo Code Mode ON)

     [WebServiceDescription(=Somethingwhatever)]
    public MySuperWebMethod
    {

        // (ASP.NET Tip) Use the Request object using Context
        if (ShiedObject.IsClientAllowedToCall(Request.UserHostAddress))
        {
            // Where isClientAllowedToCall is a method of the ShieldObjectClass.
            // IsClientAllowed implement a very basic streamreader function to read
            // the content of a specific .txt file located somewhere in a private storage.
            // This file(which could be encrypted) contains a whitelist
            // of trusted ips.
            // While looping using a "line by line pattern" if line match the current
            // client's ip then break and return true;
            // If not keep reading untill the end of the file and if there is not matches
            // return false;

            // If you reach this line you are allowed to call this "private" method

            return HelloWorld();

        }
        else
        {
            // Cheers..
            throw new YouAreNotAllowedToStayHereException();
        }
    }

7Feb/094

OWASP goes Open Source (for real)

header

Code Crawler is now on CodePlex. It has been classified with the following keywords : CodeReview, C#. At the moment I decided to mark the project as Setup which means that the content that will be illustrated is still not available.

From tomorrow on the development will be shifted to CodePlex. Every file related to CodeCrawler previously hosted on cyphersec.com has already been deleted (Sorry for that had no choice).

Results Page

Also a new beta release version will be linked from the main page which will include a couple of new features.

  • Increase stability
  • OWASP Code Guidelines support
  • Threat Analysis

threatanalysis 

Guidelines

Current Development Scenario:

In the next two weeks the Grid control used to list all the threats found in your file will be replaced by a new control (BUGID#931 Priority: Medium).

A new functionality will be also included which will make code crawler and advanced tool capable of crawling web applications affected by traversal path vulnerabilities (FEATUREID#940 Priority:Medium).

The Reporting UI will be redesigned from scratch. There is no reason to replace the reporting engine since it has reached a stable version. CC will keep supporting XSLT as standard for the reports area until new "polished" alternatives are  proposed and , from a development prospective , well-integrated.

XML Engine and threats discover to be (20% at least) faster. Cracking the system using .NET 3.5 enhancements ;)

Enhance Welcome Screen (BUGID#928 Priority:Medium); Code Crawler will be fully refined using a combination of black and gray colours. A mix of BlackBerry Style and Windows Vista Ultimate themes.

1Feb/090

OWASP CC 2.3 (two new screenshots)

Improved Code Navigation

header

Improved Scan Result View Page

full

New Objects within the solutions:

OWASP.CodeReview.CodeCrawler.Functionalities.Emails.ExceptionsHandling

  • NoReportAttached

OWASP.CodeReview.CodeCrawler.Engine.Exceptions

  • EngineExceptions (...)
  • UnableToLocateDatabaseException
  • UnableToLocateReportsDirectoryException

OWASP CC 2.3 (CodeName VoodooPeople) is planned to be released by the end of march.

5Oct/080

cc

image

In the new upcoming release of Code Crawler a new bunch of features where will released.
The Team Management section of the tool has started to work last night and I’m really confident to be able to include new features within next week.

In Code Crawler units of code are rappresented and individually marked as “functionalities”.
As you can see in the screenshot below, each functionality has its own folder and IO structure.

image

The “Emails” functionality relies on a xml data storage file which expose the place where informations like smpt server, smtp username, server port, and other personal user settings are stored.

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <CodeCrawlerFunctionality>
   3:     <EmailReports>
   4:         <ServerSettings>
   5:             <smtpServer>value</smtpServer>
   6:             <smtpUsername>value</smtpUsername>
   7:             <smtpPassword>value</smtpPassword>
   8:             <serverport>25</serverport>
   9:             <securessl>0</securessl>
  10:         </ServerSettings>
  11:         <EmailOptions>
  12:             <HighPriorityByDefault>0</HighPriorityByDefault>
  13:             <userEmail>value@value.com</userEmail>
  14:         </EmailOptions>
  15:     </EmailReports>
  16: </CodeCrawlerFunctionality>

CC will read this values and set the all the relevant variables just a bit before the email is sent, so you can change smtp server, and all other settings “on the fly”.

   1: /// <summary>
   2: /// Read the content of the /Functionalities/Emails/Settings/EmailSettings.xml file
   3: /// </summary>
   4: public void ReadUserProfile()
   5: {
   6:     XmlTextReader settingsReader = new XmlTextReader(EmailSettingsFilePath);
   7:     while (settingsReader.Read())
   8:     {
   9:         XmlNodeType nType = settingsReader.NodeType;
  10:         if (nType == XmlNodeType.Element)
  11:         {
  12:             switch (settingsReader.Name.ToLower())
  13:             {
  14:                 #region Read And Set Variables
  15:                 case "smtpserver":
  16:                     {
  17:                         smtpserver = settingsReader.ReadElementContentAsString();
  18:                         break;
  19:                     }
  20:  
  21:                 case "useremail":
  22:                     {
  23:                         useremailaddress = settingsReader.ReadElementContentAsString();
  24:                         break;
  25:                     }
  26:  
  27:                 case "smtpusername":
  28:                     {
  29:                         username = settingsReader.ReadElementContentAsString();
  30:                         break;
  31:                     }
  32:  
  33:                 case "smtppassword":
  34:                     {
  35:                         password = settingsReader.ReadElementContentAsString();
  36:                         break;
  37:                     }
  38:  
  39:                 case "securessl":
  40:                     {
  41:                         isSecureSSL = settingsReader.ReadElementContentAsBoolean();
  42:                         break;
  43:                     }
  44:  
  45:                 case "highprioritybydefault":
  46:                     {
  47:                         isMessageHighPriorityByDefault = settingsReader.ReadElementContentAsBoolean();
  48:                         break;
  49:                     }
  50:  
  51:                 case "serverport":
  52:                     {
  53:                         serverport = settingsReader.ReadElementContentAsInt();
  54:                         break;
  55:                     }
  56:                 #endregion
  57:             }
  58:         }
  59:     }
  60: }

As you can see there’s still some work on the UI but the window is pretty clear.

image 

If the specified server is working and all the information supplied are correct CC will send a pre-formatted and stiled email with the report attached. CC supports all smtp servers with or without SSL support. Each Email teamplate is based on a xslt template, company logos or rebranded teamplates can be used at any time.

image

The next functionality is Review Manager.

image 

Review Manager (also knows as Calendar in the development), share the Outgoing smtp server settings with the “Emails” functionality, I need feedbacks on this please email or even comment here what settings would you like to send or plan.

Thanks!

Call for Help:

Are you a smart skilled C# developer with serious XML skills which would like to help making CC a better and usefull tool? Please drop a line!