programming ⌨/파워쉘 PowerShell

나를 위한 PowerShell(3) : Execution Policy와 우회

Kortsec1 2024. 2. 26. 23:24

우선 아무 PowerShell 명령이 들어있는 ps1파일을 생성해 보자

New-Item -ItemType File -Path C:\Temp -Name pscript.ps1

New-Item

 

ps1파일은 cmd의 batch파일과 같이 PowerShell 명령이 작성된 스크립트 파일이다.

생성되었다면 내용을 입력할 차례다

Add-Content -Path C:\Temp\pscript.ps1 -Value 'Get-Date'

 

생성한 pscript.ps1 스크립트를 실행해 보자

Execution Error

 

이 시스템에서 스크립트를 실행할 수 없다는 오류가 뜬다.

오늘 다뤄볼 주제는 바로 이 부분이다.

 

 

Execution Policy

우선 Execution Policy에 대해 알아야 한다.

PowerShell Execution Policy는 PowerShell이 구성 파일을 로드하고 스크립트를 실행하는 조건을 제어해 주는 안전장치다

실행 정책은 사용자로 하여금 의도치 않게 기본 규칙을 위반(e.g. 악성 스크립트 실행 방지) 하지 않도록 방지한다.

 

Execution Policy에는 특정 설정값이 들어가 있는데 종류는 다음과 같다.

  • Restricted : 어떠한 스크립트도 실행할 수 없다
  • AllSigned : 스크립트를 실행할 수 있지만, 신뢰할 수 있는 게시자의 서명이 필요하다
  • RemoteSigned : AllSigned와 같지만 로컬 컴퓨터에서 작성된(인터넷에서 다운로드X) 스크립트에는 서명이 필요하지 않다
  • Bypass : 모든 스크립트가 실행 가능하며, 경고 또는 프롬프트가 없다

이 밖에도 Unrestricted, undefined와 같은 설정값이 있으니 궁금하면 참고하도록 하자

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

 

설정값 설명 중 서명에 대한 내용이 있는데, 현재로선 중요하지 않으니 다음에 자세히 다뤄보도록 하겠다

 

다시 돌아와서 처음 우리가 ps1파일을 실행하는데 실패한 것을 기억하는가?

그렇다면 우리의 Execution Policy를 확인해 볼 차례다.

Get-ExecutionPolicy

Get-ExecutionPolicy

Restricted, 모든 스크립트가 제한된 상태임을 알 수 있다.

 

 

Bypassing the Execution Policy

이를 해결할 수 있는 방법이 여럿 존재한다.

기억하자 Execution Policy는 사용자의 작업을 제한하는 보안 시스템이 아니다.

 

 

Set-ExecutionPolicy

우선 가장 먼저 떠오르는 방법으로는 Execution Policy값을 바꾸는 것이다.

Set-ExecutionPolicy RemoteSigned

Set-ExecutionPolicy RemoteSigned

재확인 과정이 나타나면 동의하고 진행한다.

다시 Get-ExecutionPolicy를 진행해 보면 바뀐 것을 확인할 수 있을 것이다.

 

RemoteSigned값은 위에서 설명했듯 로컬 컴퓨터에서 작성한 스크립트는 실행가능하다.

이제 맨 처음 생성한 pscript.ps1파일을 실행해 보자

execution success

성공적으로 실행되는 모습이다.

 

 

Read and Pipe

Pipeline을 사용하여 각 명령의 입출력을 손쉽게 제어할 수 있다.

 

두 번째 방식은 "type", "Get-Content"와 같은 명령어로 파일을 읽어오고, 파이프라인을 이용하여 powershell의 인자로 넘겨주어 실행하는 방식이다.

아래 코드를 입력해 보면 ExecutionPolicy를 우회할 수 있다.

Get-Content C:\Temp\pscript.ps1 | powershell.exe -noprofile -
type C:\Temp\pscript.ps1 | powershell.exe -noprofile -

pip to pwershell stdin

 

위 powershell 실행 시 -noprofile - 속성은 각각 다음을 의미한다.

  • NoProfile : PowerShell 프로필을 로드하지 않는다
  • - (Hyphen) : 표준 입력에서 명령 텍스트를 읽는다

 

비슷한 형태로 powershell.exe가 아닌 Invoke-Expression 명령을 실행하는 방법도 있다.

Get-Content C:\Temp\pscript.ps1 | Invoke-Expression
type C:\Temp\pscript.ps1 | Invoke-Expression

pip to Invoke-Expression

 

 

Execution Policy Flag

Execution Policy를 임시적으로 변경하며 실행할 수 있는 방법도 존재한다.

현재 로컬파일을 실행하기 위함으로 Bypass, Unrestricted 모두 사용 가능하다.

PowerShell -ExecutionPolicy Bypass -File C:\Temp\pscript.ps1
PowerShell -ExecutionPolicy UnRestricted -File C:\Temp\pscript.ps1

Execution Policy Flag

 

각 ExecutionPolicy 값의 의미는 앞쪽에 있다.

이 밖에 다양하고 신박한 우회법은 링크를 참고하자

 

 

요약


Execution Policy : PowerShell이 스크립트를 실행하는 조건을 제어해 주는 안전장치
    - Restricted : 어떠한 스크립트도 X
    - AllSigned : 서명이 존재할 경우 O
    - Bypass : 모든 스크립트 O

Execution Policy 우회법
    1. Set-ExecutionPolicy를 통한 Execution Policy 값 변경
    2. script를 읽어오고 pipeline을 통해 powershell.exe에 전달 (Invoke-Expression 가능)
    3. 일회성 ExecutionPolicy를 부여하며 파일 실행

New-Item : 파일 생성 (-ItemType, -Path, -Name)
Add-Content : 파일에 내용 입력 (-Path, -Value)
Get-ExecutionPolicy : Execution Policy 상태 출력
Set-ExecutionPolicy : Execution Policy 상태 변경
Invoke-Expression : 코드 실행
powershell.exe
    -NoProfile : 프로필 로드X
    -(hyphen) : 표준 입력을 통해 명령을 읽는 powershell 실행
    -ExecutionPolicy : 일회성 ExecutionPolicy 값 부여
    -File : 파일 경로 입력


 

 

참고

https://sid-500.com/2018/01/10/powershell-for-beginners-part-5-the-execution-policy-ps1-files/

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

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.1