1+ namespace COFlashFixer
2+ {
3+ #region References
4+
5+ using System ;
6+ using System . Runtime . InteropServices ;
7+ using System . Text ;
8+
9+ #endregion
10+ public static class Injector
11+ {
12+ #region Win32
13+ [ DllImport ( "KERNEL32.DLL" ) ]
14+ private static extern IntPtr OpenProcess ( uint dwDesiredAccess , bool bInheritHandle , uint dwProcessId ) ;
15+
16+ [ DllImport ( "KERNEL32.DLL" ) ]
17+ private static extern IntPtr VirtualAllocEx ( IntPtr hProcess , IntPtr lpAdress , UIntPtr dwSize , uint flAllocationType , uint flProtect ) ;
18+
19+ [ DllImport ( "KERNEL32.DLL" ) ]
20+ private static extern bool VirtualFreeEx ( IntPtr hProcess , IntPtr lpAdress , uint dwSize , uint dwFreeType ) ;
21+
22+ [ DllImport ( "KERNEL32.DLL" ) ]
23+ private static extern bool WriteProcessMemory ( IntPtr hProcess , IntPtr lpBaseAddress , byte [ ] lpBuffer , int nSize , int lpNumberOfBytesWritten ) ;
24+
25+ [ DllImport ( "KERNEL32.DLL" ) ]
26+ private static extern IntPtr CreateRemoteThread ( IntPtr hProcess , IntPtr se , uint dwStackSize , IntPtr lpStartAddress , IntPtr lpParameter , uint dwCreationFlags , uint lpThreadId ) ;
27+
28+ [ DllImport ( "KERNEL32.DLL" , CharSet = CharSet . Ansi ) ]
29+ private extern static IntPtr GetProcAddress ( IntPtr hModule , string lpProcName ) ;
30+
31+ [ DllImport ( "KERNEL32.DLL" , CharSet = CharSet . Ansi ) ]
32+ private static extern IntPtr GetModuleHandle ( string lpModuleName ) ;
33+
34+ [ DllImport ( "KERNEL32.DLL" ) ]
35+ private static extern bool CloseHandle ( IntPtr hObject ) ;
36+
37+ [ DllImport ( "KERNEL32.DLL" ) ]
38+ private static extern uint WaitForSingleObject ( IntPtr hHandle , uint dwMilliSeconds ) ;
39+
40+ private const uint PROCESS_ALL_ACCESS = ( uint ) ( 0x0002 | 0x0400 | 0x0008 | 0x0010 | 0x0020 ) ;
41+ private const uint MEM_COMMIT = 0x1000 ;
42+ private const uint MEM_RELEASE = 0x8000 ;
43+ private const uint PAGE_EXECUTE_READWRITE = 0x40 ;
44+ private const uint WAIT_ABANDONED = 0x00000080 ;
45+ private const uint WAIT_OBJECT_0 = 0x00000000 ;
46+ private const uint WAIT_TIMEOUT = 0x00000102 ;
47+ private const uint WAIT_FAILED = 0xFFFFFFFF ;
48+ #endregion
49+
50+ /// <summary>
51+ /// Function to inject a Dll
52+ /// </summary>
53+ /// <param name="DllName">Name of dll for inject.</param>
54+ /// <param name="ProcessName">Nombre del proceso en el que sera injectada la Dll.</param>
55+ public static bool StartInjection ( string DllName , uint ProcessID )
56+ {
57+ bool Injected = false ;
58+ try
59+ {
60+ IntPtr hProcess = new IntPtr ( 0 ) ; //openprocess
61+ IntPtr hModule = new IntPtr ( 0 ) ; //vritualAllocex
62+ IntPtr Injector = new IntPtr ( 0 ) ; //getprocadress
63+ IntPtr hThread = new IntPtr ( 0 ) ; //createremotethread
64+ int LenWrite = DllName . Length + 1 ;
65+ hProcess = OpenProcess ( PROCESS_ALL_ACCESS , false , ProcessID ) ;
66+ if ( hProcess != IntPtr . Zero )
67+ {
68+ hModule = VirtualAllocEx ( hProcess , IntPtr . Zero , ( UIntPtr ) LenWrite , MEM_COMMIT , PAGE_EXECUTE_READWRITE ) ;
69+ if ( hModule != IntPtr . Zero )
70+ {
71+ ASCIIEncoding Encoder = new ASCIIEncoding ( ) ;
72+ int Written = 0 ;
73+ if ( WriteProcessMemory ( hProcess , hModule , Encoder . GetBytes ( DllName ) , LenWrite , Written ) )
74+ {
75+ Injector = GetProcAddress ( GetModuleHandle ( "kernel32.dll" ) , "LoadLibraryA" ) ;
76+
77+ if ( Injector != IntPtr . Zero )
78+ {
79+ hThread = CreateRemoteThread ( hProcess , IntPtr . Zero , 0 , Injector , hModule , 0 , 0 ) ;
80+
81+ if ( hThread != IntPtr . Zero )
82+ {
83+ uint Result = WaitForSingleObject ( hThread , 10 * 1000 ) ;
84+ if ( Result != WAIT_FAILED || Result != WAIT_ABANDONED
85+ || Result != WAIT_OBJECT_0 || Result != WAIT_TIMEOUT )
86+ {
87+ if ( VirtualFreeEx ( hProcess , hModule , 0 , MEM_RELEASE ) )
88+ {
89+ if ( hThread != IntPtr . Zero )
90+ {
91+ CloseHandle ( hThread ) ;
92+ Injected = true ;
93+ return Injected ;
94+ }
95+ else Console . WriteLine ( "Bad thread handle ... injection failed" ) ;
96+ }
97+ else Console . WriteLine ( "Memory free problem ... injection failed" ) ;
98+ }
99+ else Console . WriteLine ( "WaitForSingle failed: " + Result . ToString ( ) + "...injection failed" ) ;
100+ }
101+ else Console . WriteLine ( "Problem when starting the thread ... injection failed" ) ;
102+ }
103+ else Console . WriteLine ( "LoadLibraryA address not found ... injection failed" ) ;
104+ }
105+ else Console . WriteLine ( "Write error in process ... injection failed" ) ;
106+ }
107+ else Console . WriteLine ( "Unallocated memory ... injection failed" ) ;
108+ }
109+ else Console . WriteLine ( "Unopened process ... injection failed" ) ;
110+ }
111+ catch ( Exception Exc ) { Console . WriteLine ( "Injection Error: " + Exc . ToString ( ) ) ; }
112+ return Injected ;
113+ }
114+ }
115+ }
0 commit comments