Archive for January, 2010

Handy SQL Server knowledge nuggets

Sunday, January 24th, 2010

These are worth reading, kind of a read once – nothing too complicated. However they will be useful if you have the need for these

I suspect everyone who has dealt with scalar UDF functions in production environments are already VERY aware of the performance sucking capability they can have on your nice server. Here are some nice comments if you are not painfully aware:

Gareth

2010 – 2012 Technology Predictions

Saturday, January 23rd, 2010

I was celebrating 10 years after the dreaded Y2K down in Florida this year and was wondering if I would dare to write down my technology predictions. So feeling bold here they are, only time will tell if I’m anywhere close to accurate :-) :

  • Cloud
  • Authorization
  • Death of smart clients
  • No-SQL and ORM
  • BI becomes commodity
  • Netbooks – and the Google ‘netbook’
  • Deployment
  • SVN ==> GIT or Mercurial

Cloud

No surprises here, with Amazon running this model ‘forever’, Google not too far behind and now even Microsoft has started to chime in as well. The are alliances and definitions for SaaS, IaaS, PaaS. This concept is here to stay, and in fact will affect us in significant ways.

Authorization/Authentication

This is the big play that will help make the cloud be really viable, and even work with managing external companies that need to work for your company (think PCI compliance and support). This model will be based on the claims concept that has been knocking around for a while now, all it needed was something to push it into the limelight. Clouds have started to push authorization / authentication! In addition when this matures to a ‘tipping point’ I have no doubt customers will start to ‘outsource’ their claims trust (with contractual backup!) to other companies. For example if your company needs to provide PCI support to a customer, today the customer will have to be provided the peoples names and assign them logon accounts and manage their passwords. This becomes a major overhead and can be relatively easily managed through a claims based authorization system. Naturally this wont happen within two years – but it is coming!

Death of Smart Clients

Companies will realize that Smart Clients (aka Prism, Click-Once web forms) are only a stop gap. Browsers will rule the application space within 5 years, and will have made significant inroads before 2012. Conceptually they all make sense, but in today’s platform varied world they are dead – but it seems not everyone realizes it yet!

No-SQL / ORM

It seems that there are two factors pushing these forwards, but the root of it is that the historical relational database has so much overhead associated with infrastructure, management and DBA’s – people are looking for alternatives. It has become increasingly common for application programmers to fear the rules of relational databases they have to integrate with as they have evolved into their own discipline. This leads into ORM and which really try to ‘ignore’ the storage mechanism and provide plain business value. The No-SQL group are in a similar boat, they are interested in scaling and maintainance and know traditional relational models don’t meet their business needs.

BI becomes a Commodity

Microsoft has been playing the “BI is coming, BI is coming” song for a while now. However it seems with the SQL 2008 R2 release we are starting to get commodity solutions. This in intern will coerce the competitors to step up. The good news for competitors is that Microsoft has got more pricey, its no longer the ‘cheap kid on the block’. Open Source is starting to catch up with its commercial cousins. Either way its going to be an interesting couple of years for BI.

Netbooks – and the Google one

We have heard the term many times before, and even had a couple of false starts. The difference now is:

  1. Processing power has significantly cheapened – allowing more inexpensive offerings
  2. Web/Cloud infrastructure is in place. No worries about email or docs being stored on line.

So we have pretty good netbooks now, but I suspect Google will potentially set the standard for the netbooks. If done right (as opposed to the Nexus!) they have a killer combination. This is one I’m really interested to see what happens!

Deployment

Vista didnt get much traction and XP is on its last officially supported legs, as such I suspect that companies will be rolling out Windows 7 over the next 2-3 years. This will stress the deployment tools, and require individuals that know deployment to step in. In addition to this Windows Server 2008 R2 only comes in 64 bit – software houses realize the time has come to admit they are going to have to invest in supporting 64 bit solutions. This is going to be unusual for customers as they have forgotten how to roll out such changes as it has been a while since the last great deployment upgrade. There will be lots of opportunities in this area!

SVN ==> GIT / Mercurial

