기타/꿀팁

윈도우 - Proxy를 Powershell로, 나아가 단축키로 실행시켜보자.

꿈꾸는 사람_Anthony 2021. 7. 7. 19:13
반응형

웹 분석과, 웹 해킹 공부를 위해, Burp Suite를 설치하였다.

Burp Suite에서 웹 request와 response를 분석하는 방법은 2가지이다.

첫째, 전용 브라우저 이용.

둘째, 프록시 이용.

 

필자는 두 가지 모두 사용하는 편이다. 

이 과정에서, 프록시를 설정할 때마다 매번 설정에 들어가서 하기는 참으로 귀찮다.

이를 단축키로 자동화할 수 있는 방법이 없을까를 찾아보던 중, powershell용으로 작성된 script를 발견하였다.

https://social.technet.microsoft.com/wiki/contents/articles/53831.enable-disable-proxy-settings-via-powershell.aspx

 

이 스크립트는 함수의 형태로, 앞으로 이 함수를 이용하여 쉽게 실행할 수 있을 것이다.

 

powershell에서 함수를 사용하는 방법은 여러가지가 있다.

  1. 현재 powershell 세션에서만 호출할 수 있는 형태
  2. 꾸준히 호출할 수 있는 형태

https://www.jonathanmedd.net/2015/01/how-to-make-use-of-functions-in-powershell.html

위 사이트에서 사용법을 확인할 수 있을 것이다.

 

추가적으로, 다음의 방법도 사용할 수 있다.

●해당 powershell의 세션에 함수 로드

https://morgantechspace.com/2016/07/how-to-call-function-in-ps1-file-from-powershell.html

 . C:ScriptsMyScript.ps1
 ##혹은
 Import-Module C:ScriptsMyScript.psm1

●로드과정없이 함수 자체를 스크립트(.ps1파일)에서 호출 (아래 명령 실행시, 함수가 실행된다)

https://stackoverflow.com/questions/1405750/calling-a-specific-powershell-function-from-the-command-line

powershell -command "& { . 스크립트주소.ps1; 함수이름 }"
## 예시 powershell -command "& { . c:\test\script1.ps1; My-Func }"

 

필자는 꾸준히 호출하여 사용할 수 있도록 할 것이다. (profile 설정)

 

Step 1# profile설정.

New-Item -Path $profile -ItemType File -Force
notepad $PROFILE

notepad $PROFILE을 엔터로 실행하면, 메모장이 뜰 것이다.

해당 메모장 안에 다음 내용을 붙여넣는다.

<#
 .Synopsis
 This function will set the proxy settings provided as input to the cmdlet.
 .Description
 This function will set the proxy server and (optional) Automatic configuration script.
 .Parameter Proxy Server
 This parameter is set as the proxy for the system.
 Data from. This parameter is Mandatory.
 .Example
 Setting proxy information.
 Set-NetProxy -proxy "proxy:7890"
 .Example
 Setting proxy information and (optional) Automatic Configuration Script.
 Set-NetProxy -proxy "proxy:7890" -acs "http://proxy Jump :7892"
#>


Function Set-NetProxy
    {
        [CmdletBinding()]
        Param(
           
            [Parameter(Mandatory=$True,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
            [String[]]$Proxy,

            [Parameter(Mandatory=$False,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
            [AllowEmptyString()]
            [String[]]$acs
                   
        )

        Begin
        {

                $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
           
        }
       
        Process
        {
           
            Set-ItemProperty -path $regKey ProxyEnable -value 1

            Set-ItemProperty -path $regKey ProxyServer -value $proxy
                               
            if($acs)
            {         
               
                     Set-ItemProperty -path $regKey AutoConfigURL -Value $acs       
            }

        }
       
        End
        {

            Write-Output "Proxy is now enabled"

            Write-Output "Proxy Server : $proxy"

            if ($acs)
            {
               
                Write-Output "Automatic Configuration Script : $acs"

            }
            else
            {
               
                Write-Output "Automatic Configuration Script : Not Defined"

            }
        }
    }


Function Disable-NetProxy
{
  Begin
    {

            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
       
    }
   
    Process
    {
       
        Set-ItemProperty -path $regKey ProxyEnable -value 0 -ErrorAction Stop

        Set-ItemProperty -path $regKey ProxyServer -value "" -ErrorAction Stop
                           
        Set-ItemProperty -path $regKey AutoConfigURL -Value "" -ErrorAction Stop       
      
    }
   
    End
    {

        Write-Output "Proxy is now Disabled"

             
    }

}

저장하고 메모장 창을 닫아준다.

 

Step2# 원할 때 powershell을 열고 실행해준다.

(추가적으로 방금 적용한 profile을 현재의 powershell 세션(현재 열려있는 powershell 창)에 적용하려면, 다음 명령을 실행해주자.https://stackoverflow.com/questions/11546069/refreshing-restarting-powershell-session-w-out-exiting)

. $PROFILE

간단한 사용법은 다음과 같다.

이외의 사용법에 대해서는 위 함수 소스코드의 맨 위 주석을 참고하자.

## Proxy 시작
Set-NetProxy 프록시(IP)주소:포트
## 예시 : Set-NetProxy 127.0.0.1:8080

## Proxy 종료
Disable-NetProxy

사용하는 예시에 대한 스크린샷이다.

●Proxy 시작(enable)

●Proxy 종료(disable)

 

 

 

 

여기까지만 해도 충분하다. 상당히 간편하게 proxy를 키고 끌 수 있게되었다. 하지만, 미래의 나에게 투자하는 마음으로, 단축키를 지정해주었다. 사실 파워쉘 키는 것이 귀찮..

 

단축키 지정에 대한 자세한 설명은 다음 포스팅을 참고하자.

(link)

 

 

반응형