Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

WhiteTiger

This is no cleaned up version... as it's the code I've used and tested with
It's part of my local __ProcessX.au3 I use for different kind of things...

;~    https://www.autoitscript.com/forum/topic/88214-_winapi_getcommandlinefrompid-from-any-process/

;~ Func _WinAPI_GetCommandLineFromPID($iPID)
;~ If $iDELETE = replace CommandLine with "*" so no one can read it again (ProgramPath isn't replaced)
;~ If $iDELETE > 1 = even replace the ProgramPath
Func _ProcessGetCommandLine($iPID = @AutoItPID, $iDELETE = 0)
   Local $dwDesiredAccess = 0x0410; 0x0410 = PROCESS_VM_READ (0x0010) | PROCESS_QUERY_INFORMATION (0x0400)         0x0438 = && PROCESS_VM_OPERATION (0x0008) | PROCESS_VM_WRITE (0x0020)
   If $iDELETE Then
      $iDELETE = Int($iDELETE + 0);
      $dwDesiredAccess += 0x0028;
   EndIf
   Local $aCall = DllCall('Kernel32.dll', 'HANDLE', 'OpenProcess', _
         'DWORD', $dwDesiredAccess, _
         'BOOL', 0, _
         'DWORD', $iPID)
   If @error Or Not $aCall[0] Then
      Return SetError(1, 0, '')
   EndIf
   Local $hProcess = $aCall[0]
   Local $tPROCESS_BASIC_INFORMATION = DllStructCreate('dword_ptr ExitStatus;' & _
         'ptr PebBaseAddress;' & _
         'dword_ptr AffinityMask;' & _
         'dword_ptr BasePriority;' & _
         'dword_ptr UniqueProcessId;' & _
         'dword_ptr InheritedFromUniqueProcessId')
   $aCall = DllCall('ntdll.dll', 'int', 'NtQueryInformationProcess', _
         'handle', $hProcess, _
         'dword', 0, _ ; ProcessBasicInformation
         'ptr', DllStructGetPtr($tPROCESS_BASIC_INFORMATION), _
         'dword', DllStructGetSize($tPROCESS_BASIC_INFORMATION), _
         'dword*', 0)
   If @error Then
      DllCall('Kernel32.dll', 'bool', 'CloseHandle', 'handle', $hProcess)
      Return SetError(2, 0, '')
   EndIf
   Local $tPEB = DllStructCreate('byte InheritedAddressSpace;' & _
         'byte ReadImageFileExecOptions;' & _
         'byte BeingDebugged;' & _
         'byte Spare;' & _
         'ptr Mutant;' & _
         'ptr ImageBaseAddress;' & _
         'ptr LoaderData;' & _
         'ptr ProcessParameters;' & _
         'ptr SubSystemData;' & _
         'ptr ProcessHeap;' & _
         'ptr FastPebLock;' & _
         'ptr FastPebLockRoutine;' & _
         'ptr FastPebUnlockRoutine;' & _
         'dword EnvironmentUpdateCount;' & _
         'ptr KernelCallbackTable;' & _
         'ptr EventLogSection;' & _
         'ptr EventLog;' & _
         'ptr FreeList;' & _
         'dword TlsExpansionCounter;' & _
         'ptr TlsBitmap;' & _
         'dword TlsBitmapBits[2];' & _
         'ptr ReadOnlySharedMemoryBase;' & _
         'ptr ReadOnlySharedMemoryHeap;' & _
         'ptr ReadOnlyStaticServerData;' & _
         'ptr AnsiCodePageData;' & _
         'ptr OemCodePageData;' & _
         'ptr UnicodeCaseTableData;' & _
         'dword NumberOfProcessors;' & _
         'dword NtGlobalFlag;' & _
         'ubyte Spare2[4];' & _
         'int64 CriticalSectionTimeout;' & _
         'dword HeapSegmentReserve;' & _
         'dword HeapSegmentCommit;' & _
         'dword HeapDeCommitTotalFreeThreshold;' & _
         'dword HeapDeCommitFreeBlockThreshold;' & _
         'dword NumberOfHeaps;' & _
         'dword MaximumNumberOfHeaps;' & _
         'ptr ProcessHeaps;' & _
         'ptr GdiSharedHandleTable;' & _
         'ptr ProcessStarterHelper;' & _
         'ptr GdiDCAttributeList;' & _
         'ptr LoaderLock;' & _
         'dword OSMajorVersion;' & _
         'dword OSMinorVersion;' & _
         'dword OSBuildNumber;' & _
         'dword OSPlatformId;' & _
         'dword ImageSubSystem;' & _
         'dword ImageSubSystemMajorVersion;' & _
         'dword ImageSubSystemMinorVersion;' & _
         'dword GdiHandleBuffer[34];' & _
         'dword PostProcessInitRoutine;' & _
         'dword TlsExpansionBitmap;' & _
         'byte TlsExpansionBitmapBits[128];' & _
         'dword SessionId')
   $aCall = DllCall('Kernel32.dll', 'bool', 'ReadProcessMemory', _
         'ptr', $hProcess, _
         'ptr', DllStructGetData($tPROCESS_BASIC_INFORMATION, 'PebBaseAddress'), _
         'ptr', DllStructGetPtr($tPEB), _
         'dword', DllStructGetSize($tPEB), _
         'dword*', 0)
   If @error Or Not $aCall[0] Then
      DllCall('Kernel32.dll', 'bool', 'CloseHandle', 'handle', $hProcess)
      Return SetError(3, 0, '')
   EndIf
   Local $tPROCESS_PARAMETERS = DllStructCreate('dword AllocationSize;' & _
         'dword ActualSize;' & _
         'dword Flags;' & _
         'dword Unknown1;' & _
         'word LengthUnknown2;' & _
         'word MaxLengthUnknown2;' & _
         'ptr Unknown2;' & _
         'handle InputHandle;' & _
         'handle OutputHandle;' & _
         'handle ErrorHandle;' & _
         'word LengthCurrentDirectory;' & _
         'word MaxLengthCurrentDirectory;' & _
         'ptr CurrentDirectory;' & _
         'handle CurrentDirectoryHandle;' & _
         'word LengthSearchPaths;' & _
         'word MaxLengthSearchPaths;' & _
         'ptr SearchPaths;' & _
         'word LengthApplicationName;' & _
         'word MaxLengthApplicationName;' & _
         'ptr ApplicationName;' & _
         'word LengthCommandLine;' & _
         'word MaxLengthCommandLine;' & _
         'ptr CommandLine;' & _
         'ptr EnvironmentBlock;' & _
         'dword Unknown[9];' & _
         'word LengthUnknown3;' & _
         'word MaxLengthUnknown3;' & _
         'ptr Unknown3;' & _
         'word LengthUnknown4;' & _
         'word MaxLengthUnknown4;' & _
         'ptr Unknown4;' & _
         'word LengthUnknown5;' & _
         'word MaxLengthUnknown5;' & _
         'ptr Unknown5;')
   $aCall = DllCall('Kernel32.dll', 'bool', 'ReadProcessMemory', _
         'ptr', $hProcess, _
         'ptr', DllStructGetData($tPEB, 'ProcessParameters'), _
         'ptr', DllStructGetPtr($tPROCESS_PARAMETERS), _
         'dword', DllStructGetSize($tPROCESS_PARAMETERS), _
         'dword*', 0)
   If @error Or Not $aCall[0] Then
      DllCall('Kernel32.dll', 'bool', 'CloseHandle', 'handle', $hProcess)
      Return SetError(4, 0, '')
   EndIf
   $aCall = DllCall('Kernel32.dll', 'bool', 'ReadProcessMemory', _
         'ptr', $hProcess, _
         'ptr', DllStructGetData($tPROCESS_PARAMETERS, 'CommandLine'), _
         'wstr', '', _
         'dword', DllStructGetData($tPROCESS_PARAMETERS, 'MaxLengthCommandLine'), _
         'dword*', 0)
   If @error Or Not $aCall[0] Then
      DllCall('Kernel32.dll', 'bool', 'CloseHandle', 'handle', $hProcess)
      Return SetError(5, 0, '')
   EndIf
   If $iDELETE Then
      Local $tmp = '';
      Local $tmp_len = DllStructGetData($tPROCESS_PARAMETERS, 'LengthCommandLine');
      Local $tmp_slen = ($tmp_len / 2)
      Local $tmp_nlen = 0;
      Local $pos = 0;
      If $iDELETE == 1 Then
         $pos = StringInStr($aCall[3], ' ');
         If $pos <> 0 Then
            Local $pos2 = StringInStr($aCall[3], '"');
            If $pos2 == 0 Or $pos < $pos2 Then
               $tmp = StringLeft($aCall[3], $pos);
            Else
               $pos2 = StringInStr($aCall[3], '"', 0, 2);
               $pos = StringInStr($aCall[3], ' ', 0, 1, $pos2);
               If $pos <> 0 Then
                  $tmp = StringLeft($aCall[3], $pos);
               Else
                  $pos = $tmp_slen;
               EndIf
            EndIf
         Else
            $pos = $tmp_slen;
         EndIf
      EndIf
;~       $tmp &= '/TUStart /pid:532';
      For $i = $pos To $tmp_slen
         $tmp &= '*';
      Next
      _WinAPI_WriteProcessMemory($hProcess, DllStructGetData($tPROCESS_PARAMETERS, 'CommandLine'), $tmp, $tmp_len, $tmp_nlen, 'wstr');
      If $tmp_nlen <> $tmp_len Then
;~          ConsoleWrite('WriteError!' & @LF);
         DllCall('Kernel32.dll', 'bool', 'CloseHandle', 'handle', $hProcess)
         Return SetError(0, 6, $aCall[3])
      EndIf
   EndIf
   DllCall('Kernel32.dll', 'bool', 'CloseHandle', 'handle', $hProcess)
   Return $aCall[3]
EndFunc   ;==>_ProcessGetCommandLine
madburg

WhiteTiger wrote:

It's done in AutoIt and actually OpenSource... so I'll handle it out on request (and it's ugly as there's a lot of comments, unused crap and that like... As I said, not finished)

@WhiteTiger, would you be so kind and share the AutoIT code block(s) that re-write the process commandline, so no one can view the original process commandline (which contains the USR/PWD) in taskmgr.

Much appreciated.
Petr

Implemented in 5.2.2 beta and released today :-)
WhiteTiger

