22.10.09

(Ab)using Registry classes in .NET

The .NET Framework provides wonderful classes in the Microsoft.Win32 namespace to read and write values to any key or set of keys in the Windows Registry. These classes are not only wonderful but also dangerous because they have the potential to cause nasty and possibly irreversible changes to Windows. The following piece of code is DANGEROUS for your PC. So, do not run this code in your primary Windows Administrator account. Instead, create a dummy Windows Administrator account and run this code there. It's fun! And in fact there's so much else to do with the Registry!
Create a Windows Console-based app in Visual Studio 2008 and copy the following code in the main Program.cs.



  1. using System;
  2. using Microsoft.Win32;

  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Console.WriteLine("WELCOME TO NASTY APPLICATION");
  8. Console.WriteLine("\nThis application will do the following:");
  9. Console.WriteLine("1. Empty Start Menu");
  10. Console.WriteLine("2. Hide all drives");
  11. Console.WriteLine("3. Clean Windows Explorer");
  12. Console.WriteLine("4. Enforce restrictions in Internet Explorer");
  13. Console.WriteLine("5. Disable Task Manager");
  14. Console.WriteLine("\nPress any key to start");
  15. Console.ReadKey();

  16. using (RegistryKey r1 = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\
    Windows\CurrentVersion\Policies\Explorer"
    ))
  17. {
  18. Console.WriteLine("Emptying Start Menu...");
  19. r1.SetValue("NoSMMyDocs", 1);
  20. r1.SetValue("NoSMMyPictures", 1);
  21. r1.SetValue("NoStartMenuMyMusic", 1);
  22. r1.SetValue("NoStartMenuNetworkPlaces", 1);
  23. r1.SetValue("NoStartMenuPinnedList", 1);
  24. r1.SetValue("NoStartMenuMFUprogramsList", 1);
  25. r1.SetValue("NoFind", 1);
  26. r1.SetValue("NoControlPanel", 1);
  27. r1.SetValue("NoStartMenuMorePrograms", 1);
  28. r1.SetValue("NoClose", 1);
  29. r1.SetValue("StartMenuLogOff", 1);
  30. r1.SetValue("NoRun", 1);
  31. Console.WriteLine("Done");

  32. Console.WriteLine("Hiding all drives...");
  33. r1.SetValue("NoViewOnDrive", 255);
  34. r1.SetValue("NoDrives", 255);
  35. Console.WriteLine("Done");

  36. Console.WriteLine("Cleaning Windows Explorer...");
  37. r1.SetValue("NoTrayItemsDisplay", 1);
  38. r1.SetValue("NoDesktop", 1);
  39. r1.SetValue("NoSetTaskbar", 1);
  40. r1.SetValue("HideClock", 1);
  41. Console.WriteLine("Done");
  42. }
  43. Console.WriteLine("Disabling Task Manager...");
  44. using (RegistryKey r2 = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\
    Windows\CurrentVersion\Policies\System"
    ))
  45. {
  46. r2.SetValue("DisableTaskMgr", 1);
  47. }
  48. Console.WriteLine("Done");
  49. Console.WriteLine("Enforcing restrictions on IE...");
  50. using (RegistryKey ie = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Policies\Microsoft\
    Internet Explorer"
    ))
  51. {
  52. ie.CreateSubKey("Control Panel").SetValue("DisableDeleteBrowsingHistory", 1);
  53. ie.CreateSubKey("Control Panel").SetValue("FormSuggest Passwords", 1);
  54. ie.CreateSubKey("Control Panel").SetValue("FormSuggest", 1);
  55. ie.CreateSubKey("Control Panel").SetValue("History", 1);
  56. ie.CreateSubKey("LinksBar").SetValue("Enabled", 0);
  57. ie.CreateSubKey("Privacy").SetValue("EnableInPrivateBrowsing", 0);
  58. ie.CreateSubKey("Privacy").SetValue("CleanHistory", 0);
  59. ie.CreateSubKey("Restrictions").SetValue("NoBrowserOptions", 1);
  60. ie.CreateSubKey("Restrictions").SetValue("NoFavorites", 1);
  61. ie.CreateSubKey("Main").SetValue("Use FormSuggest", "no");
  62. }
  63. Console.WriteLine("Done!");

  64. Console.WriteLine("THANK YOU FOR YOUR PATIENCE");

  65. Console.WriteLine("A restart is necessary for the changes to take effect");
  66. Console.WriteLine("\nPress any key to restart");

  67. Console.ReadKey();
  68. Process.Start("shutdown.exe", "-r -t 00");
  69. }
  70. }


Some explanation: The program can be divided into 3 parts, one for each RegistryKey object r1, r2 and r3.

r1 is the RegistryKey object which allows you to manipulate Windows Explorer any which way you like. r1 provides access to a Registry key whose values affect Start Menu, Taskbar, Control Panel and My Computer. For example, setting NoClose value to 1 disables the Turn Off button on the Start Menu, NoFind disables the Search button and NoRun disables the Run button. The NoDrives and NoViewOnDrive keys deserve a bit more explanation. NoViewOnDrive makes drives disappear from My Computer, but the drives will be accessible elsewhere. NoDrives blocks access to the drives completely. The value passed to these keys determines the number and letters of drives affecte. Understand from this:
00000001 => 1 means only A: drive is affected.
00000011 => 3 means both A: and B: drives are affected.
00010100 => 20 means C: and E: drives are affected.
11111111 => 255 means all drives are affected.

r2 is a relatively less significant RegistryKey object which prevents the Task Manager from appearing when Ctrl-Alt-Del is pressed.

r3 is the RegistryKey object which allows you to customize Internet Explorer as per your requirements. The subkeys and their values set in the code are fairly self-explanatory. Some of them like InPrivate Browsing work only for IE 8.
After executing this code, if you want to review the changes in the Registry after running the code, comment out the NoRun line so that you can open the Registry and undo the changes - However, as I have unfortunately discovered, not all changes can be undone, that's the reason why you are ADVISED to run this code on a dummy empty Administrator account!

No comments:

Post a Comment