Thursday, May 01, 2008

Ensuring only one instance of an application can be run and then breaking that approach

Whidbey_2005_0409_131710

A question posted on a DL was as follows

"I have an application that allows only one instance on the desktop, how do I force it to open multiple instance for some testing purposes".

Disclaimer: Note that this is kinda hacky and only relevant if you are a tester trying to break something. This shouldn't be treated as a way to achieve anything productive.

Since the user didn't explain which approach is used to ensure only one instance is allowed I'll try listing down the methods I know off and how I can break them.

Using named Mutex:
This is the more resilient approach taken by applications like Windows Media player. Here the application tries to create a named Mutex with sufficiently complicated name to avoid collision. E.g. namespace.app.guid and then try to acquire the mutex. In case it fails to acquire it then an instance is already running and it closes itself. If it acquired the mutex it means it is the first instance and it continues normally. An approach is outlined here.

Even though this seems to be the most robust solution this can be made to fail as well. If an user has sufficient permission he can do a CloseHandle on a Mutex handle created by some other application as well. For this you don't even need to write code. Do the following

  1. Download and install Process Explorer.
  2. Launch it (might need elevation on Vista)
  3. Have an application that uses this approach like my application named SingleInstanceApp.exe. Try launching a second instance and it will fail to open it.
  4. Click on the app finder toolbar button in Process Explorer and drag it to SingleInstanceApp window. This will get this application selected in process explorer.
  5. Select View->Lower Pane View ->Handles or simply Ctrl+H
  6. This will show all the handles created by SingleInstanceApp.
  7. Scroll down looking for the mutexes. At the end the screen looks something as follows Capture
  8. Right click on the mutex and choose Close Handle.
  9. Now try creating the second instance and it will open fine.

Enumerate the names of applications running and see if the app is already running:
This uses a combination of Process.GetCurrentProcess and Process.GetProcessByName(), see an implementation here.

This is easy to break. To do a DOS (Deinal of Service or DOS) just  create another application with the name that the app expects and per-launch it. To open two instances copy the application to another name and open that first and then the original application.

Enumerate Windows

This involves iterating through all the open windows in the system using EnumWindows and then seeing if a Window with a given name or classname is already present. Since the system doesn't guarantee uniqueness in either of the text or class name this can be broken in a similar approach to the first one. However, there are some complications to it as it is difficult to change window text from outside. However, a combination of code injection and SetWindowText win32 API should work.

1 comment:

Anonymous said...

Good afternoon www.blogger.com guys.

Are you looking to get your Local Store [b]Indexed on top of MSN[/b] search engine? Maybe you are looking to get more unique visitors? We have the perfect deal for you! We offer you an exclusive chance to experiance the knowledge of our [b][url=http://xrumer-blast.com/marketplace/xrumer-products/]Xrumer Experts[/url][/b]. [url=http://www.xrumer-blast.com][img]http://xrumer-blast.com/wp-includes/images/smilies/icon_smile.gif[/img][/url]

This is NOT trash! This is a human made entry specifically created to demonstrate the power of our automated software and its potential of getting to wide variety of audience in an effective and time saving manner.

We are a group of veteran [b]Xrumer[/b] and [b]Search Engine Optimization Experts[/b] that provide [b]Affordable, High Quality, [url=http://xrumer-blast.com/marketplace/seo-products/]Search Engine Optimization Service[/url][/b]. Post your message on thousands of forums, message boards, blogs and journals and gain organic traffic in a matter of days! With the help of our [b][url=http://xrumerblast.livejournal.com/]Search Engine Optimization Experts[/url][/b] your [b]Site URL[/b] can appear on [b]3384[/b] journals and forums. Receive almost instant increase of SERPS, Get thousands of quality backlinks directly to your Store without lifting a finger!

Our goal is simple; we want to make you and your website on the top of every search engine so you and your family can enjoy the comfort of steady, automatic income without leaving your home. Trough our [b][url=http://xrumer-blast.com/contact-xrumer-blast-com/]SEO Advice[/url][/b] you will get one of the best support seen in the industry. As part of our [b]Search Engine Optimization Services[/b], we take care of everything for you, from Optimizing your Website for search engines all through ranking your Website on the first page of your Yahoo.

For more information regarding [b]Search Engine Optimiztion[/b] and [b]Xrumer Blast Services[/b] visit [b][url=http://xrumerservice.livejournal.com/]Xrumer Services[/url][/b]. Our site includes everything you need to order your first [b]Xrumer Blast[/b], you can create the most appropriate message (Project) to suit your Bussiness, send us the information and after the payment clearance we will start working immediatly!

[url=http://xrumer-blast.com/marketplace/][img]http://xrumer-blast.com/wp-content/uploads/Xrumer-Blast-Logo.png[/img][/url]