Talk on Common Web Vulnerabilities

I recently gave a talk on common web vulnerabilities with my colleague Scott Kirkland (see a link to his blog on the right) at the 2011 IT Security Symposium at UC Davis.  If you attended the session, hope you enjoyed it!  Otherwise we’ve posted the demo and the slide deck on Github.

We covered topics such as SSL, SQL Encryption, Cross Site Scripting (XSS), Cookie Policies, Html Encoding, Cross Site Request Forgery (CSRF) and Insecure Direct Object Reference.

The demo is also bundled with Scott’s talk on Data Validation and Html5.

You can find the demo and slides here.

Bing Maps jQuery UI Widget

Recently I was working on a project that required the usage of a maps service.  I needed a map that could add pins dynamically (one to many) and have the ability to do routing between two of the pins.  So the obvious choices were Google Maps and Bing Maps.  I opted to use Bing Maps because of the routing abilities for the specific area I needed it to route (a college campus).  Google maps has walking routing but for some reason it didn’t support walking paths on the campus.

My requirements were that it would be easily reusable, easy to setup on the page with one to many pins and that I could turn on and off features with a quick parameter change.  If you would like to see a demo please go here.  This plugin essentially wraps around a small portion of the Bing Maps API, but it’s the portion that was required for my needs.


The Html is simple div container to house the map and another div container to house the coordinates.  The coordinates are stored inside a dl.  It’s not exactly compliant with standards have invalid attributes, however it’s on the list to use the data attribute that is compatible with jQuery 1.4.3.  But for now this works.  The information for the pins (names/descriptions) are stored in dt and dd tags.

<div id="map" style="margin: 0px 0px 20px">
               <div class="" lat="38.53701730537018" lng="-121.7490667104721">
                    <dt >Mrak Hall</dt>
                    <dd>Home of AG Dean's Office</dd>
               <div class="" lat="38.539411128558385" lng="-121.74984186887741">
                    <dt >Shields Library</dt>
                    <dd>The Main Library of Davis</dd>
     </div> <!-- End of coordinate container -->
</div> <!-- End of map container –>


The script couldn’t be more simple.  Below is a basic call to initialize a map in a map container with an id of “map” and seta height of 400px.

     $("#map").bingmaps({height: "400px"});


There are several parameters that can be passed into the plugin:

Parameter Description
height height of the map box
width width of the map box
defaultLat default latitude location on load (float)
defaultLng default longitude location on load (float)
defaultZoom default zoom level (int)
defaultMapStyle default map style (VEMapStyle)
enableRouting Put into routing mode (boolean)
routeMode routing mode (walking, driving) (VERouteMode)
autoRoute whether or not to auto route between two points (boolean)
displayCurrentLocation draw crosshair on center and display current lat/lng coordinates (boolean)
displayLatitudeControl control to enter specific latitutde control (reference)
displayLongitudeControl control to enter specific longitude control (reference)
crosshairLocation path to the crosshair image
displayZoom display the current zoom level (boolean)
displayZoomControl control to enter specific zoom level (reference)
displaySearch display a box to search with (boolean)
locationTitle n/a
searchTitle n/a
coordinateTitle n/a
loadAllPins initially load all pins (boolean)
hideCoordinates hides coordinates (boolean)
usePushPins use push pins or a custom shape (boolean)
customShape html for custom shape, used only if usePushPins is false
allowShapeDragging allows shapes to be dragged (boolean)


You can download the plugin here.


Active Directory Permitted Logon Times with C# .Net 3.5 using System.DirectoryServices.AccountManagement

Recently I was working on a proof of concept for some active directory manipulation.  Using the System.DirectoryServices.AccountManagement was the easiest and best option for managing accounts (as the name implies).  However, the one thing that I really needed was a way to restrict when a particular user would be able to logon.

What I discovered was that the UserPrincipal class does in fact have a property called PermittedLogonTimes, but the catch is that there is no documentation on how to set the property for specific hours and to make this more confusing it is a byte[].

