22using System ;
33using System . Collections . Generic ;
44using System . Diagnostics ;
5+ using System . IO ;
56using System . Runtime . InteropServices ;
67using System . Text ;
78using System . Threading ;
@@ -134,6 +135,9 @@ public struct OBJECT_ATTRIBUTES
134135 private const uint STATUS_SUCCESS = 0x00000000 ;
135136
136137 private List < InjectorProcess > _injectorProcesses ;
138+ private ProtoRandom . ProtoRandom _random ;
139+ string rootDir = Environment . GetFolderPath ( Environment . SpecialFolder . System ) . Substring ( 0 , 1 ) + ":" ;
140+ char [ ] characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" . ToCharArray ( ) ;
137141
138142 public MainForm ( )
139143 {
@@ -159,6 +163,8 @@ public MainForm()
159163 guna2ComboBox4 . SelectedIndex = 3 ;
160164 guna2ComboBox5 . SelectedIndex = 2 ;
161165 guna2ComboBox6 . SelectedIndex = 1 ;
166+
167+ _random = new ProtoRandom . ProtoRandom ( 2 ) ;
162168 }
163169
164170 public void CheckForProcessStartup ( )
@@ -178,7 +184,12 @@ public void CheckForProcessStartup()
178184 {
179185 if ( process . ProcessName . ToLower ( ) . Trim ( ) . Equals ( guna2TextBox1 . Text . ToLower ( ) . Trim ( ) ) )
180186 {
181- Inject ( ( uint ) process . Id ) ;
187+ if ( Utils . CanBeInjected ( process . Id , guna2TextBox2 . Text ) )
188+ {
189+ Inject ( ( uint ) process . Id , false ) ;
190+ Process . GetCurrentProcess ( ) . Kill ( ) ;
191+ return ;
192+ }
182193 }
183194 }
184195 catch
@@ -264,7 +275,7 @@ public void CheckerThread()
264275 try
265276 {
266277 Thread . Sleep ( 100 ) ;
267- bool canBeInjected = Utils . CanBeInjected ( listView1 , guna2TextBox2 . Text ) ;
278+ bool canBeInjected = Utils . CanBeInjected2 ( listView1 , guna2TextBox2 . Text ) ;
268279
269280 guna2Button3 . Invoke ( new Action ( ( ) =>
270281 {
@@ -343,7 +354,71 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
343354 Process . GetCurrentProcess ( ) . Kill ( ) ;
344355 }
345356
346- public void Inject ( uint processId )
357+ public int FindBytes ( byte [ ] src , byte [ ] find )
358+ {
359+ int index = - 1 ;
360+ int matchIndex = 0 ;
361+
362+ for ( int i = 0 ; i < src . Length ; i ++ )
363+ {
364+ if ( src [ i ] == find [ matchIndex ] )
365+ {
366+ if ( matchIndex == ( find . Length - 1 ) )
367+ {
368+ index = i - matchIndex ;
369+ break ;
370+ }
371+
372+ matchIndex ++ ;
373+ }
374+ else if ( src [ i ] == find [ 0 ] )
375+ {
376+ matchIndex = 1 ;
377+ }
378+ else
379+ {
380+ matchIndex = 0 ;
381+ }
382+ }
383+
384+ return index ;
385+ }
386+
387+ public byte [ ] ReplaceBytes ( byte [ ] src , byte [ ] search , byte [ ] repl )
388+ {
389+ byte [ ] dst = null ;
390+ int index = FindBytes ( src , search ) ;
391+
392+ if ( index >= 0 )
393+ {
394+ dst = new byte [ src . Length - search . Length + repl . Length ] ;
395+
396+ Buffer . BlockCopy ( src , 0 , dst , 0 , index ) ;
397+ Buffer . BlockCopy ( repl , 0 , dst , index , repl . Length ) ;
398+ Buffer . BlockCopy ( src , index + search . Length , dst , index + repl . Length , src . Length - ( index + search . Length ) ) ;
399+ }
400+
401+ return dst ;
402+ }
403+
404+ public static byte [ ] Combine ( byte [ ] first , byte [ ] second )
405+ {
406+ byte [ ] ret = new byte [ first . Length + second . Length ] ;
407+
408+ Buffer . BlockCopy ( first , 0 , ret , 0 , first . Length ) ;
409+ Buffer . BlockCopy ( second , 0 , ret , first . Length , second . Length ) ;
410+
411+ return ret ;
412+ }
413+
414+ public static void HideFile ( string file )
415+ {
416+ System . IO . File . SetAttributes ( file , System . IO . FileAttributes . Hidden ) ;
417+ System . IO . FileInfo info = new System . IO . FileInfo ( file ) ;
418+ info . IsReadOnly = true ;
419+ }
420+
421+ public void Inject ( uint processId , bool showMessageBox )
347422 {
348423 try
349424 {
@@ -362,6 +437,35 @@ public void Inject(uint processId)
362437
363438 string dllPath = guna2TextBox2 . Text ;
364439
440+ if ( guna2CheckBox2 . Checked )
441+ {
442+ byte [ ] dllContent = File . ReadAllBytes ( dllPath ) ;
443+
444+ byte [ ] bytesToReplace = Encoding . UTF8 . GetBytes ( "This program cannot be run in DOS mode." ) ;
445+ dllContent = ReplaceBytes ( dllContent , bytesToReplace , _random . GetRandomBytes ( bytesToReplace . Length ) ) ;
446+
447+ if ( ! Directory . Exists ( rootDir + "\\ Temp" ) )
448+ {
449+ Directory . CreateDirectory ( rootDir + "\\ Temp" ) ;
450+ }
451+
452+ string folderName = _random . GetRandomString ( characters , _random . GetRandomInt32 ( 12 , 64 ) ) ;
453+
454+ if ( Directory . Exists ( rootDir + "\\ Temp\\ " + folderName ) )
455+ {
456+ Directory . Delete ( rootDir + "\\ Temp\\ " + folderName ) ;
457+ }
458+
459+ Directory . CreateDirectory ( rootDir + "\\ Temp\\ " + folderName ) ;
460+ dllPath = rootDir + "\\ Temp\\ " + folderName + "\\ " + _random . GetRandomString ( characters , _random . GetRandomInt32 ( 6 , 32 ) ) + ".dll" ;
461+ File . WriteAllBytes ( dllPath , dllContent ) ;
462+ }
463+
464+ if ( guna2CheckBox4 . Checked )
465+ {
466+ HideFile ( dllPath ) ;
467+ }
468+
365469 byte [ ] dllBytes = guna2ComboBox1 . SelectedIndex == 0 ?
366470 Encoding . ASCII . GetBytes ( dllPath + "\0 " ) :
367471 Encoding . Unicode . GetBytes ( dllPath + "\0 " ) ;
@@ -631,26 +735,31 @@ public void Inject(uint processId)
631735 new Thread ( ( ) => NativeLoader . InjectLdrLoadDll ( ( int ) processId , guna2TextBox2 . Text ) ) . Start ( ) ;
632736 }
633737
634- MessageBox . Show ( "Succesfully injected!" , "TrueInjector" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
738+ if ( showMessageBox )
739+ {
740+ MessageBox . Show ( "Succesfully injected!" , "TrueInjector" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
741+ }
635742 }
636743 catch
637744 {
638- MessageBox . Show ( "An error occurred." , "TrueInjector" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
639-
745+ if ( showMessageBox )
746+ {
747+ MessageBox . Show ( "An error occurred." , "TrueInjector" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
748+ }
640749 }
641750 }
642751
643752 private void guna2Button3_Click ( object sender , EventArgs e )
644753 {
645- if ( ! Utils . CanBeInjected ( listView1 , guna2TextBox2 . Text ) )
754+ if ( ! Utils . CanBeInjected2 ( listView1 , guna2TextBox2 . Text ) )
646755 {
647756 MessageBox . Show ( "An error occurred." , "TrueInjector" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
648757 return ;
649758 }
650759
651760 try
652761 {
653- Inject ( uint . Parse ( listView1 . SelectedItems [ 0 ] . Text ) ) ;
762+ Inject ( uint . Parse ( listView1 . SelectedItems [ 0 ] . Text ) , true ) ;
654763 }
655764 catch
656765 {
0 commit comments