It seems that the CVS savior is in the process of being usurped by the DVCS products. Specially GIT and Mercurial are the strong runners. Its a shame because I had waited a while for SVN to mature to a point and now GIT & Mercurial are eating up projects. Oddly at the time of writing this even the Microsoft sponsored CodePlex site now supports Mercurial !

Finally the one I didn’t add to the list is software parallelism programming models. I suspect this will mature with all the new multi-core processors that are coming out. However I think this will mature after 2012 :-)

Gareth

Who knows of the .Net Secure Strings?

Thursday, January 21st, 2010

[Warning this is not new stuff - but shouldn't be overlooked if you need to secure sensitive data in your application]

Isn’t “Secure String” an oxymoron for .Net? So if we are thinking about securing some sensitive data in say C or C++ its relatively simple load it into a char array memory and encrypt it, wiping the memory out after the information has been loaded.

Now try that with .Net! From the Microsoft site:

A String is called immutable because its value cannot be modified once it has been created.

So how can you destroy one? Set it to empty? Well simply put you can’t :-) . Once your string is not longer referenced, or worse yet your object containing the string its time for the Garbage Collector to come and do its work. The problem is if your object has been around long enough to get into Generation 1 or 2 then it is going to take a bit longer.

Hmmm so in translation if you keep a password, Credit Card, encryption key or some other sensitive text in memory as a string you cant destroy it (think memset for us oldies!). Only the GC can free the memory for you, and you are dependent on HOW it frees that memory. I personally don’t know for a fact if it memsets it to blank, or just dereferences the pointer. However I would be willing to bet it is the option that requires the least amount of work and that doesn’t bode well for controlling the exposure of our sensitive data.

Plainly that proverbially sucks!

Enter the “SecureString” class, from the MS site it says:

“Represents text that should be kept confidential. The text is encrypted for privacy when being used, and deleted from computer memory when no longer needed.”

Wow doesn’t that just sound like the ticket we need! Secure, Encryption, delete from memory – how fantastic! Uh oh keep reading the remarks:

“Your application can render the instance immutable and prevent further modification by invoking the MakeReadOnly method.

Use appropriate members of the System.Runtime.InteropServices..::.Marshal class, such as the SecureStringToBSTR method, to manipulate the value of a SecureString object.”

BSTR – oh I feel the COM headache coming back!

Actually its really not that bad, but its definitely not a straight swap for a System.String. See some example code below:


using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security;

namespace CSharpHacker.Utilities
{
   /// <summary>
   /// Demo helper class for <see cref="SecureString"/>
   /// </summary>
   public class SensitiveDataHelper
   {
      private SecureString sensitiveData;

      /// <summary>
      /// Gets or sets the sensitive data class from helper
      /// </summary>
      /// <value>The sensitive data container.</value>
      public SecureString SensitiveData
      {
         get { return sensitiveData; }
         set { sensitiveData = value; }
      }

      /// <summary>
      /// UNSECURE: Converts the secured sensitive data into an unsecured string.
      /// </summary>
      /// <remarks>
      /// This is a dangerous function that converts secured information into
      /// unsecured data.
      /// </remarks>
      /// <returns>String representing the secured data</returns>
      public string SensitiveDataToString()
      {
         IntPtr ptr = Marshal.SecureStringToGlobalAllocUnicode(this.sensitiveData);
         try
         {
             // Unsecure managed string
             return Marshal.PtrToStringUni(ptr);
         }
         finally
         {
             Marshal.ZeroFreeGlobalAllocUnicode(ptr);
         }
      }

      /// <summary>
      /// UNSECURE: Base64s encodes a SHA512 has of the sensitive data
      /// </summary>
      /// <returns>SHA512 hash value</returns>
      public string Base64SensitiveDataHash()
      {
         IntPtr bstr = Marshal.SecureStringToBSTR(sensitiveData);
         try
         {
            // Pretend simple hash function that returns a string - this is fake just for readability!!
            string output = Marshal.PtrToStringBSTR(bstr);

            Marshal.FreeBSTR(bstr);

            SHA512 sha = new SHA512Managed();
            byte[] result = sha.ComputeHash(Encoding.UTF8.GetBytes(output));
            return Convert.ToBase64String(result);
         }
         finally
         {
            Marshal.ZeroFreeBSTR(bstr);
         }
      }

