capture command line of all running processes?

Windows specific forum
jassing
Addict
Addict
Posts: 1775
Joined: Wed Feb 17, 2010 12:00 am

capture command line of all running processes?

Post by jassing »

is there a way, when enumerating task processes, that I can obtain their full command line used to start them?

Edit: I think i found the api needed NtQueryInformationProcess()
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: capture command line of all running processes?

Post by Zebuddi123 »

Hi Jassing is this what your looking for :?: http://msdn.microsoft.com/en-gb/library ... s.85).aspx

typedef struct _RTL_USER_PROCESS_PARAMETERS {
BYTE Reserved1[16];
PVOID Reserved2[10];
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine <-------------------------------------------------here
} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;

Zebuddi. :)
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: capture command line of all running processes?

Post by em_uk »

I use Comate and WMI :

Code: Select all

XIncludeFile "COMatePLUS.pbi"

Procedure GetProcess(strComputer.s)
 
  strComputer.s
  Define.COMateObject objWMIService, objProcess
  colProcessList.COMateEnumObject
  objWMIService = COMate_GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
 
  If objWMIService
    colProcessList = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_Process')")
    If colProcessList
      objProcess = colProcessList\GetNextObject()
     
      While objProcess
        name$=objProcess\GetStringProperty("Name")
        handle=objProcess\GetIntegerProperty("Handle")
        path$=objProcess\GetStringProperty("ExecutablePath")
        time.l=objProcess\GetIntegerProperty("KernelModeTime")
        cmdline$=objProcess\GetStringProperty("CommandLine")
       
        username$=objProcess\GetStringProperty("Username, Domain")
       
        If objProcess\GetIntegerProperty("GetOwner(" + Str(@userName) + " byref)") = #S_OK
          username$=PeekS(userName, -1, #PB_Unicode)
          SysFreeString_(userName)
        Else
          Debug "Cannot locate user's name."
        EndIf
       
        objProcess = colProcessList\GetNextObject()
       
        AddGadgetItem(1,-1,Str(handle)+Chr(10)+name$+Chr(10)+username$+Chr(10)+path$+Chr(10)+cmdline$)
    Wend
    colProcessList\Release()
  EndIf
  objWMIService\Release()
 
Else
  MessageRequester("Processes", "Error detecting processes!")
EndIf
EndProcedure

If OpenWindow(0,10,10,800,600,"Processes")
  ListIconGadget(1,0,0,798,598,"Processes",50)
  AddGadgetColumn(1,100,"Name",100)
  AddGadgetColumn(1,100,"Username",100)
  AddGadgetColumn(1,100,"Path",250)
  AddGadgetColumn(1,100,"Cmd Line",650)
 
  GetProcess(".")
 
  Repeat
    Event = WindowEvent()
    Delay(5)
  Until Event = #PB_Event_CloseWindow
EndIf
Edit : Better working example. You just need COMatePLUS installed.

Edit 2 : fixed my Endif issue. Code was ripped from an app I wrote.
Last edited by em_uk on Mon Jun 03, 2013 2:45 pm, edited 1 time in total.
----

R Tape loading error, 0:1
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: capture command line of all running processes?

Post by MachineCode »

@em_uk: Your code doesn't compile. Missing an EndIf. Can't work out where it's meant to go because you've got a While/Else/EndIf loop, and if I change While to If, then I get a Break level too high error. Please check your code.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: capture command line of all running processes?

Post by RASHAD »

Code: Select all

Prototype.i GetModuleFileNameExW(hProcess.l,hModule.l,*lpFilename,nSize.i)
Prototype.i GetModuleFileNameExA(hProcess.l,hModule.l,*lpFilename,nSize.i)

CompilerIf #PB_Compiler_Unicode
  Global GetModuleFileNameEx.GetModuleFileNameExW
CompilerElse
  Global GetModuleFileNameEx.GetModuleFileNameExA
CompilerEndIf

Lib = OpenLibrary(#PB_Any,"psapi.dll")
If Lib
 
  CompilerIf #PB_Compiler_Unicode
    Global GetModuleFileNameEx.GetModuleFileNameExW = GetFunction(Lib,"GetModuleFileNameExW")
  CompilerElse
    Global GetModuleFileNameEx.GetModuleFileNameExA = GetFunction(Lib,"GetModuleFileNameExA")
  CompilerEndIf
  
Else

  MessageRequester("Warning", "Can not load Psapi.dll" ,#MB_ICONWARNING)
  End
  
EndIf

Procedure CheckRunningExe()
    Proc32.PROCESSENTRY32
    Proc32\dwSize = SizeOf(PROCESSENTRY32)
   
    snap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0) 
      If Snap
          If Process32First_(snap, @Proc32)
          While Process32Next_(snap, @Proc32)
              ImageName$ = PeekS(@Proc32\szExeFile)
              FilePath$ = Space(1024)
              hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, 0, Proc32\th32ProcessID)
              If hProcess
                  GetModuleFileNameEx(hProcess, 0, @FilePath$, Len(FilePath$))
                  CloseHandle_(hProcess)
              EndIf             
              Debug ImageName$
              Debug FilePath$
              Debug " "
          Wend
      EndIf
      CloseHandle_(Snap)
  EndIf

EndProcedure

CheckRunningExe()

Egypt my love
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: capture command line of all running processes?

Post by Bisonte »

@Rashad: I think this is not what jassing mean... he want the startparameters of the tasks.

I modified the code from em_uk to work :

Code: Select all

XIncludeFile "COMatePLUS.pbi"

EnableExplicit 

Procedure GetProcess(strComputer.s)

  Protected.COMateObject objWMIService, objProcess
  Protected colProcessList.COMateEnumObject
  Protected name$, handle, path$, time.l, cmdline$, username$, username
  
  objWMIService = COMate_GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
  
  If objWMIService
    colProcessList = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_Process')")
    If colProcessList
      objProcess = colProcessList\GetNextObject()
      
      While objProcess
        name$=objProcess\GetStringProperty("Name")
        handle=objProcess\GetIntegerProperty("Handle")
        path$=objProcess\GetStringProperty("ExecutablePath")
        time.l=objProcess\GetIntegerProperty("KernelModeTime")
        cmdline$=objProcess\GetStringProperty("CommandLine")
        
        username$=objProcess\GetStringProperty("Username, Domain")
        
        If objProcess\GetIntegerProperty("GetOwner(" + Str(@userName) + " byref)") = #S_OK
          username$=PeekS(userName, -1, #PB_Unicode)
          SysFreeString_(userName)
        Else
          Debug "Cannot locate user's name."
        EndIf
        
        objProcess = colProcessList\GetNextObject()
        
        AddGadgetItem(1,-1,Str(handle)+Chr(10)+name$+Chr(10)+username$+Chr(10)+path$+Chr(10)+cmdline$)
        
        If Not objProcess
          Break 1
        EndIf
        
      Wend
      colProcessList\Release()
    EndIf
    objWMIService\Release()
  
  Else
    MessageRequester("Processes", "Error detecting processes!")
  EndIf
  
EndProcedure

Define Event

If OpenWindow(0,10,10,800,600,"Processes")
  ListIconGadget(1,0,0,798,598,"Processes",50)
  AddGadgetColumn(1,100,"Name",100)
  AddGadgetColumn(1,100,"Username",100)
  AddGadgetColumn(1,100,"Path",250)
  AddGadgetColumn(1,100,"Cmd Line",650)
 
  GetProcess(".")
 
  Repeat
    Event = WindowEvent()
    Delay(5)
  Until Event = #PB_Event_CloseWindow
EndIf
At the column "Cmd Line" you see the parameters , like PBCompiler.exe /STANDBY /LANGUAGE Deutsch...
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
jassing
Addict
Addict
Posts: 1775
Joined: Wed Feb 17, 2010 12:00 am

Re: capture command line of all running processes?

Post by jassing »

Nice. thank you gents!
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: capture command line of all running processes?

Post by em_uk »

Fixed the missing Endif.

Also, my version is useful for targeting another machine.

Simply change the "." to what ever machine name/ip.

Obviously you need to be an admin of that device.
----

R Tape loading error, 0:1
Post Reply