What's wrong with my wrapper :P
It does all that... passes "port", "directory" etc^^ Well... passing the directory is a bit hacky as WinSCP has no support for that one.
And even though I'm using my wrapper and I'm kind of happy with it, a native solution within WinSCP is preferred... (and faster/better directory change on KiTTY's part)
The worst thing about my wrapper is (besides the fact I haven't completed it) that it still has to read from registry and clean it afterwards... it leaves no tracks but still shouldn't be considered "portable"
Simba

Hi, Martin,

Well i doubt somebody will start from scratch. If somebody will decide to build another version, they probably would use putty sources. Same as Mozilla, bunch of Firefox spin-offs.

Problem with passing parameters, there does not seem to be a way to pass custom PORT, keeping in mind that first step securing your server is to change SSH port, makes this method useless.

Of course, it would be the best, if we could pass host, login info, PORT and even directory.
martin

Simba wrote:

Next step would be to rename PuTTY instances to "SSH client" (i believe in future there might arise new , even better alternatives).

It's still not generic enough to support any SSH client fully. It relies on registry keys format of PuTTY and its clones. But you can at least pass username, password and hostname to any SSH client that is able to accept them on command line (patterns !U, !P and !@).

Display Note "Portable Kitty version not supported".

That contradicts your suggestion above, doesn't it? Anyway, even portably KiTTY is partially supported, when you use !U@!@ pattern to pass username and hostname.
Simba

Tested and it's working perfectly.

Next step would be to rename PuTTY instances to "SSH client" (I believe in future there might arise new, even better alternatives).

Include dropdown – PuTTY or KiTTY

Display Note "Portable Kitty version not supported".

And we are good to go.

Thanks!
Simba

Martin, my above post provides very simple solution.

Maybe you could add it for another users?

Thanks.
Simba

Actually I found very easy way.

On <invalid hyperlink removed by admin>:
The problem was that KiTTY is using a different key in the Windows registry to store its settings.

For PuTTY you have:
[HKCU\Software\SimonTatham\PuTTY\*]

For KiTTY it's
[HKCU\Software\9bis.com\KiTTY\*]

So in order to launch a KiTTY session straight from WinSCP (portable) you need to edit WinSCP.ini

Just browse for the line:
PuttyRegistryStorageKey=Software%5CSimonTatham%5CPuTTY

and replace it with:
PuttyRegistryStorageKey=Software%5C9bis.com%5CKiTTY

Just make sure you are using NOT portable KiTTY version.
After changing WinSCP registry key, change path to Kitty.exe and it will work.
martin