Blocking CTRL+ALT+DEL and simple DLL injecting

Share your advanced PureBasic knowledge/code with the community.
125
User
User
Posts: 15
Joined: Mon Mar 28, 2005 10:12 pm
Contact:

Blocking CTRL+ALT+DEL and simple DLL injecting

Post by 125 »

Hello,
i had a work-free morning and were a bit bored. So I started coding this code, because blocking CTRL+ALT+DEL is asked often.
It is a combination of DLL injecting Functions and a source of a DLL to inject in winlogon.exe to disable CTRL+ALT+DEL.

I only tested on XP, If someone could test it on Vista.... :)

The code was inspired by http://www.codeproject.com/win32/Antoni ... print=true

I ported some passages of it in this code.

Sorry for my bad English. :oops:
Functions
Inject(PID,DLLPath.s) -> Inject DLL, Important: Full path!
Eject(PID,hLibModule) -> Eject DLL, hLibModule is returned by Inject
GetPIDFromName(Name.s) -> Get PID of an EXE
DebugPrevileg(Bool) -> #True = Debug Previleg on, is needed for injecting in SYSTEM apps. This function is used by Inject and Eject automatically
The functions:

Code: Select all

EnableExplicit  

Procedure GetPIDFromName(Name.s) 
Define hSnapshot.l, ProcessInfo.PROCESSENTRY32, Retval.l, PID.l 
  
  hSnapshot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0) 
  ProcessInfo\dwSize = SizeOf(ProcessInfo) 
  Retval = Process32First_(hSnapshot,@ProcessInfo) 
  
  While Retval 
    If PeekS(@ProcessInfo\szExeFile) = Name 
      PID=PeekL(@ProcessInfo\th32ProcessID) 
      Break 
    Else 
      Retval = Process32Next_(hSnapshot,@ProcessInfo) 
    EndIf 
  Wend 
  
ProcedureReturn PID 
EndProcedure 

Procedure DebugPrevileg(Flag) 
Define tk.TOKEN_PRIVILEGES, hToken.l 

