Archive for September, 2008

Root Priviledge Escalation in Windows

September 29th, 2008

I have just uncovered a way to perform root priviledge escalation under Windows (tested using Server 2003 SP2)…so easy, with no addons or anything – all you need is a console.

  1. Open up a command prompt (cmd.exe)
  2. Type whoami. This should return your username – lowly peon user.
  3. In the command prompt, enter the following: at <current time + 1 min> /interactive “cmd.exe”
    The point of this step is to set up a scheduled task to execute in one minute of the current time. This scheduled task will launch a command prompt under the credentials of Local System.
    For example: at 11:05 /interactive “cmd.exe” will launch the cmd window at 11:05am.
  4. Type whoami into the new cmd window…..Voila!

Once escalated, you can use taskmgr to kill explorer and then re-run it from the new command prompt with the escalated priviledge.

Thread Safety and Locking

September 28th, 2008

I was recently reading a post about writing non-threadsafe code which talks about the main peril of multi-threading, and one way you can work around it.

I’ve long been a believer that doing anything multi-threaded is fraught with danger and you have to tread incredibly carefully when doing so. I say this with experience. What I learnt from reading that post wasn’t in the content, but in the comments, which talked about the Interlocked class for performing simple, thread-safe increments and decrements of operators.

So i decided to try it and see what the benefit really is, and i was surprised by by the results! I did my own profile against 3 scenarios:

  1. No thread safety (fast comparitively, though gave incorrect results)
  2. Locking using “lock” keyword (correct, but very slow by magnitude of nearly 10x)
  3. Locking using Interlocked class (correct, and fast – faster than no thread safety in some test runs)

Clearly these results aren’t scientific, but are quite good to give relative indicators of performance. I’ve reproduced the code below.


using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;

namespace ThreadingExample
{
	public interface IThreadTest
	{
		int Value { get; }
		void Debit();
		void Credit();
	}

	public class NonThreadSafe : IThreadTest
	{
		public int Value { get; private set; }

		public void Debit()
		{
			Value--;
		}

		public void Credit()
		{
			Value++;
		}
	}

	public class ThreadSafe : IThreadTest
	{
		public int Value { get; private set; }

		object lockSentinel = new object();

		public void Debit()
		{
			lock (lockSentinel)
			{
				Value--;
			}
		}

		public void Credit()
		{
			lock (lockSentinel)
			{
				Value++;
			}
		}
	}

	public class ThreadSafeUsingInterlocking : IThreadTest
	{
		private int value;
		public int Value
		{
			get { return value; }
			private set { this.value = value; }
		}

		public void Debit()
		{
			Interlocked.Decrement(ref value);
		}

		public void Credit()
		{
			Interlocked.Increment(ref value);
		}
	}

	[TestFixture]
	public class TestClass
	{
		[Test]
		public void TestNonThreadSafe()
		{
			NonThreadSafe nts = new NonThreadSafe();

			ExecuteThreadedTest(nts);

			Assert.AreEqual(0, nts.Value);
		}

		[Test]
		public void TestThreadSafe()
		{
			ThreadSafe ts = new ThreadSafe();

			ExecuteThreadedTest(ts);

			Assert.AreEqual(0, ts.Value);
		}

		[Test]
		public void TestThreadSafeUsingInterlocking()
		{
			ThreadSafeUsingInterlocking tsui = new ThreadSafeUsingInterlocking();

			ExecuteThreadedTest(tsui);

			Assert.AreEqual(0, tsui.Value);
		}

		private void ExecuteThreadedTest(IThreadTest threadTest)
		{
			int maxIterations = 99999999;
			DateTime start = DateTime.Now;
			Thread t1 = new Thread(() =>
			{
				for (int i = 0; i < maxIterations; i++)
				{
					threadTest.Credit();
				}
			}
			);
			t1.Name = "t1";

			Thread t2 = new Thread(() =>
			{
				for (int i = 0; i < maxIterations; i++)
				{
					threadTest.Debit();
				}
			}
			);
			t2.Name = "t2";

			t1.Start();
			t2.Start();

			t1.Join();
			t2.Join();

			DateTime finish = DateTime.Now;
			Debug.WriteLine(String.Format("Took {0}ms to complete", (finish - start).TotalMilliseconds));
		}
	}
}

Passing Interfaces Instead of Concrete Classes

September 25th, 2008

I’ve just read a blog post about why you should pass interfaces instead of concrete classes as arguments to your methods.

I normally try to think about the most appropriate usages of interfaces for my own classes, but what this post alerted me to was the necessity to use interfaces when working with framework classes.
IE: IDictionary instead of Dictionary

The reasons the author discusses i believe are quite valid….It’s something i’m going to more actively do when writing code…

Attached Files:

Using $exception to inspect thrown exceptions

September 19th, 2008

You can use the reserved keyword $exception in the object inspector to get details of any caught exception.

I’d forgotten about this one. Found it when reading about other tricks for tracking down exceptions

