Infrastructure/Network & Security

Powershell로 Fortigate 여러개의 IP Address 주소 생성기 만들기

seongho.choi 2024. 5. 26. 03:28
반응형

제목 그대로, 아주 간단하면서도 업무에 효율성을 높여줄 스크립트를 만들어 볼 것이다.

 

보통 기업 CERT 부서에서 방화벽을 사용한다면 한 번쯤 해보는 것이 BlackList IP 차단일 것이다.

하지만, 보통 BlackList IP 차단하는게 여간 힘든 것이 아니다. 수동으로 일일히 클릭하면서 넣는 경우도 있겠지만, CLI를 통해 하나씩 IP 넣어가면서 작업하는 분도 계실 것이다.

 

하지만, 차단해야할 IP가 만다면 하나씩 반복적으로 넣기 힘들뿐더러, 실수로 다른 IP가 차단될 수 있기 때문이다.

때문에 Powershell를 통해서 이러한 문제점을 해결해줄 GUI 스크립트를 만들어 볼 것이다.

 

Add-Type -AssemblyName System.Windows.Forms

# GUI 폼 생성
$form = New-Object System.Windows.Forms.Form
$form.Text = "Fortigate IP Address 생성기"
$form.Size = New-Object System.Drawing.Size(400, 300)
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

# 접두어 입력 텍스트 박스 생성
$prefixLabel = New-Object System.Windows.Forms.Label
$prefixLabel.Text = "Name 앞 접두어를 입력하세요.:"
$prefixLabel.Location = New-Object System.Drawing.Point(10, 20)
$prefixLabel.Size = New-Object System.Drawing.Size(200, 20)
$form.Controls.Add($prefixLabel)

$prefixBox = New-Object System.Windows.Forms.TextBox
$prefixBox.Location = New-Object System.Drawing.Point(220, 20)
$prefixBox.Size = New-Object System.Drawing.Size(150, 20)
$form.Controls.Add($prefixBox)

# IP 주소 입력 텍스트 박스 생성 (여러 줄 입력 가능, 스크롤 바 포함)
$ipLabel = New-Object System.Windows.Forms.Label
$ipLabel.Text = "IP를 입력하세요. (엔터로 구분합니다.):"
$ipLabel.Location = New-Object System.Drawing.Point(10, 60)
$ipLabel.Size = New-Object System.Drawing.Size(250, 20)
$form.Controls.Add($ipLabel)

$ipBox = New-Object System.Windows.Forms.TextBox
$ipBox.Location = New-Object System.Drawing.Point(10, 90)
$ipBox.Size = New-Object System.Drawing.Size(360, 100)
$ipBox.Multiline = $true
$ipBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
$form.Controls.Add($ipBox)

# "저장" 버튼 생성
$saveButton = New-Object System.Windows.Forms.Button
$saveButton.Text = "생성!"
$saveButton.Location = New-Object System.Drawing.Point(150, 200)
$form.Controls.Add($saveButton)

# 버튼 클릭 이벤트 핸들러
$saveButton.Add_Click({
    $prefix = $prefixBox.Text
    $ipInput = $ipBox.Text
    $ipAddresses = $ipInput -split "`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
    
    if ($ipAddresses.Count -eq 0) {
        [System.Windows.Forms.MessageBox]::Show("No IP addresses entered. Exiting.")
        return
    }

    # 명령어 문자열 생성
    $commands = "config firewall address`n"
    foreach ($ip in $ipAddresses) {
        $commands += "edit '${prefix}_$ip'`n"
        $commands += "set subnet $ip 255.255.255.255`n"
        $commands += "next`n"
    }
    $commands += "end"

    # 결과를 표시할 새로운 폼 생성
    $resultForm = New-Object System.Windows.Forms.Form
    $resultForm.Text = "Generated Commands"
    $resultForm.Size = New-Object System.Drawing.Size(400, 400)
    $resultForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

    $resultBox = New-Object System.Windows.Forms.TextBox
    $resultBox.Multiline = $true
    $resultBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
    $resultBox.ReadOnly = $true
    $resultBox.Size = New-Object System.Drawing.Size(360, 320)
    $resultBox.Location = New-Object System.Drawing.Point(10, 10)
    $resultBox.Text = $commands

    $resultForm.Controls.Add($resultBox)
    $resultForm.ShowDialog()
})

# 폼을 표시
$form.ShowDialog()

 

반응형