So after a little bit of playing around in Active Directory, setting the permitted logon times and reading it back out in C# it finally made sense.  The property is basically a byte[21] with a mask to cover the hours the user is allowed to logon.  Each day of the week is broken down into 3 byte values (refer to the table below for the index and the hours).

Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Midnight-8am 1 4 7 10 13 16 19
8am-4pm 2 5 8 11 14 17 20
4pm-Midnight 3 6 9 12 15 18 0

Each hour in each individual block has a specific mask value.  To get a specific time span, all you need to do is add up the values for the hours you want.  Refer to the following table:

Group 1 Group 2 Group 3
1 12am 8am 4pm
2 1am 9am 5pm
4 2am 10am 6pm
8 3am 11am 7pm
16 4am 12pm 8pm
32 5am 1pm 9pm
64 6am 2pm 10pm
128 7am 3pm 11pm

For example, if you would like to allow logon on Monday from 1pm-5pm your byte array would be all zeros except value at index 5 would be 224 and index 6 would be 1.

I’ve written the following classes to aid in calculating the byte masks:

LogonTime – is used to pass the information for logon for a specific day.

PermittedLogonTime – used to calculate the actual values that need to be passed to Active Directory.

Update 5/6/2011 : Thanks to comments and testing by Simone Greci, we came to the conclusion that the masks provided here are for Pacific Standard Time (PST, GMT -8).  I would like to update this code when I have time but keep in mind of this limitation.

Update 10/18/2011: I’ve finally gotten around to rewriting the code.  I rewrote everything from the ground up so it also takes into account the timezone.  By default it should go to your machine’s time zone, but can be passed specific timezones.  I’ve included code that converts the AD byte mask back into a list of the LogonTime objects.  To make it easier to use, I’ve compiled it into a project so you can just import the dll and use it.  The project is up on github (here).

There is a demo console app that compares the output to my old code that worked for PST (-8 GMT).

Usage is fairly straight forward:

To create a new logon time using your machine’s time zone:

var time = new LogonTime(DayOfWeek.Monday, 
new DateTime(2011, 1, 1, 8, 0, 0), 
new DateTime(2011, 1, 1, 10, 0, 0));

To use a specific time zone:

var zone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var time = new LogonTime(DayOfWeek.Monday, 
     new DateTime(2011, 1, 1, 8, 0, 0), 
     new DateTime(2011, 1, 1, 10, 0, 0), zone);

To get the byte mask to submit to AD:

var zone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var time = new LogonTime(DayOfWeek.Monday, 
     new DateTime(2011, 1, 1, 8, 0, 0), 
     new DateTime(2011, 1, 1, 10, 0, 0), zone); 
var times = new List<LogonTime>();
var mask = PermittedLogonTimes.GetByteMask(times)

To get the list of times from the AD mask:

byte[] mask;// populate from AD
var times = PermittedLogonTimes.GetLogonTimes(mask);

*The resulting times that are parsed have invalid dates, just the hour ranges.

I hope this helps those of you using those permitted logon times. Enjoy!

Updated: Enabling SSL/HTTPS on Team Foundation Server 2010 RTM

Not too long ago I posted a set of instructions on how to setup SSL/HTTPS for TFS 2010 RTM (here).  But it had a few short comings, ie. Report Server was not over SSL and TFS had no idea the Sharepoint was running over SSL either or even the FQDN I was using for the site.  Since then I’ve been hacking away at trying to fix those short comings and as far as I can tell everything seems to be functioning now.  Instead of completely editing the old post, I’ll just give a new straight forward set of instructions.


  1. Have the basic TFS install completed along with the configuration.
  2. FQDN you would like to use, for this example I will be using “”
  3. A certificate from a Microsoft Certificate Services Server (this cannot be a self-signed certificate because Visual Studio Team Explorer will not accept it)
    To request the certificate from Microsoft Certificate Services use the following steps:

  • Open up the IIS Manager and select the server
  • Select “Server Certificate”
  • In the Actions pane > select “Create Domain Certificate”
  • Follow the steps in the “Create Certificate” dialog.

