Advertisement
Guest User

A3-Unlocker Run Multiple Instance of A3 v2

a guest
Sep 10th, 2014
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Security.AccessControl;
  8. using System.Security.Principal;
  9. namespace FileLockInfo
  10. {
  11.  
  12.     public class Win32API
  13.     {
  14.         [DllImport("ntdll.dll")]
  15.         public static extern int NtQueryObject(IntPtr ObjectHandle, int
  16.             ObjectInformationClass, IntPtr ObjectInformation, int ObjectInformationLength,
  17.             ref int returnLength);
  18.  
  19.         [DllImport("kernel32.dll", SetLastError = true)]
  20.         public static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax);
  21.  
  22.         [DllImport("ntdll.dll")]
  23.         public static extern uint NtQuerySystemInformation(int
  24.             SystemInformationClass, IntPtr SystemInformation, int SystemInformationLength,
  25.             ref int returnLength);
  26.  
  27.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  28.         public static extern IntPtr OpenMutex(UInt32 desiredAccess, bool inheritHandle, string name);
  29.  
  30.         [DllImport("kernel32.dll")]
  31.         public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
  32.  
  33.         [DllImport("kernel32.dll")]
  34.         public static extern int CloseHandle(IntPtr hObject);
  35.  
  36.         [DllImport("kernel32.dll", SetLastError = true)]
  37.         [return: MarshalAs(UnmanagedType.Bool)]
  38.         public static extern bool DuplicateHandle(IntPtr hSourceProcessHandle,
  39.            ushort hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle,
  40.            uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions);
  41.  
  42.         [DllImport("kernel32.dll")]
  43.         public static extern IntPtr GetCurrentProcess();
  44.  
  45.         public enum ObjectInformationClass : int
  46.         {
  47.             ObjectBasicInformation = 0,
  48.             ObjectNameInformation = 1,
  49.             ObjectTypeInformation = 2,
  50.             ObjectAllTypesInformation = 3,
  51.             ObjectHandleInformation = 4
  52.         }
  53.  
  54.         [Flags]
  55.         public enum ProcessAccessFlags : uint
  56.         {
  57.             All = 0x001F0FFF,
  58.             Terminate = 0x00000001,
  59.             CreateThread = 0x00000002,
  60.             VMOperation = 0x00000008,
  61.             VMRead = 0x00000010,
  62.             VMWrite = 0x00000020,
  63.             DupHandle = 0x00000040,
  64.             SetInformation = 0x00000200,
  65.             QueryInformation = 0x00000400,
  66.             Synchronize = 0x00100000
  67.         }
  68.  
  69.         [StructLayout(LayoutKind.Sequential)]
  70.         public struct OBJECT_BASIC_INFORMATION
  71.         { // Information Class 0
  72.             public int Attributes;
  73.             public int GrantedAccess;
  74.             public int HandleCount;
  75.             public int PointerCount;
  76.             public int PagedPoolUsage;
  77.             public int NonPagedPoolUsage;
  78.             public int Reserved1;
  79.             public int Reserved2;
  80.             public int Reserved3;
  81.             public int NameInformationLength;
  82.             public int TypeInformationLength;
  83.             public int SecurityDescriptorLength;
  84.             public System.Runtime.InteropServices.ComTypes.FILETIME CreateTime;
  85.         }
  86.  
  87.         [StructLayout(LayoutKind.Sequential)]
  88.         public struct OBJECT_TYPE_INFORMATION
  89.         { // Information Class 2
  90.             public UNICODE_STRING Name;
  91.             public int ObjectCount;
  92.             public int HandleCount;
  93.             public int Reserved1;
  94.             public int Reserved2;
  95.             public int Reserved3;
  96.             public int Reserved4;
  97.             public int PeakObjectCount;
  98.             public int PeakHandleCount;
  99.             public int Reserved5;
  100.             public int Reserved6;
  101.             public int Reserved7;
  102.             public int Reserved8;
  103.             public int InvalidAttributes;
  104.             public GENERIC_MAPPING GenericMapping;
  105.             public int ValidAccess;
  106.             public byte Unknown;
  107.             public byte MaintainHandleDatabase;
  108.             public int PoolType;
  109.             public int PagedPoolUsage;
  110.             public int NonPagedPoolUsage;
  111.         }
  112.  
  113.         [StructLayout(LayoutKind.Sequential)]
  114.         public struct OBJECT_NAME_INFORMATION
  115.         { // Information Class 1
  116.             public UNICODE_STRING Name;
  117.         }
  118.  
  119.         [StructLayout(LayoutKind.Sequential, Pack = 1)]
  120.         public struct UNICODE_STRING
  121.         {
  122.             public ushort Length;
  123.             public ushort MaximumLength;
  124.             public IntPtr Buffer;
  125.         }
  126.  
  127.         [StructLayout(LayoutKind.Sequential)]
  128.         public struct GENERIC_MAPPING
  129.         {
  130.             public int GenericRead;
  131.             public int GenericWrite;
  132.             public int GenericExecute;
  133.             public int GenericAll;
  134.         }
  135.  
  136.         [StructLayout(LayoutKind.Sequential, Pack = 1)]
  137.         public struct SYSTEM_HANDLE_INFORMATION
  138.         { // Information Class 16
  139.             public int ProcessID;
  140.             public byte ObjectTypeNumber;
  141.             public byte Flags; // 0x01 = PROTECT_FROM_CLOSE, 0x02 = INHERIT
  142.             public ushort Handle;
  143.             public int Object_Pointer;
  144.             public UInt32 GrantedAccess;
  145.         }
  146.  
  147.         public const int MAX_PATH = 260;
  148.         public const uint STATUS_INFO_LENGTH_MISMATCH = 0xC0000004;
  149.         public const int DUPLICATE_SAME_ACCESS = 0x2;
  150.         public const int DUPLICATE_CLOSE_SOURCE = 0x1;
  151.     }
  152.  
  153.     public class Win32Processes
  154.     {
  155.         const int CNST_SYSTEM_HANDLE_INFORMATION = 16;
  156.         const uint STATUS_INFO_LENGTH_MISMATCH = 0xc0000004;
  157.  
  158.         public static string getObjectTypeName(Win32API.SYSTEM_HANDLE_INFORMATION shHandle, Process process)
  159.         {
  160.             IntPtr m_ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id);
  161.             IntPtr ipHandle = IntPtr.Zero;
  162.             var objBasic = new Win32API.OBJECT_BASIC_INFORMATION();
  163.             IntPtr ipBasic = IntPtr.Zero;
  164.             var objObjectType = new Win32API.OBJECT_TYPE_INFORMATION();
  165.             IntPtr ipObjectType = IntPtr.Zero;
  166.             IntPtr ipObjectName = IntPtr.Zero;
  167.             string strObjectTypeName = "";
  168.             int nLength = 0;
  169.             int nReturn = 0;
  170.             IntPtr ipTemp = IntPtr.Zero;
  171.  
  172.             if (!Win32API.DuplicateHandle(m_ipProcessHwnd, shHandle.Handle,
  173.                                           Win32API.GetCurrentProcess(), out ipHandle,
  174.                                           0, false, Win32API.DUPLICATE_SAME_ACCESS))
  175.                 return null;
  176.  
  177.             ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic));
  178.             Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation,
  179.                                    ipBasic, Marshal.SizeOf(objBasic), ref nLength);
  180.             objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType());
  181.             Marshal.FreeHGlobal(ipBasic);
  182.  
  183.             ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength);
  184.             nLength = objBasic.TypeInformationLength;
  185.             while ((uint)(nReturn = Win32API.NtQueryObject(
  186.                 ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType,
  187.                   nLength, ref nLength)) ==
  188.                 Win32API.STATUS_INFO_LENGTH_MISMATCH)
  189.             {
  190.                 Marshal.FreeHGlobal(ipObjectType);
  191.                 ipObjectType = Marshal.AllocHGlobal(nLength);
  192.             }
  193.  
  194.             objObjectType = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType());
  195.             if (Is64Bits())
  196.             {
  197.                 ipTemp = new IntPtr(Convert.ToInt64(objObjectType.Name.Buffer.ToString(), 10) >> 32);
  198.             }
  199.             else
  200.             {
  201.                 ipTemp = objObjectType.Name.Buffer;
  202.             }
  203.  
  204.             strObjectTypeName = Marshal.PtrToStringUni(ipTemp, objObjectType.Name.Length >> 1);
  205.             Marshal.FreeHGlobal(ipObjectType);
  206.             return strObjectTypeName;
  207.         }
  208.  
  209.  
  210.         public static string getObjectName(Win32API.SYSTEM_HANDLE_INFORMATION shHandle, Process process)
  211.         {
  212.             IntPtr m_ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id);
  213.             IntPtr ipHandle = IntPtr.Zero;
  214.             var objBasic = new Win32API.OBJECT_BASIC_INFORMATION();
  215.             IntPtr ipBasic = IntPtr.Zero;
  216.             IntPtr ipObjectType = IntPtr.Zero;
  217.             var objObjectName = new Win32API.OBJECT_NAME_INFORMATION();
  218.             IntPtr ipObjectName = IntPtr.Zero;
  219.             string strObjectName = "";
  220.             int nLength = 0;
  221.             int nReturn = 0;
  222.             IntPtr ipTemp = IntPtr.Zero;
  223.  
  224.             if (!Win32API.DuplicateHandle(m_ipProcessHwnd, shHandle.Handle, Win32API.GetCurrentProcess(),
  225.                                           out ipHandle, 0, false, Win32API.DUPLICATE_SAME_ACCESS))
  226.                 return null;
  227.  
  228.             ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic));
  229.             Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation,
  230.                                    ipBasic, Marshal.SizeOf(objBasic), ref nLength);
  231.             objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType());
  232.             Marshal.FreeHGlobal(ipBasic);
  233.  
  234.  
  235.             nLength = objBasic.NameInformationLength;
  236.  
  237.             ipObjectName = Marshal.AllocHGlobal(nLength);
  238.             while ((uint)(nReturn = Win32API.NtQueryObject(
  239.                      ipHandle, (int)Win32API.ObjectInformationClass.ObjectNameInformation,
  240.                      ipObjectName, nLength, ref nLength))
  241.                    == Win32API.STATUS_INFO_LENGTH_MISMATCH)
  242.             {
  243.                 Marshal.FreeHGlobal(ipObjectName);
  244.                 ipObjectName = Marshal.AllocHGlobal(nLength);
  245.             }
  246.             objObjectName = (Win32API.OBJECT_NAME_INFORMATION)Marshal.PtrToStructure(ipObjectName, objObjectName.GetType());
  247.  
  248.             if (Is64Bits())
  249.             {
  250.                 ipTemp = new IntPtr(Convert.ToInt64(objObjectName.Name.Buffer.ToString(), 10) >> 32);
  251.             }
  252.             else
  253.             {
  254.                 ipTemp = objObjectName.Name.Buffer;
  255.             }
  256.  
  257.             if (ipTemp != IntPtr.Zero)
  258.             {
  259.  
  260.                 byte[] baTemp2 = new byte[nLength];
  261.                 try
  262.                 {
  263.                     Marshal.Copy(ipTemp, baTemp2, 0, nLength);
  264.  
  265.                     strObjectName = Marshal.PtrToStringUni(Is64Bits() ?
  266.                                                            new IntPtr(ipTemp.ToInt64()) :
  267.                                                            new IntPtr(ipTemp.ToInt32()));
  268.                     return strObjectName;
  269.                 }
  270.                 catch (AccessViolationException)
  271.                 {
  272.                     return null;
  273.                 }
  274.                 finally
  275.                 {
  276.                     Marshal.FreeHGlobal(ipObjectName);
  277.                     Win32API.CloseHandle(ipHandle);
  278.                 }
  279.             }
  280.             return null;
  281.         }
  282.  
  283.         public static List<Win32API.SYSTEM_HANDLE_INFORMATION>
  284.         GetHandles(Process process = null, string IN_strObjectTypeName = null, string IN_strObjectName = null)
  285.         {
  286.             uint nStatus;
  287.             int nHandleInfoSize = 0x10000;
  288.             IntPtr ipHandlePointer = Marshal.AllocHGlobal(nHandleInfoSize);
  289.             int nLength = 0;
  290.             IntPtr ipHandle = IntPtr.Zero;
  291.  
  292.             while ((nStatus = Win32API.NtQuerySystemInformation(CNST_SYSTEM_HANDLE_INFORMATION, ipHandlePointer,
  293.                                                                 nHandleInfoSize, ref nLength)) ==
  294.                     STATUS_INFO_LENGTH_MISMATCH)
  295.             {
  296.                 nHandleInfoSize = nLength;
  297.                 Marshal.FreeHGlobal(ipHandlePointer);
  298.                 ipHandlePointer = Marshal.AllocHGlobal(nLength);
  299.             }
  300.  
  301.             byte[] baTemp = new byte[nLength];
  302.             Marshal.Copy(ipHandlePointer, baTemp, 0, nLength);
  303.  
  304.             long lHandleCount = 0;
  305.             if (Is64Bits())
  306.             {
  307.                 lHandleCount = Marshal.ReadInt64(ipHandlePointer);
  308.                 ipHandle = new IntPtr(ipHandlePointer.ToInt64() + 8);
  309.             }
  310.             else
  311.             {
  312.                 lHandleCount = Marshal.ReadInt32(ipHandlePointer);
  313.                 ipHandle = new IntPtr(ipHandlePointer.ToInt32() + 4);
  314.             }
  315.  
  316.             Win32API.SYSTEM_HANDLE_INFORMATION shHandle;
  317.             List<Win32API.SYSTEM_HANDLE_INFORMATION> lstHandles = new List<Win32API.SYSTEM_HANDLE_INFORMATION>();
  318.  
  319.             for (long lIndex = 0; lIndex < lHandleCount; lIndex++)
  320.             {
  321.                 shHandle = new Win32API.SYSTEM_HANDLE_INFORMATION();
  322.                 if (Is64Bits())
  323.                 {
  324.                     shHandle = (Win32API.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
  325.                     ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle) + 8);
  326.                 }
  327.                 else
  328.                 {
  329.                     ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle));
  330.                     shHandle = (Win32API.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
  331.                 }
  332.  
  333.                 if (process != null)
  334.                 {
  335.                     if (shHandle.ProcessID != process.Id) continue;
  336.                 }
  337.  
  338.                 string strObjectTypeName = "";
  339.                 if (IN_strObjectTypeName != null){
  340.                      strObjectTypeName = getObjectTypeName(shHandle, Process.GetProcessById(shHandle.ProcessID));
  341.                     if (strObjectTypeName != IN_strObjectTypeName) continue;
  342.                 }
  343.  
  344.                 string strObjectName = "";
  345.                 if (IN_strObjectName != null){
  346.                      strObjectName = getObjectName(shHandle, Process.GetProcessById(shHandle.ProcessID));
  347.                     if (strObjectName != IN_strObjectName) continue;
  348.                 }
  349.  
  350.                 string strObjectTypeName2 = getObjectTypeName(shHandle, Process.GetProcessById(shHandle.ProcessID));
  351.                 string strObjectName2 = getObjectName(shHandle, Process.GetProcessById(shHandle.ProcessID));
  352.                 Console.WriteLine("{0}   {1}   {2}", shHandle.ProcessID, strObjectTypeName2, strObjectName2);
  353.  
  354.                 lstHandles.Add(shHandle);
  355.             }
  356.             return lstHandles;
  357.         }
  358.  
  359.         public static bool Is64Bits()
  360.         {
  361.             return Marshal.SizeOf(typeof(IntPtr)) == 8 ? true : false;
  362.         }
  363.     }
  364.  
  365.     class Program
  366.     {
  367.         static void Main(string[] args)    
  368.         {
  369.        
  370.         try
  371.         {
  372.                 WindowsIdentity  identity  = WindowsIdentity.GetCurrent();
  373.                 WindowsPrincipal principal = new WindowsPrincipal(identity);
  374.                 if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
  375.                 {
  376.                     Console.WriteLine("===========================================================");
  377.                     Console.WriteLine("                                                           ");
  378.                     Console.WriteLine("Please Run A3-Unlocker.exe as a \"Run As Administrator\" ");                
  379.                     Console.WriteLine("                                                           ");
  380.                     Console.WriteLine("===========================================================");
  381.                     Console.WriteLine("Press Enter to Exit");
  382.                 }
  383.             else {
  384.             String MutexName = "OnceMutexA3Client";
  385.             String ProcessName = "A3Client";
  386.             bool Violated = false;
  387.             Console.WriteLine("===========================================================");
  388.             Console.WriteLine("                                                           ");          
  389.             Console.WriteLine("Welcome To A3 Unlocker, Lets Kill Some Mutex");
  390.             Console.WriteLine("                                                           ");
  391.             Console.WriteLine("===========================================================");
  392.             Console.WriteLine("                                                           ");
  393.             try {
  394.             Process [] processes = Process.GetProcessesByName(ProcessName);
  395.                 if (processes.Length == 0) {
  396.                     Console.WriteLine("NO A3 Client Running");
  397.                     Console.WriteLine("                                                           ");
  398.                     Console.WriteLine(" Please Run A3 Client First!!!");
  399.                     Violated = false;
  400.                 }
  401.                  else  {
  402.                             Console.WriteLine("{0} A3 Instance", processes.Length);
  403.                             foreach (var process in processes ) {
  404.                                 try
  405.                                     {                
  406.                                         var handles = Win32Processes.GetHandles(process, "Mutant", "\\Sessions\\1\\BaseNamedObjects\\" + MutexName);
  407.                                         if (handles.Count == 0) throw new System.ArgumentException("NoMutex", "original");
  408.                                         foreach (var handle in handles) {
  409.                                             IntPtr ipHandle = IntPtr.Zero;
  410.                                             if (!Win32API.DuplicateHandle(Process.GetProcessById(handle.ProcessID).Handle, handle.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_CLOSE_SOURCE))
  411.                                             Console.WriteLine("DuplicateHandle() failed, error = {0}", Marshal.GetLastWin32Error());
  412.                                             Console.WriteLine("Mutex was killed");
  413.                                             Violated = true;
  414.                                             }
  415.                                     }
  416.            
  417.                                 catch (ArgumentException)
  418.                                     {
  419.                                         Console.WriteLine("The Mutex '{0}' was not found in the process '{1}'", MutexName, ProcessName);
  420.                                         Violated = true;
  421.                                     }            
  422.                             }
  423.                         }          
  424.                 }
  425.             catch (IndexOutOfRangeException)
  426.             {
  427.                 Console.WriteLine("NO A3 Client Running");
  428.                 Console.WriteLine("                                                           ");
  429.                 Console.WriteLine(" Please Run A3 Client First!!!");
  430.                 Violated = false;
  431.                                
  432.             }
  433.             if (Violated) {
  434.             Console.WriteLine("                                                           ");
  435.             Console.WriteLine("We are done!!! Enjoy");
  436.             Console.WriteLine("                                                           ");
  437.             Console.WriteLine("Must close this EXE before running another A3 Instance. Press Enter to exit");          
  438.             }
  439.         }
  440.         }
  441.          catch (Exception ex)
  442.         {
  443.             if (ex != null) {}
  444.         }
  445.             Console.ReadLine();
  446.         }
  447.     }
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement