Function/Setup/Install-F2B.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<#
    .NOTES
        File Name : Install-F2B
        Author : Thomas ILLIET, contact@thomas-illiet.fr
        Date : 2018-02-15
        Last Update : 2018-02-15
        Version : 1.0.1
#>


function Install-F2B(){

    Write-Host "# ++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Yellow
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "Fail2Ban Install"
    Write-Host "# ++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Yellow

    # Get install configuration
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Get install configuration"
    Try {
        $ConfigFile = Get-Content (Join-Path -Path $F2BModuleRoot -ChildPath "Config/Install.json") -ErrorAction Stop
        $Config =  $ConfigFile | ConvertFrom-Json
    } Catch {
        Write-Error "Unable to get configuration file : $_"
        break
    }

    # Create Registry Item
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Create Registry Items :"
    try {
        foreach($Item in $Config.Registry.Item){

            $RegistryPath = "$($Item.Path)/$($Item.Name)"
            Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "`t- $RegistryPath"
        
            if((Test-Path $RegistryPath) -eq $false) {
                New-Item -Path $Item.Path -Name $Item.Name -ErrorAction Stop | Out-Null
            }
        }
    } Catch {
        write-error "Unable to create registry Item ($RegistryPath) : $_"
        break
    }

    # Create Registry Property
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Create Registry Propertys"
    Try {
        foreach($Item in $Config.Registry.Property){

            $RegistryPath = "$($Item.Path)/$($Item.Name)"
            $ItemCollection = (Get-Item $Item.Path).Property
            Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "`t- $RegistryPath"

            if($ItemCollection.Contains($Item.Name) -eq $false) {
                New-ItemProperty -Path $Item.Path -Name $Item.Name -Value $Item.Value -PropertyType $Item.Type -ErrorAction Stop | Out-Null
            }
        }
    } Catch {
        write-error "Unable to create Property Item ($RegistryPath) : $_"
        break
    }

    # Create Folders
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Create Folders :"
    Try {
        foreach($Item in $Config.Registry.folders){

            Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "`t- $Item"
            if((Test-Path $Item) -eq $false) {
                New-Item -ItemType directory -Path $Item -ErrorAction Stop | Out-null
            }
        }
    } Catch {
        write-error "Unable to create Folder ($Item) : $_"
        exit
    }

    # Create EventLog Source
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Create EventLog Source"
    if((Test-Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\Fail2Ban") -eq $false) {
        Try {
            New-EventLog -LogName Application -Source "Fail2Ban" -ErrorAction Stop
        } Catch {
            Write-Error "Unable to create source into EventLog : $_"
            break
        }
    }

    # Create Scheduled Task
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Create Scheduled Task :"
    Try {
        # Service
        Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "`t- Fail2ban-Service"
        if((Get-ScheduledTask -TaskName "Fail2Ban-Manager" -ErrorAction SilentlyContinue) -eq $null) {
            $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& {Import-Module Fail2ban; Initialize-F2BService}"'
            $trigger =  New-ScheduledTaskTrigger -AtStartup
            Register-ScheduledTask -Action $action -Trigger $trigger -User "System" -TaskName "Fail2Ban-Service" -Description "Fail2Ban is an intrusion prevention Powershell framework that protects computer servers from brute-force attacks"  -ErrorAction Stop | Out-Null
        }

        # Manager
        Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "`t- Fail2ban-Manager"
        if((Get-ScheduledTask -TaskName "Fail2Ban-Manager" -ErrorAction SilentlyContinue) -eq $null) {
            $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& {Retart-F2B}"'
            $trigger =  New-ScheduledTaskTrigger -Daily -At 0am
            Register-ScheduledTask -Action $action -Trigger $trigger -User "System" -TaskName "Fail2Ban-Manager" -Description "Fail2Ban is an intrusion prevention Powershell framework that protects computer servers from brute-force attacks"  -ErrorAction Stop | Out-Null
        }
    } Catch {
        Write-Error "Unable to create Scheduled Task : $_"
        break
    }

    Write-Host "# ++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Yellow
}