Here is some basic information I’ll be using during this guide:

Server Name: tfs-server
Certificate Name: tfs-cert



Configure Sharepoint:
  • Open up SharePoint Administrator
  • Navigate to the “Operations” tab  
  • Select “Alternate Access Mappings”sharepoint1 
  • Click on “Edit Public URLs”sharepoint2
  • Select the “Default Web Site”sharepoint3 sharepoint4 sharepoint5
  • Change the “Default” from “http://tfs-server” to “”sharepoint6
  • Click “Save”
  • That should be it with Sharepoint Administrator.
  • One last thing is that we need to test the site.  When I did the initial configuration I was unable to navigate to the Sharepoint site from the server it self but was able to from client machines.  The site would prompt me for credentials over and over but would not let me into the site.  So just open a browser from the server and try to navigate to “”.  If that doesn’t work then you need to follow the next step, otherwise you are complete.
  • I came across this blog post that describes the very problem.  In the end I had to disable the loopback check, but different solutions may solve your problem.
Configure Reporting Services:
  • Open up “Reporting Services Configuration Manager”
  • Select “Web Service URL”
  • In the right panel, select “tfs-cert” as the SSL Certificate and 443 as the SSL Port.reporting1
  • Select Apply
  • Select “Report Manager URL”
  • In the right panel, select “Advanced”reporting2
  • In the “Advanced Multiple Web Site Configuration” window that pops up click “Add” under the “Multiple SSL Identities for Report Manager”reporting3
  • The “Add a Report Manager SSL Binding” window will pop-up, just select “tfs-cert” and it will automatically get the URL from the certificate.reporting4
  • Click “OK” until you get back to the main Reporting Services Configuration window.
  • That should be it for the Reporting Services Configuration.
Configure IIS:
  • Open IIS Manager, you should see something similar to the image below in your left panel.
  • Select “Default Web Site” and select “Bindings” in the Action Pane.iis2
  • Click “Add” in the “Site Bindings” pop-up.
  • Change the following values:
    Type: https
    Port: 443
    SSL Certificate: tfs-cert


  • Click “Ok” in the Add Site Binding and “Close” in “Site Bindings”
  • You will need to perform the same steps for the “Team Foundation Server” website except use port 8088 instead of 443.
  • That will be it for IIS Manager.
Configure Firewall:

You will need to ensure that your firewall is allowing 443 and 8088 in.

Configure TFS
  • Open up “Team Foundation Server Administration Console”
  • Navigate to the "Application Tier"
  • In the right pane, select "Change URLs".
  • In the "Change URLs" pop up, change the Notification URL to “"tfs
  • Click “Ok”, we are finished configuring the Application Tier.
  • Navigate to the “Sharepoint Web Applications”
  • In the right pane, select the “http://tfs-server” application and click “Change”tfs1
  • Change the values in the “Sharepoint Web Application Settings”tfs2
    Friendly Name:
    Web Application URL:
    Central Administration URL: http://tfs-server:17012
    Default Location: sites
  • Navigate to “Reporting” in the left pane.
  • In the right pane select “Edit”tfs3
  • The “Reporting” window will popup.  Select the “Reports” tab.
  • Select the “Populate URLs”, this will cause the drop downs in the tab to refresh with what the Report Server has configured.
  • Change the drop downs in the URL section to the https addresses that were created earlier.
    Web Service:
    Report Manager:
  • Once you click “Ok”, be sure to click “Start Jobs” in the Reporting pane.

Congratulations!  You have finished the configuration.  Enjoy!


