2017-12-19 10 views
0

Ich möchte die Adresse der Funktion CreateTransaction in PowerShell erhalten.Wie bekomme ich CreateTransaction Adresse von PowerShell

Ich weiß, wie es in C++ zu tun:

#include "stdafx.h" 
#include <Windows.h> 

typedef NTSTATUS(NTAPI *CreateTransaction) 
(
    IN LPSECURITY_ATTRIBUTES lpTransactionAttributes OPTIONAL, 
    IN LPGUID    UOW OPTIONAL, 
    IN DWORD     CreateOptions OPTIONAL, 
    IN DWORD     IsolationLevel OPTIONAL, 
    IN DWORD     IsolationFlags OPTIONAL, 
    IN DWORD     Timeout OPTIONAL, 
    IN LPWSTR    Description OPTIONAL 
    ); 

int main() 
{ 
    HMODULE hKtmw32 = GetModuleHandle(L"Ktmw32.dll"); 
    CreateTransaction createTransaction = (CreateTransaction)GetProcAddress(hKtmw32, "CreateTransaction"); 

    return 0; 
} 

Wie kann ich es in Powershell?
Ich habe versucht, die folgende Funktion zu verwenden, um es zu tun.
Es funktioniert gut mit anderen Funktionen, aber nicht mit CreateTransaction.

function Local:Get-ProcAddress 
{ 
    Param 
    (
     [OutputType([IntPtr])] 

     [Parameter(Position = 0, Mandatory = $True)] 
     [String] 
     $Module, 

     [Parameter(Position = 1, Mandatory = $True)] 
     [String] 
     $Procedure 
    ) 

    # Get a reference to System.dll in the GAC 
    $SystemAssembly = [AppDomain]::CurrentDomain.GetAssemblies() | 
     Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].Equals('System.dll') } 
    $UnsafeNativeMethods = $SystemAssembly.GetType('Microsoft.Win32.UnsafeNativeMethods') 
    # Get a reference to the GetModuleHandle and GetProcAddress methods 
    $GetModuleHandle = $UnsafeNativeMethods.GetMethod('GetModuleHandle') 
    $GetProcAddress = $UnsafeNativeMethods.GetMethod('GetProcAddress') 
    # Get a handle to the module specified 
    $Kern32Handle = $GetModuleHandle.Invoke($null, @($Module)) 
    $hexAddrs = [convert]::ToString($Kern32Handle.ToInt32(), 16) 
    Write-Host "[*] Got $($Module) at 0x$($hexAddrs)" 
    $tmpPtr = New-Object IntPtr 
    $HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr, $Kern32Handle) 

    # Return the address of the function 
    Write-Output $GetProcAddress.Invoke($null, @([System.Runtime.InteropServices.HandleRef]$HandleRef, $Procedure)) 
} 

Get-ProcAddress Ktmw32.dll CreateTransaction # => DOES NOT WORK 
Get-ProcAddress ntdll.dll NtCreateSection # => WORKS 

Referenz:
https://github.com/HarmJ0y/Misc-PowerShell/blob/master/Get-System.ps1

Ich weiß nicht, warum es mich nicht zurückkehren die Adresse CreateTransaction.

Ich habe Abhilfe die Funktion zu nennen, aber es interessant, mich noch, wie kann ich die Funktionsadresse mit Powershell erhalten:

Add-Type -TypeDefinition @" 
    using System; 
    using System.Runtime.InteropServices; 
namespace PInvoke { 

    public class Program 
    { 
     [StructLayout(LayoutKind.Sequential)] 
     public struct SECURITY_ATTRIBUTES { 
      int nLength; 
      IntPtr lpSecurityDescriptor; 
      int bInheritHandle; 
     } 
     [DllImport("Ktmw32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
     public static extern IntPtr CreateTransaction( 
       SECURITY_ATTRIBUTES securityAttributes, 
       IntPtr guid, int options, int isolationLevel, int isolationFlags, 
       int milliSeconds, string description 
     ); 
    } 
} 
"@ 

$Class1 = New-Object PInvoke.Program 
$struct = New-Object PInvoke.Program+SECURITY_ATTRIBUTES 
[PInvoke.Program]::CreateTransaction($struct, 0, 0, 0, 0, 0, "notepad.exe") 
+0

Hat 'Write-Host '[*] $ ($ Module) bei 0x $ ($ hexAddrs)" 'irgendwas zurückgeben? –

+0

@LievenKeersmaekers Nur Ausgabe an die Zeichenfolge. Sie können diese Zeile ignorieren oder entfernen. – E235

+0

Ich weiß, was "Write-Host" tut. Die Frage war, zeigt es dir etwas Sinnvolles? Hat der GetModuleHandle zumindest gelingen? –

Antwort

2

Ich weiß nicht, warum es zurückgeben mir nicht die Adresse CreateTransaktion.

Aller Wahrscheinlichkeit nach, kehrt er nicht die Adresse, weil es nicht existiert - ich nicht von einem einzigen Modul oder einer Einrichtung in Powershell denken können, die auf KtmW32.dll abhängen, dass so vorausgesetzt, es hätte wurde geladen von powershell.exe scheint naiv.

Sie können dies überprüfen, indem Sie die Modules Eigenschaft des aktuellen Prozesses Abfrage:


Als Alternative zu Add-Type, können Sie die Bibliothek in den Prozess manuell über SafeNativeMethods.LoadLibrary() laden:

$SystemAssembly = [AppDomain]::CurrentDomain.GetAssemblies() | 
    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].Equals('System.dll') } |Select -First 1 
$SafeNativeMethods = $SystemAssembly.GetType('Microsoft.Win32.SafeNativeMethods') 
$KtmW32 = $SafeNativeMethods::LoadLibrary('KtmW32.dll') 

Jetzt können Sie Get-ProcAddress wie in Ihrem Beispiel verwenden, oder verwenden Sie das vonzurückgegebene Modulhandledirekt:

# Same as before 
$UnsafeNativeMethods = $SystemAssembly.GetType('Microsoft.Win32.UnsafeNativeMethods') 
$GetProcAddress = $UnsafeNativeMethods.GetMethod('GetProcAddress') 

$tmpPtr = New-Object IntPtr 

# Now use the module handle from LoadLibrary instead 
$HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr, $KtmW32) 

$GetProcAddress.Invoke($null,@([System.Runtime.InteropServices.HandleRef]$handleref,'CreateTransaction')) 
Verwandte Themen