OpenProcessToken_(GetCurrentProcess_(),#TOKEN_ADJUST_PRIVILEGES | #TOKEN_QUERY | #TOKEN_READ, @hToken) 

LookupPrivilegeValue_(0,"SeDebugPrivilege",tk\Privileges\Luid) 

tk\PrivilegeCount = 1 
If Flag = #True 
tk\Privileges\Attributes = #SE_PRIVILEGE_ENABLED 
Else 
tk\Privileges\Attributes = 0 
EndIf 

AdjustTokenPrivileges_(hToken,0,tk,0,0,0) 

EndProcedure 

Procedure Inject(PID,DLLPath.s) 
   Define   hThread.l, hProcess.l,pLibRemote.l,hLibModule.l 

  DebugPrevileg(#True) 

   hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, #False, PID) 

   pLibRemote = VirtualAllocEx_(hProcess, #Null, 255, #MEM_COMMIT, #PAGE_READWRITE) 
    
   WriteProcessMemory_(hProcess, pLibRemote, DLLPath, 255, #Null) 
    
   hThread = CreateRemoteThread_(hProcess,#Null,0,GetProcAddress_(GetModuleHandle_("Kernel32"), "LoadLibraryA"),pLibRemote,0,#Null) 
    
   WaitForSingleObject_(hThread, #INFINITE) 
   GetExitCodeThread_(hThread, @hLibModule) 
   CloseHandle_(hThread) 
   CloseHandle_(hProcess) 

   VirtualFreeEx_(hProcess, pLibRemote, SizeOf(DLLPath), #MEM_RELEASE) 

  DebugPrevileg(#False) 
  
  ProcedureReturn hLibModule 
EndProcedure 

Procedure Eject(PID,hLibModule.l) 
   Define   hThread.l, hProcess.l 

  DebugPrevileg(#True) 

   hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, #False, PID) 
    
   hThread = CreateRemoteThread_(hProcess,#Null,0,GetProcAddress_(GetModuleHandle_("Kernel32"), "FreeLibrary"),hLibModule,0,#Null) 
    
   WaitForSingleObject_(hThread, #INFINITE) 
   CloseHandle_(hThread) 
   CloseHandle_(hProcess) 

  DebugPrevileg(#False) 
EndProcedure
The DLL for injecting in winlogon.exe and disabling CTRL+ALT+DEL

Code: Select all

Global hSASWnd.l,OldSASProc.l 

ProcedureDLL MakeLong(low,high) 
  ProcedureReturn low + (high << 16) 
EndProcedure 

ProcedureDLL SASWindowProc(hWnd,uMsg,wParam,lParam) 
If uMsg = #WM_HOTKEY 
      If lParam = MakeLong(#MOD_CONTROL | #MOD_ALT, #VK_DELETE) 
         ProcedureReturn 1 
      EndIf 
EndIf 

   ProcedureReturn CallWindowProc_(OldSASProc, hWnd, uMsg, wParam, lParam) 
EndProcedure 

ProcedureDLL AttachProcess(hInstance) 
       hSASWnd = FindWindow_("SAS Window class","SAS window") 
        OldSASProc = SetWindowLong_(hSASWnd, #GWL_WNDPROC, @SASWindowProc()) 
EndProcedure 

ProcedureDLL DetachProcess(hInstance) 
     SetWindowLong_(hSASWnd, #GWL_WNDPROC, OldSASProc) 
EndProcedure
Examplecode:

Code: Select all

Define hLibModule.l,PID.l 

PID=GetPidFromName("winlogon.exe") 
hLibModule=Inject(PID,<Put full path of the DLL here>) 

Delay(10000) 

Eject(PID,hLibModule)
Sincerely
125
tomijan
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Dec 11, 2005 1:32 pm

Post by tomijan »

@125
thanks for sharing, interesting code, but

Code: Select all

 VirtualFreeEx_(hProcess, pLibRemote, SizeOf(DLLPath), #MEM_RELEASE)
should be replaced by

Code: Select all

 VirtualFreeEx_(hProcess, pLibRemote, #Null, #MEM_RELEASE)
it works because SizeOf(DLLPath) give wrong result - 0, but its small mistake :)

tom
registered user
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

This is a very dangerous subject... :evil:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
mueckerich
User
User
Posts: 22
Joined: Thu Dec 16, 2004 10:36 am
Location: Germany/Allgaeu

Post by mueckerich »

No, my knife isn't dangerous if i use it it to cut my bread, it's only dangerous if i kill somebody with it. There will be more danger with all the guns in the world. :shock:

This code can be very helpfull, especialy on PC-Systems in the industrie where you control machines or have a HMI (Human machine interface). There is a necessity to lock the system to prevent accidents and loss of production.

IMHO there are People who seed maleware or build it with some "MakeYourOwnMalwareToolKit". They don't reard such postings because the most of them don't understand what it means. The other ones who build the "MakeYourOwnMalwareToolKit" and sell it to the others, know this stuff already. These guys are not dumb but criminal. :x
Believe means you don't know
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

mueckerich wrote:No, my knife isn't dangerous if i use it it to cut my bread, it's only dangerous if i kill somebody with it. There will be more danger with all the guns in the world. :shock:

This code can be very helpfull, especialy on PC-Systems in the industrie where you control machines or have a HMI (Human machine interface). There is a necessity to lock the system to prevent accidents and loss of production.

IMHO there are People who seed maleware or build it with some "MakeYourOwnMalwareToolKit". They don't reard such postings because the most of them don't understand what it means. The other ones who build the "MakeYourOwnMalwareToolKit" and sell it to the others, know this stuff already. These guys are not dumb but criminal. :x
agree. This code is very useful for me.

Thank you for share it, 125.
PB 6.21 beta, PureVision User
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

sweet .. just like having the guns .. never know when you need to send
a "reach out and touch someone" special kind of greeting card.

this will go nice with my 'unlock and delete file structures' routines... j/k :twisted:

- np
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Post by eJan »

Thanks 125!
Please write solution to work from copiled exe without .dll.
Godai
Enthusiast
Enthusiast
Posts: 171
Joined: Thu Oct 05, 2006 8:13 pm

Post by Godai »

I sincerely hope it's not possible to block CTRL-ALT-DELETE in Vista.
This is user expected behavior and a severe breach of the user interface guidelines ;)
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

But being able to block ctrl-alt-delete is good for programs that run on demo machines or kiosks, etc
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

lol.. evil is rising ..

- np
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

Very nice, 125. Thanks for sharing.
:shock:
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

DoubleDutch wrote:But being able to block ctrl-alt-delete is good for programs that run on demo machines or kiosks, etc
Indeed
I like logic, hence I dislike humans but love computers.
hardfalcon
User
User
Posts: 89
Joined: Fri Apr 29, 2005 3:03 pm
Location: Luxembourg
Contact:

Post by hardfalcon »

It's not working in Vista (So you shouldn't run Vista on a kiosk machine ;) nor should you run it on ANY computer whatsoever :twisted: ).

//EDIT: This is the thread in the german forum:
http://www.purebasic.fr/german/viewtopic.php?t=13074
If I remember the whole thing right, this is also possible without a DLL, using code injection instead of DLL injection. 125 achieved this, to, but won't publish it because it wouldn't be useful for any applications except malware...

//EDIT 2: Some more or less similar codes from Thorium in the german forum:
http://www.purebasic.fr/german/viewtopic.php?t=13047
http://www.purebasic.fr/german/viewtopic.php?t=13011
"And God caused a deep sleep to fall upon Adam, and he slept: and he took one of his ribs, and closed up the flesh instead thereof; And the spare rib, which God had taken from man, made he a woman, and brought her unto the man"
Post Reply