title: How to Block USB to block Pen Drive and External Hard Drives
body: <p>I want to make a program to block USB so that no one can access USB port to transfer media.</p>
  
  <p>I don't want to block USB port from BIOS because I use USB Key-board and Mouse.</p>
  
  <p>Note: I want to give Administrator Privilege to all the systems but want to block just USB.  In my office people need admin privilege to access other registries and windows services because those are programmers. Few Antivirus like Norton have options of blocking USB whether system is running in Admin or User mode. In this every system Client Antivirus runs and Server Antivirus runs in Server. We do settings in Server and in all clients USB gets blocked. Now please dont say that have Norton Antivirus :) I want to know that there must be any way like these Fundu programers did in Norton Antivirus.</p>
  
  <p>Now what I did to achieve this so far.</p>
  
  <p>I created two windows service. One service is continuously changing USBSTOR's Start value to 4 by using timer, to make USB disable always. Also first service is checking continuously  whether Second service is running or not. If not then they are sending notification mail to me that some one has tried to stop the service.</p>
  
  <p>Second service is checking continuously whether First service is running or not. If not then they are sending notification mail to me.</p>
  
  <p>I made this so that People should not be able to stop service to unblock USB.</p>
  
  <p>Now the problem is that people can disable my service and the service will not work. At this stage the STATUS of Service is Running but actually its Disabled. So I can only check the STATE (Disabled/Enabled/Automatic) from the Registry Key (Start) of my Service which will be at the location</p>
  
  <pre><code>HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/MyServiceName
  </code></pre>
  
  <p>If Start's value is 2(Automatic), 3(Manual), 4(Disabled) we can check via our service every 2 seconds (for example) whether Start value got changed to 4, if yes then We can reset it to 2.</p>
  
  <p>But here problem is that from service I am able to access USBSTOR's values but not any Service's Registies (Because of security issue)</p>
  
  <p>I used this code to access USBSTOR registry with having write permission</p>
  
  <pre><code>Microsoft.Win32.RegistryKey key;
  string user = Environment.UserDomainName + "\\" + Environment.UserName;
  RegistrySecurity rs = new RegistrySecurity();
  rs.AddAccessRule(new RegistryAccessRule(user, RegistryRights.WriteKey, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow));
  key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\USBSTOR", true);
  key.SetAccessControl(rs);
  key.SetValue("Start", "4",RegistryValueKind.DWord);
  </code></pre>
  
  <p>This code is working fine. (I kept this code in timer function to make Start value = 4 every time, to block USB every time if any third party tool is trying to enable USB by registry fix)</p>
  
  <p>But the problem is that I want to get notification when ever some one is disabling Service. I can not access </p>
  
  <pre><code>HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/MyServiceName
  </code></pre>
  
  <p>registry through due to security created by Windows.</p>
  
  <p>But via Windows Application we can access</p>
  
  <pre><code>HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/services/MyServiceName
  </code></pre>
  
  <p>registry. So I created an application which will run every time windows get started and will check that some one has disabled my service which blocks USB.</p>
  
  <p>But here also problem is that user can go to </p>
  
  <pre><code>HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run
  </code></pre>
  
  <p>and delete my key to prevent my application to Run.</p>
  
  <p>Now my actual question after a long story is that</p>
  
  <ol>
  <li><p>How to block USB so that any one cant access it
  [Can we block it by blocking it on hardware level or by making some changes in its Driver. If yes then How?]</p></li>
  <li><p>Can we make a windows service which cant be Disabled
  [If Yes then How?. We can make it CanPauseAndContinue = False, CanShutDown = False, CanStop = False. But is there any way to avoid Disable it?]</p></li>
  <li><p>Can we prevent Access to Registries (Specific Keys)
  [ Antivirus software can do this, then how they do this? Does anyone have any idea or knowledge to such an extent? I want if some one is trying to edit USBSTOR value then it must say that Access is denied]</p></li>
  </ol>
  
  <p>Please Every Geeks, Programmers, Hackers are invited to answer this question.
  By answering this various newbie also get an idea to make security softwares. Please try to give solution to my problem.</p>
  
  <p>Thanks in Advance.</p>
  
  <ul>
  <li>Rahul</li>
  </ul>