      /// <summary>
      /// Loads the sensitive data into a <see cref="SecureString"/>
      /// </summary>
      /// <param name="sensitiveInformation">The sensitive information to protect.</param>
      public void LoadSensitiveData(char[] sensitiveInformation)
      {
         try
         {
            using (SecureString securePassword = new SecureString())
            {
               foreach (char c in sensitiveInformation)
               {
                  securePassword.AppendChar(c);
               }
               securePassword.MakeReadOnly();
               this.sensitiveData = securePassword.Copy();
            }
         }
         finally
         {
            // discard the char array
            Array.Clear(sensitiveInformation, 0, sensitiveInformation.Length);
         }
      }

      /// <summary>
      /// Loads the sensitive data into a <see cref="SecureString"/>
      /// </summary>
      /// <param name="sensitiveInformation">The sensitive information to protect.</param>
      public void LoadSensitiveData(string sensitiveInformation)
      {
         char[] sensitive = new char[sensitiveInformation.Length];
         sensitiveInformation.CopyTo(0, sensitive, 0, sensitive.Length);

         LoadSensitiveData(sensitive);
      }

      /// <summary>
      /// Loads the sensitive data into a <see cref="SecureString"/>
      /// </summary>
      /// <param name="sensitiveInformation">The sensitive information to protect.</param>
      public void LoadSensitiveData(StringBuilder sensitiveInformation)
      {
         char[] sensitive = new char[sensitiveInformation.Length];
         sensitiveInformation.CopyTo(0, sensitive, 0, sensitive.Length);

         LoadSensitiveData(sensitive);
      }

      /// <summary>
      /// Creates the secure string from string.
      /// </summary>
      /// <param name="unprotectedSensitiveInformation">The unprotected sensitive information.</param>
      /// <returns></returns>
      public static SecureString CreateSecureStringFromString(string unprotectedSensitiveInformation)
      {
         char[] unprotectedSensitive = unprotectedSensitiveInformation.ToCharArray();
         SecureString secureInformation = new SecureString();
         try
         {
            foreach (char c in unprotectedSensitive)
            {
               secureInformation.AppendChar(c);
            }
            secureInformation.MakeReadOnly();
            return secureInformation;
         }
         finally
         {
            // discard the char array
            Array.Clear(unprotectedSensitive, 0, unprotectedSensitive.Length);
         }
      }
   }
}

A word of caution to the above code:

  • SensitiveDataToString – is considered insecure as it returns a string of the encrypted data. The same data we are trying to encrypt! However it is a commonly requested function, and so there is the implementation.
  • Base64SensitiveDataHash – is considered insecure as it currently uses a temporary string (:-( ), at this time I don’t have a way to converts a BSTR into a an array without going through a string first. One way would be to process it prior to being made ReadOnly, or alternatively someone can write a comment how to convert a C# BSTR into a byte array!

So even with all those disclaimers running a program that takes input will still a ‘leak information’ for the System.String. Specifically the tricky area is how do you get them into you program in the first place? Read them from a database, WinForm user input, or a web page? Kinda tricky :-) ! If you search the web there are implementations of  secure login controls that build it up character by character, but certainly something to think about.

So how Secure is “SecureString”? The answer is “it depends”, but reasonably secure and a heck of a lot better than System.String. A while ago there was a big storm about tools that can connect to the process and decrypt your SecureStrings. The best rebuttal to this I’ve seen can be read about in [SecureString Redux]. I definitely recommend reading this.

Now I have to say I’ve been meaning to write this for some time now! Hopefully this helped raise awareness of the string leakage risks in the .Net language and ways to help minimize the string information leak scenario.

Potential enhancements to the helper class would be:

  • Make the Hashing really secure, and allow a “HashAlgorithm” to be passed in
  • Allow external encryption
  • Allow secure serialization of data (via the encryption)

Gareth

Links to Software Security Code reviews

Friday, January 15th, 2010

Here is a link of helpful links if you want to double check your existing security code process (y’all do have them – right?):

Note as other are suggested, or I find others I’ll update the list.