List of Technologies/Software I would like to try out from ground zero

September 16th, 2008

Reading an article about unit testing got me thinking about some of the tools i’d love to sink my teeth into.

I’m documenting them here in case i forget.

Backend
NHibernate (ORM)
SQLLite (DB)

Framework
Castle Windsor/Ninject (DI)
Lof4Net (Logging)
LINQ (Language Querying)
Tree Surgeon (Environment setup)

Testing
NUNit (Unit testing)
Rhino.Mocks/Moq (Mocking)
WatIn (UI testing)

Build Integration
Nant (Build tool)
CC.NET (CI server)

UI
WPF (GUI)
ASP.NET MVC /Monorail (Web engine)
PRISM (WPF App framework)
NHaml (MVC View Engine)

a lot of these i have or currently do use….some of them i have only ever played around with and a few i’ve never even touched.

At least me putting it down on paper (or bits, in this case) is a reminder of what i’m keen to try, and will get to it soon.

Recently Closed Tabs in Chrome

September 15th, 2008

A friend told me this one – I missed the feature of “recently closed tabs” in FireFox – it displays a small menu which lists all tabs closed in chronological order.

Well Chrome doesnt have exactly that, but CTRL-SHIFT-T will re-open the last closed tab. If you repeat the keystroke in sucession, it will re-open the one before that, ad nausem.

its perfect for the trigger happy like myself.

My Syndication URLs Have Changed!

September 11th, 2008

If you are reading this through an RSS reader, take note – I have changed the URLs for my feeds, and they are not accessible via this website, but through the following URL:

http://feeds.feedburner.com/TheTomesOfExperience

So just to re-iterate, please update the feed URLs to the new address:

http://feeds.feedburner.com/TheTomesOfExperience

MissingMethodException: ?

September 10th, 2008

Sometimes though when you’re changing interfaces across several projects, you can end up in a stink because a method reference is removed or a signature is changed, and you end up with the following error:

When i build in Visual Studio, in order to save time, I don’t always do a <ctrl><shift>-<b> (Rebuild Solution). instead, i do a <alt>-<b>,<u> in order to rebuild just the project, and its dependencies (faster build).

I’ve had this error come up a few times, and it’s easy to solve – in case i ever forget here it is:.

Server Error in ‘/’ Application.
——————————————————————————–

?
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: ?

When you get this problem, just re-build the entire project.

The Hollywood Principle

September 10th, 2008

The Hollywood principle is a software design methodology that takes its name from the cliche response given to amateurs auditioning in Hollywood: “Don’t call us, we’ll call you”. It is a useful paradigm that assists in the development of code with high cohesion and low coupling that is easier to debug, maintain and test.

http://en.wikipedia.org/wiki/Hollywood_Principle

Mass Reverse-DNS Lookup

September 4th, 2008

I needed to quickly browse through some web-server logs and pull out the hostnames which were accessing the web-server (to see where people were originating from).

The following single linux command will pull the IP address out of the apache log (for a particular date) and do the reverse DNS lookup for me:

cat access.log | grep ’1/Sep’ | awk ‘{print $1}’ | sort | uniq | xargs -n1 host | grep -v ‘not found’

just in case i ever need it again….

FireFox hit back at Chrome

September 4th, 2008

In a stunning display of dick-measuring, FireFox has countered Google’s Chrome browser by claiming New Firefox JavaScript engine is faster than Chrome’s V8

Give me a break. If this new JS engine in FF was so good, why has noone heard of it until AFTER Chrome was launched? Coincidence? I think not.

And more importantly, even if the new JS engine in FF has been around for some time, the fact is that Chrome beat them to the market. Stop talking shit and show me the money.

Chrome Saves The Day: Unsecured HTTP Content

September 3rd, 2008

This morning at work we had a conundrum – one of our client’s websites was displaying the infamous “This page contains unsecured content” message when you navigate to the secured (HTTPS) version of their website.

In order to work out which parts of the page were making references to unsecured (HTTP) content, I could either start by trial and error and target code I believe might be causing the problem. Possibly time-consuming and in the end no guarantee I will work it out.

Enter Google Chrome. This new little beauty has a built in debugging console, which allows me to inspect JS elements on the page, or view any errors which are thrown. and coincidentally, it perfectly listed all the URLs which were being referenced via the unsecured scheme. It’s unreal. Just saved me 10-20 mins of unnecessary work.

Google Chrome: My thoughts

September 3rd, 2008

Well first thing i have to state is that unlike FireFox, the initial download is actually a 500KB downloader which actually gets the full Chrome package off the net. Just how big is the full package? Well Chrome drops itself into the %Program FilesUserLocal SettingsApplication DataGoogleChrome directory, and the installer there is about 22MB, so not terribly big.

Its also nice to see that after installing, Chrome will import my bookmarks (not that I use any). You need to close FF in order for it to access the bookmark data file (presumably) but that’s really a negligible issue.

