-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
181 lines (148 loc) · 4.5 KB
/
main.go
File metadata and controls
181 lines (148 loc) · 4.5 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bufio"
_ "embed"
"fmt"
"installer/auth"
"installer/db"
"os"
"os/exec"
"strings"
"time"
)
//go:embed suricata.yaml
var data string
// Command represents a shell command with a description and emoji
type Command struct {
cmd string
description string
emoji string
}
func main() {
// Check if running with sudo
if os.Geteuid() != 0 {
fmt.Println("❌ This script must be run with sudo privileges")
fmt.Println("Please run: sudo go run main.go")
os.Exit(1)
}
envMap := db.EnvLoader()
auth.Authentication(envMap)
commands := []Command{
{"apt update", "Updating package lists", "📦"},
{"apt upgrade -y", "Upgrading packages", "⬆️"},
{"apt -y install libnetfilter-queue-dev libnetfilter-queue1 libnfnetlink-dev libnfnetlink0 jq", "Installing dependencies", "🔧"},
{"add-apt-repository ppa:oisf/suricata-stable -y", "Adding Suricata repository", "📚"},
{"apt install suricata -y", "Installing Suricata", "🛡️"},
{"systemctl stop suricata.service", "Stopping Suricata service", "🛑"},
}
for _, cmd := range commands {
executeCommand(cmd)
}
// Inject custom Suricata configuration
fmt.Println("\n📝 Updating Suricata configuration file...")
updateSuricataConfig()
// Update rules
fmt.Println("\n📜 Listing available rule sources...")
listRuleSources()
// Restart Suricata with new configuration
finalCommands := []Command{
{"suricata-update", "Updating Suricata rules", "🔄"},
{"suricata -T -c /etc/suricata/suricata.yaml -v", "Testing configuration", "🧪"},
{"systemctl restart suricata.service", "Restarting Suricata service", "♻️"},
{"curl http://testmynids.org/uid/index.html", "Testing IDS functionality", "🌐"},
{"cat /var/log/suricata/fast.log", "Checking logs", "📋"},
}
for _, cmd := range finalCommands {
executeCommand(cmd)
}
fmt.Println("\n✅ Suricata installation and configuration complete! 🚀")
// Clear logs
clearSuricataLogs()
}
// Execute a shell command and log output
func executeCommand(cmd Command) {
fmt.Printf("\n%s %s...\n", cmd.emoji, cmd.description)
command := exec.Command("bash", "-c", cmd.cmd)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
fmt.Printf("❌ Error executing command: %v\n", err)
fmt.Println("Would you like to continue anyway? (y/n)")
if !confirmAction() {
os.Exit(1)
}
}
time.Sleep(1 * time.Second) // Small delay for readability
}
// Inject the embedded Suricata config into /etc/suricata/suricata.yaml
func updateSuricataConfig() {
configPath := "/etc/suricata/suricata.yaml"
file, err := os.Create(configPath)
if err != nil {
fmt.Printf("❌ Failed to update Suricata config: %v\n", err)
os.Exit(1)
}
defer file.Close()
var ifn = gtInterfaceDetails("ethernet")
if ifn == nil {
panic("No wan network interface found")
}
ifnStr, ok := ifn.(string)
if !ok {
fmt.Printf("❌ Failed to convert interface name to string\n")
os.Exit(1)
}
newData := strings.Replace(data, "_IFACE_", ifnStr, 1)
_, err = file.WriteString(newData)
if err != nil {
fmt.Printf("❌ Error writing to Suricata config: %v\n", err)
os.Exit(1)
}
fmt.Println("✅ Suricata configuration updated successfully! 🎉")
}
// List available rule sources
func listRuleSources() {
cmd := exec.Command("suricata-update", "list-sources")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
// Enable selected rule sources
func enableRuleSources() {
fmt.Println("Enter the names of the sources you want to enable (space-separated):")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
sources := strings.TrimSpace(input)
if sources != "" {
cmd := exec.Command("bash", "-c", "suricata-update enable-source "+sources)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
}
// Confirm user action
func confirmAction() bool {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
return strings.ToLower(strings.TrimSpace(input)) == "y"
}
// Clear the testing Logs
func clearSuricataLogs() {
fmt.Println("\n🧹 Clearing Suricata log files...")
commands := []string{
`sudo su -c 'echo "" > /var/log/suricata/eve.json'`,
`sudo su -c 'echo "" > /var/log/suricata/fast.log'`,
}
for _, cmd := range commands {
command := exec.Command("bash", "-c", cmd)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
fmt.Printf("❌ Error clearing logs: %v\n", err)
} else {
fmt.Printf("✅ Successfully cleared: %s\n", cmd)
}
}
}