Update (5/18/2010): Thanks to the comment by Jason, I have added an extra step to configure the Application Tier in the TFS Administration Console.

    Enabling SSL/HTTPS on Team Foundation Server 2010 RTM

    Update: I’ve been working on some of the issues from this guide and figured out a good portion of them.  Please take a look at my updated guide here.

    I’ve found lots of guides on the internet on setting up SSL/HTTPS on Team Foundation Server 2008, but not much on 2010 RTM.  It’s pretty much the same process but I had to figure a few things out differently because of IIS7 and just my personal preferences.

    First of all, if you are using Visual Studio 2008 you will need to make sure you have the following (doesn’t really have anything to do with enabling SSL/HTTPS, but you’ll need it to connect to TFS 2010 anyways):

    • Download and install the Visual Studio Team System 2008 Service Pack 1 Forward Compatibility Update for Team Foundation Server 2010 (Installer) found here.  Fun little name isn’t it?
    • Ensure that you’ve installed Visual Studio 2008 SP1 after installing TFS Team Explorer.
        You’ll first need two SSL certifications, one for SharePoint and one for TFS application layer.  It maybe possible to use one SSL certification but I like to have two so i can give each a different address.  Ex. and  First I tried using two self-signed certificates but that didn’t work because Team Explorer doesn’t like self-signed certificates, but the certificate for SharePoint can use a self-signed certificate.  We used Microsoft Certificate Services to provide the certificate necessary for the TFS application layer.
        To request the certificate from Microsoft Certificate Services use the following steps:
    • Open up the IIS Manager and select the server
    • Select “Server Certificate”
    • In the Actions pane > select “Create Domain Certificate”
    • Follow the steps in the “Create Certificate” dialog.
      Once you have your two certificates, follow the next configuration steps:
    Configure IIS
    • Open IIS Manager and select the server
    • Expand the “Sites”, you should see a list similar to the following
      Default Web Site = SharePoint Site
      Team Foundation Sever = TFS Application Layer
      SharePoint redirector = redirector for SharePoint (I’ll explain later)
    • Select the “Default Web Site”
    • In the Actions Pane select “Bindings…”
    • In the Site Bindings window, select “Add”, which will bring up the “Edit Site Binding” page.
    • Select the following:
      Type: https
      IP Address: all unassigned
      Port: 443
      SSL certificate: the certificate you created for SharePoint
    • Click “Ok”, then “Close” in the “Site Bindings” window.
    • Perform the same steps with the “Team Foundation Server” site, except use port 8088 for the port and the certificate created for the TFS application layer.
    • Close IIS Manager, we are done with it.
    Configure Firewall
    • Open “Windows Firewall with Advanced Security”
    • Select ‘”Inbound Rules”
    • Find “Team Foundation Server TFSWebSite:8080”, right click and choose disable.
    • In the right Actions pane, select “New Rule”
    • In the “New Inbound rule wizard” fill the following:
      Type: Port
      TCP/UDP: TCP
      Specific local port: 8088
      Allow the Connection
      Name: Name of your choice
    Configure SharePoint
    • Open up the SharePoint Administrator
    • Select the “Operations” tab, then under “Global Configuration” select “Alternate Access Mappings”
    • In the “Alternate Access Mappings” select “Edit Public URLs”
    • First select the “Alternate Access Mapping Collection” and choose the “Default Web Site” in the pop-up.
    • Fill in the default and Internet addresses.
      Default: http://servername (leave default alone)
    • Click “Save”

    At this point you should have SSL setup for both the TFS SharePoint sites and the TFS application layer.  In Team Explorer you should be able to add a server by entering “” as the address.

    The only thing left to do is make the SharePoint sites a bit easier to access.  Right now you would have to access the SharePoint site by going to, but wouldn’t it be nice to just type in the address bar and be redirected?  Once you complete these final steps you should be able to do just that.

    • Open up IIS manager.
    • Select your server and right click on “Sites” and choose “Add Web Site”
    • Fill in the following information:
      Site Name: “SharePoint Redirector”
      Physical Path: doesn’t matter i use c:\inetpub
      Binding: Leave as port 80 and defaults.
    • Click “Ok”
    • Select the redirector and in the main window double-click on “HTTP Redirect”
    • Check Redirect request to destination and fill in your target destination. i.e..
    • Click “Apply” in the Actions Pane

    Ok that’s all I got for this post, today.  Enjoy your newly secured TFS server.

    Update (4/26/2010):  Apparently there is a little funny business with accessing the report server from the outside network.  Instead of using the FQDN to get to the report server it uses the computer name.  So outside the local network it doesn’t work from the SharePoint site.  There are some steps to get it working, but I can’t quite get SharePoint over SSL to work properly on the server itself resulting in some problems with the TFS configuration tool.

    First setting up SharePoint SSL slightly differently than I’ve stated above similar to the instructions here. Then the following steps are the issues.

    Part of the problem is that I cannot access from the server itself, but I can access it from other computers.  I believe that because of this problem changing the configuration using TFS Administration Console is a problem.  Clicking on the Grant Access button


    Fill in the information for Url for Team Foundation Server : to https://mycompanysource:8088/tfs.  Then in the “SharePoint Web Application” select the https://.. site from the drop down.  When you click on ok I get an error message saying TFS cannot access the SharePoint site.


    Once this issue with the SharePoint SSL is resolved it should also resolve configuring Reporting Services for SSL, then modifying the configuration for TFS Administration Console for SSL reporting services.

    Configuring SSL for reporting services is pretty straight forward.

    Open up the Reporting Services Configuration Manager and go to the “Web Service URL”.  Configure the SSL Certificate and the SSL Port.


    So one problem is sort of holding up the whole issue with configuring the server to accept the reporting services over SSL.  Anyone have any ideas, I’m open to suggestions.

    Update (5/7/2010) : I’ve been working on some of the issues from this guide and figured out a good portion of them.  Please take a look at my updated guide here.

    Configuring Team Foundation Server Permissions for Small Groups

    Team Foundation Server (TFS) is a pretty good source control and process guidance tool, especially with the release of the latest version TFS 2010.  One benefit is that it allows extremely granular permissions which are great for large enterprise setups, but I work in a small group where everyone needs to have access to everyone’s files.  So it becomes sort of a pain because unless you add everyone into the administrators group you need to add permissions manually to everything.

    One thing to understand about TFS is that it is really 3 separate applications working together, TFS Application Layer, Microsoft SQL Server Reporting Services and Microsoft Sharepoint Services.  And since they don’t really talk with each other about permissions, these need to be granted individually to each application.

    I’ve come up with the following steps, even though they may not be best practices they work for my environment.  Before beginning I recommend setting up a security group that will contain all of your users that you wish to have access to the TFS server.

    TFS Application Layer Permissions
    • Open up the Team Foundation Server Administration Console
    • Expand Server Name > Application Tier > Team Project Collections
    • Select “Group Membership”
    • tfs-adminconsole1
    • In the “Global Groups on http://localhost:8080/tfs/defaultcollection” window click “New…” button.
    • In the “Create New Team Foundation Server Group” fill in “User” as “Group Name” and a Description if you choose.  And click “Ok”
    • tfs-adminconsole2
    • In the “Global Groups on http://localhost:8080/tfs/defaultcollection” window, select the newly created group and click the “Properties” button.
    • In the properties window add your users that you wish to have access to all projects.
    Sharepoint Permissions

    Sharepoint permissions need to be sort of granted individually for each project that you have.  But with the way I have it set up it should be slightly easier to manage.  All users are granted to the main site in Sharepoint.  Each project’s site is a sub site of the main site and can inherit the permissions from the main site.  The first part is granting permissions to the main site.

    • Log in to your Sharepoint site (most likely http://localhost/tfs/defaultcollection) as an administrator
    • Select “Site Action” > “Site Settings” > “Advanced Permissions”
    • In the permissions page you will want to add all of your users as contributors, this is where that security group will come in handy as well.

    The next step is for each project that you have going on, you need to set it to inherit permissions.

    • Once again log into your Sharepoint site.
    • Select the project you would like to change (url example http://localhost/tfs/defaultcollection/projectname)
    • Select “Site Action” > “Site Settings” > “Advanced Permissions”
    • In the permissions page select “Actions” > “Inherit Permissions”

    In the end permissions will be automatically updated if you edit the permissions on the main site or if you decided to setup the Active Directory group changing membership in the group.

    Report Server Permissions
    • Log in to your reporting services management site (usually http://localhost/reports)
    • Click on “Properties” > “New Role Assignment”
    • In the “New Role Assignment” screen, add your user (or security group) and select the “Browser” role.
    • reportingservices1

    Congratulations your permissions are now granted so everyone in your group can access all the Sharepoint sites and all the source control.

    Safely Displaying Html Formatted User Content

    Html escaping in ASP.NET MVC is a great way to prevent Cross-Site Scripting (XSS) attacks on your application.  The only problem I found was that sometimes an application needs to display formatted text and using the “Html.Encode(content)” will remove all formatting (as it is expected to).

    So I have written a little extension method that enables specific html formatting to be allowed while still escaping potentally dangerous html.

    public static class HtmlHelperExtensions
         private const string htmlTag = @&quot;&amp;lt;{0}&amp;gt;&quot;;
         public static string HtmlEncode(this HtmlHelper helper, string text)
              // encode the string
              string encodedText = HttpUtility.HtmlEncode(text);
              // put the text in a string builder
              StringBuilder formattedEncodedText = new StringBuilder(encodedText);
              // replace the escaped characters with the correct strings to allow formatting
              // &lt;p&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;p&quot;), @&quot;&lt;p&gt;&quot;);
              // &lt;/p&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;/p&quot;), @&quot;&lt;/p&gt;&quot;);
              // &lt;strong&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;strong&quot;), @&quot;&lt;strong&gt;&quot;);
              // &lt;/strong&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;/strong&quot;), @&quot;&lt;/strong&gt;&quot;);
              // &lt;em&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;em&quot;), @&quot;&lt;em&gt;&quot;);
              // &lt;/em&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;/em&quot;), @&quot;&lt;em&gt;&quot;);
              // &lt;span style=&quot;text-decoration:underline;&quot;&gt;
              string underline = @&quot;&amp;lt;span style=&amp;quot;text-decoration: underline;&amp;quot;&amp;gt;&quot;;
              string underlineReplacement = @&quot;&lt;span style=&quot;&quot;text-decoration:underline;&quot;&quot;&gt;&quot;;
              formattedEncodedText.Replace(underline, underlineReplacement);
              // &lt;/span&gt;
              // only find the spans that are related to the span for underlining
              var temp = formattedEncodedText.ToString();
              // for each instance of underline
              foreach (int i in temp.IndexOfAll(underlineReplacement))
                   // find the first instance of &lt;/span&gt; after the underline span and replace
                   var index = temp.IndexOf(string.Format(htmlTag, @&quot;/span&quot;), i);
                   // delete the string at that location
                   temp = temp.Remove(index, string.Format(htmlTag, @&quot;/span&quot;).Length);
                   // add in the new string at that location
                   temp = temp.Insert(index, @&quot;&lt;/span&gt;&quot;);
              formattedEncodedText = new StringBuilder(temp);
              // &lt;ul&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;ul&quot;), @&quot;&lt;ul&gt;&quot;);
              // &lt;/ul&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;/ul&quot;), @&quot;&lt;/ul&gt;&quot;);
              // &lt;ol&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;ol&quot;), @&quot;&lt;ol&gt;&quot;);
              // &lt;/ol&gt;
              formattedEncodedText.Replace(string.Format(htmlTag, @&quot;/ol&quot;), @&quot;&lt;/ol&gt;&quot;);
              // &lt;li&gt;
             formattedEncodedText.Replace(string.Format(htmlTag, @&quot;li&quot;), @&quot;&lt;li&gt;&quot;);
             // &lt;/li&gt;
             formattedEncodedText.Replace(string.Format(htmlTag, @&quot;/li&quot;), @&quot;&lt;/li&gt;&quot;);
             formattedEncodedText.Replace(@&quot;&amp;amp;nbsp;&quot;, @&quot;&amp;nbsp;&quot;);
             return formattedEncodedText.ToString();