i’m somewhat surprised that Google have seemingly ignored the use of standard windows controls for the application. It doesn’t adhere to my Windows theme, there’s no menu system at all. It really is the minimalist app, but they must have gone to a lot of effort to make it look and work like that.

[UPDATE 1]
Chrome allows you to customise which search engine is your default and interestingly enough, it’s actually modified the list of available search engines based on my locality and the services provided. So I can choose Yahoo7, Sensis or ninemsn as my default search engine…

[UPDATE 2]
Scarily enough, Chrome has a “Passwords” section in its options dialog which allows you to see all usernames and passwords that it has kept track of during your browsing session…..Or as in my case, the passwords it has imported from FireFox. I never realised just how much data my browser was keeping for me…

[UPDATE 3]
The address bar text is color coded!! Simple idea, pretty effective, too. The domain portion of the URL is in full-black colour, and all other parts of the URL are in a lighter, grey colour in order to emphasise the fact that you’re still viewing a primary site, and not interested in the subdirectories below the top. And when you’re viewing a site which is encrypted with SSL, the “https” scheme is green in colour.

[UPDATE 4]
Boy it’s fast. very fast. And the popup-blocker is non-obtrusive!

[UPDATE 5]
Lol this is great. It even has a built-in task manager for micro managing any tabs which get out of control! For each tab, it clearly tabulates the memory usage, the CPU usage and live bandwidth that tab is consuming. At the bottom of the tab, is a link “Stats for nerds” which takes you to the URL “about:memory”, and gives you a complete breakdown of all memory usage (physical and virtual) for all internal threads.

[UPDATE 6]
This is bizzare….The DNS resolver built into Chrome seems to ignore any overrides I specify in my HOSTS file….I have changed the resolving IP for Site A in the hosts file, and despite many <CTRL>-F5 refreshes, Chrome is still adamant on using the old IP. There is an option called Use DNS pre-fetching to improve page load performance in the Options dialog. Only after turning this option OFF and restarting Chrome did it faithfully adhere to my HOST entry override. This might catch other people out there.

[UPDATE 7]
Most applications have external dialogs for configuration, or options, or downloaded files etc…I’ve noticed that Chrome does away with a lot of these. The Options dialog is probably the only desktop-level window apart from the main browser window itself. Everything else is represented as an HTML page in the browser itself. (sorry i correct myself – the task manager is another top-level dialog)

[UPDATE 8]
Very cool – I can drag-drop tabs from FF into Chrome. This is a BIIIIG boon considering i’m a tab-slut and have no less than 10-15 open at any one time.

[UPDATE 9]
Chrome attaches a little resize “grip” to the bottom left of any HTML <textarea> control. This allows you to resize the textarea beyond what the original designers intended – perfect for those designers who still have websites running in “1990′s” mode (ie: 800×600)

[UPDATE 10]
Cute. When you forcibly kill a process using Chome’s built-in task manager, the offending page changes to the following:
Chrome Error Page
…I guess the obviosu problem here is that there is no Reload button…..but i digress….. :)

[UPDATE 11]
Well this just bugs me. The address-bar search feature seems to take precendent over the fact that I haven’t entered a scheme in my URL, and therefore won’t resolve my website unless I specifically put “http”.
For example: If i create a host header entry called mywebsite.localhost and navigate to “mywebsite.localhost” in Chrome (nb: no HTTP://), it takes me to a Google search with that web-address as the search seed. IMO what it *SHOULD* have done was to try and resolve mywebsite.localhost FIRST, and IFF it couldn’t resolve, should it fall-back to the keyword search.
After you enter the scheme for the first time (and it resolves), Chrome learns that mywebsite.localhost is actually a site, and in future will resolve the website without requiring the scheme to be input.

This bugs me because i constantly enter URLs without entering a scheme. Moreover, as a web-developer i’m creating lots of host-header entries in order to run multiple websites too, so without entering HTTP the first time around for each URL, i’ll end up going to search when I didn’t actually need to. Very simple fix, guys – it would be nice if you could do it!

[UPDATE 12]
My enthusiasm for Chrome and what it represents has been recognised by my fan base in New Zealand. In an effort to try and achieve *some* work today, This will be my last update to this entry…
Application shortcuts are very cool. You can take any tab in Chrome, click the Options icon and select Create Application Shortcuts…This creates a launcher shortcut for Chrome to open in a very specific window, designed to make it looks and feel like a desktop-application.
Why does this excite me? Because I authored a project to do exactly that about 5 years ago, and it was incorporated and sold into a suite of products at the time. Unlike Chrome, my software was built using Internet Explorer’s rendering engine, but the “browser-less” concept was the same – to make a web-application feel like a desktop app…..And to the best of my knowledge, the project is still in use.

Attached Files:

Google Chrome is Live!

September 3rd, 2008

http://www.google.com/chrome

Downloading and installing it now…

In a way, i’m kind of excited. I hope this represents the start of a short, fast journey to push up the web…The one thing that I *hate* most about web-development is cross-browser incompatibility problems.

Reviews are here