PowerShell访问Windows Api

admin 2021年12月11日14:35:02评论937 views字数 2335阅读7分47秒阅读模式

Powershell是微软的后期开发语言,使用c#/.net编写扩展的powershell,意味着能做几乎所有的事情。

1.User32 : : MessageBox

创建消息框,是调用api最直接的一个。

Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class User32
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool MessageBox(
IntPtr hWnd, /// Parent window handle
String text, /// Text message to display
String caption, /// Window caption
int options); /// MessageBox type
}
"@
[User32]::MessageBox(0,"Text","Caption",0) |Out-Null

执行以上的脚本:

PowerShell访问Windows Api
当然也可以改变消息框的类型。

[User32]::MessageBox(0,"Text","Caption",0x4)

PowerShell访问Windows Api
2.User32 : : CallWindowProc

从dll中调用一个功能函数。

[Kernel32]::LoadLibrary                 # Load DLL     |___[Kernel32]::GetProcAddress      # Get function pointer            |___[User32]::CallWindowProc # Call function

CallWindowProc只有在当前函数没有任何参数的情况下才能生效,User32.dll中包含一个LockWorkStation函数,使用来锁定用户桌面的。

PowerShell访问Windows Api
运行这个脚本,将会锁定桌面。

PowerShell访问Windows Api
重新登录后,可以看到该函数提供的输出

PowerShell访问Windows Api
3.MSFvenom : : WinExec

尝试用msfvenom生成一个dll。

PowerShell访问Windows Api
我没有太多次使用msfpayload生成dll。因为他做不到我想要做的事,而且所有的内容会在ida中暴露出来。

PowerShell访问Windows Api
该dll并没有调用WinExec,而是dll在建立时调用了CreateProcess.

PowerShell访问Windows Api
这个调用有些古怪,看起来像是CreateProcess在挂起(dwCreationFlags = 0x44)的状态下,启动rundll32.exe,rundll32.exe通常是在lpApplicationName,这次却被放置在lpCommandLine,无论怎么样,当lpCommandLine第一个参数被当作模块名称时lpApplicationName可以为null,是合法的。

然后shellcode获得了进程的句柄,注入进字节数组的载荷并恢复线程。

PowerShell访问Windows Api
反观最初目标,从powershell中执行一个有效的payload是简单直接的。由于所有的东西都在ida的DLLMain中,我们只需要从适当的路径调用LoadLibrary。一个缺点是,当我们开始调用的话,powershell将会被冻结,为避免这种情况,需要在后台启动进程。

PowerShell访问Windows Api
执行之后会弹出计算器。

PowerShell访问Windows Api
4.Kernel32 : : CreateProcess

之前演示的都是小规模的,不会遇到什么问题,但并非每次都是这样。调用CreateProcess API是一个很好的例子,但是当你在远程计算机上运行命令的时候,总会弹出cmd窗口,再仔细看windows api可以发现CreateProcess API提供了更详细的功能,包括去处cmd的gui窗口。

BOOL WINAPI CreateProcess(   _In_opt_    LPCTSTR               lpApplicationName,   _Inout_opt_ LPTSTR                lpCommandLine,   _In_opt_    LPSECURITY_ATTRIBUTES lpProcessAttributes, --> SECURITY_ATTRIBUTES Struct   _In_opt_    LPSECURITY_ATTRIBUTES lpThreadAttributes,  --> SECURITY_ATTRIBUTES Struct   _In_        BOOL                  bInheritHandles,   _In_        DWORD                 dwCreationFlags,   _In_opt_    LPVOID                lpEnvironment,   _In_opt_    LPCTSTR               lpCurrentDirectory,   _In_        LPSTARTUPINFO         lpStartupInfo,       --> STARTUPINFO Struct   _Out_       LPPROCESS_INFORMATION lpProcessInformation --> PROCESS_INFORMATION Struct);

PowerShell访问Windows Api
PowerShell访问Windows Api
上面的代码,标志着没有窗口的执行cmd.exe,然后使用cmd.exe执行calc.exe,你可以确认,cmd与进程管理器没有关联的窗口。

PowerShell访问Windows Api
代码有点麻烦,修改下。

PowerShell访问Windows Api
PowerShell访问Windows Api
PowerShell访问Windows Api
来看下。

第一张是正常显示cmd.exe的窗口的。

PowerShell访问Windows Api
下面是经过我修改代码之后的效果,是不显示cmd.exe窗口的。

PowerShell访问Windows Api
这里的cmd被称作“无窗口cmd”,cmd.exe去执行bitsadmin命令抓取在greyhathacker域中执行的二进制文件和结果。

PowerShell访问Windows Api

结束语

希望本文章能在powershell调用windows api这方面给你一些启发。


from:http://www.fuzzysecurity.com/tutorials/24.html

翻译:关注安全技术&&heresec

转载注明。

本文始发于微信公众号(关注安全技术):PowerShell访问Windows Api

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年12月11日14:35:02
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   PowerShell访问Windows Apihttp://cn-sec.com/archives/502794.html

发表评论

匿名网友 填写信息