/
health.go
291 lines (254 loc) · 9.68 KB
/
health.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package cmd
import (
"fmt"
"os"
"path"
"sort"
"strings"
"github.com/fatih/color"
"github.com/j3ssie/osmedeus/core"
"github.com/j3ssie/osmedeus/execution"
"github.com/j3ssie/osmedeus/libs"
"github.com/j3ssie/osmedeus/utils"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
func init() {
var healthCmd = &cobra.Command{
Use: "health",
Aliases: []string{"hea", "heal", "health", "healht"},
Short: "Run diagnostics to check configurations",
Long: core.Banner(),
RunE: runHealth,
}
RootCmd.AddCommand(healthCmd)
healthCmd.PreRun = func(cmd *cobra.Command, args []string) {
if options.FullHelp {
cmd.Help()
os.Exit(0)
}
}
}
func runHealth(_ *cobra.Command, args []string) error {
if options.PremiumPackage {
fmt.Printf("💠 Osmedeus Premium %s: Run diagnostics to ensure that everything is in arrange.\n", libs.VERSION)
} else {
fmt.Printf("🚀 Osmedeus %s: Run diagnostics to ensure that everything is in arrange.\n", libs.VERSION)
}
sort.Strings(args)
var err error
for _, arg := range args {
switch arg {
case "store", "git", "storages", "stora":
err = checkStorages()
case "cloud", "dist", "provider":
err = checkCloud()
case "all", "a", "full":
err = checkStorages()
if err != nil {
fmt.Println(color.YellowString("⚠️️ There is might be something wrong with your storages: %v\n", err))
}
err = checkCloud()
if err != nil {
fmt.Println(color.YellowString("%s If you install osmedeus on a single machine then it's okay to ignore the cloud setup\n", "[!] Cloud config setup incorrectly."))
}
err = generalCheck()
if err != nil {
fmt.Printf("‼️ There is might be something wrong with your setup: %v\n", color.HiRedString("%v", err))
return nil
}
break
}
if err != nil {
fmt.Println(color.YellowString("⚠️️ There is might be something wrong with your cloud or storages setup: %v\n", err))
return nil
}
}
if len(args) > 0 {
return nil
}
if err = generalCheck(); err != nil {
fmt.Printf("‼️ There is might be something wrong with your setup: %v\n", err)
return nil
}
if err = listFlows(); err != nil {
fmt.Printf("‼️ There is might be something wrong with your workflow setup: %v\n", err)
return nil
}
if err = listDefaultModules(); err != nil {
fmt.Printf("‼️ There is might be something wrong with your workflow setup: %v\n", err)
return nil
}
fmt.Printf(color.GreenString("\n🦾 Everything is in order. Happy Hacking 🦾\n"))
fmt.Printf("💡 Refer to this page if you're uncertain about which workflow to run: %v\n", color.GreenString("https://docs.osmedeus.org/installation/running-your-first-scan/ "))
fmt.Printf("⏱️ The current baseline threads hold is %v\n", color.GreenString("%v", options.Threads))
return nil
}
func checkCloud() error {
// check packer program
if _, err := utils.RunCommandWithErr("packer -h"); err != nil {
if _, err := utils.RunCommandWithErr(fmt.Sprintf("%s -h", path.Join(options.Env.BinariesFolder, "packer"))); err != nil {
color.Red("[-] Packer program setup incorrectly")
return fmt.Errorf("error checking core programs: %v", fmt.Sprintf("%s -h", path.Join(options.Env.BinariesFolder, "packer")))
}
}
// check config files
if !utils.FileExists(options.CloudConfigFile) {
return fmt.Errorf("distributed cloud config doesn't exist: %v", path.Join(options.Env.CloudConfigFolder, "provider.yaml"))
}
if utils.DirLength(path.Join(options.Env.CloudConfigFolder, "providers")) == 0 {
return fmt.Errorf("providers file doesn't exist: %v", path.Join(options.Env.CloudConfigFolder, "providers"))
}
// check SSH Keys
if !utils.FileExists(options.Cloud.SecretKey) {
keysDir := path.Dir(options.Cloud.SecretKey)
os.RemoveAll(keysDir)
utils.MakeDir(keysDir)
utils.DebugF("Generate SSH Key at: %v", options.Cloud.SecretKey)
if _, err := utils.RunCommandWithErr(fmt.Sprintf(`ssh-keygen -t ed25519 -f %s -q -N ''`, options.Cloud.SecretKey)); err != nil {
color.Red("[-] error generated SSH Key for cloud config at: %v", options.Cloud.SecretKey)
return fmt.Errorf("[-] error generated SSH Key for cloud config at: %v", options.Cloud.SecretKey)
}
}
if !utils.FileExists(options.Cloud.PublicKey) {
return fmt.Errorf("providers SSH Key missing: %v", options.Cloud.PublicKey)
}
fmt.Printf("[+] Health Check Cloud Config: %s\n", color.GreenString("✔"))
return nil
}
func checkStorages() error {
utils.DebugF("Checking storages setup")
if !execution.ValidGitURL(options.Storages["summary_repo"]) {
return fmt.Errorf("invalid git summary: %v", options.Storages["summary_repo"])
}
utils.DebugF("Check if your summary directory is exist or not: %v", options.Env.StoragesFolder)
if utils.DirLength(options.Env.StoragesFolder) < 1 {
return fmt.Errorf("storages folder doesn't exist: %v", options.Env.StoragesFolder)
}
utils.DebugF("Check the secret key for git usage: %v", options.Storages["secret_key"])
if !utils.FileExists(options.Storages["secret_key"]) {
return fmt.Errorf("secret key for git command doesn't exist: %v", options.Storages["secret_key"])
}
fmt.Printf("[+] Health Check Storages Config: %s\n", color.GreenString("✔"))
return nil
}
func generalCheck() error {
exist := utils.FolderExists(options.Env.BaseFolder)
if !exist {
color.Red("[-] Core folder setup incorrect: %v", options.Env.BaseFolder)
return fmt.Errorf("error running diagnostics")
}
// check core programs
var err error
var errorBinary []string
if _, err = utils.RunCommandWithErr("timeout --help"); err != nil {
errorBinary = append(errorBinary, "timeout")
}
if _, err = utils.RunCommandWithErr("amass -h"); err != nil {
errorBinary = append(errorBinary, "amass")
}
if _, err = utils.RunCommandWithErr(fmt.Sprintf("%s -h", path.Join(options.Env.BinariesFolder, "httpx"))); err != nil {
errorBinary = append(errorBinary, "httpx")
}
// if _, err = utils.RunCommandWithErr(fmt.Sprintf("%s -h", path.Join(options.Env.BinariesFolder, "httprobe"))); err != nil {
// errorBinary = append(errorBinary, "httprobe")
// }
if _, err = utils.RunCommandWithErr(fmt.Sprintf("%s -h", path.Join(options.Env.BinariesFolder, "nuclei"))); err != nil {
errorBinary = append(errorBinary, "nuclei")
}
if len(errorBinary) > 0 {
color.Red("[-] Core program setup incorrectly")
return fmt.Errorf("error checking core programs: %v", color.HiCyanString(strings.Join(errorBinary, ", ")))
}
fmt.Printf("[+] Health Check Core Programs: %s\n", color.GreenString("✔"))
// Check core signatures
okVuln := false
if utils.DirLength("~/nuclei-templates") > 0 {
okVuln = true
}
if okVuln {
fmt.Printf("[+] Health Check Vulnerability scanning config: %s\n", color.GreenString("✔"))
} else {
color.Red("vulnerability scanning config setup incorrectly")
return fmt.Errorf("vulnerability scanning config setup incorrectly")
}
// check data folder
if utils.FolderExists(options.Env.DataFolder) {
fmt.Printf("[+] Health Check Data Config: %s\n", color.GreenString("✔"))
} else {
color.Red("[-] Data setup incorrectly: %v", options.Env.DataFolder)
return fmt.Errorf("[-] Data setup incorrectly: %v", options.Env.DataFolder)
}
return nil
}
func listFlows() error {
flows := core.ListFlow(options)
if len(flows) == 0 {
color.Red("[-] Error to list workflows: %s", options.Env.WorkFlowsFolder)
return fmt.Errorf("[-] Error to list workflows: %s", options.Env.WorkFlowsFolder)
}
fmt.Printf("[+] Health Check Workflows: %s\n", color.GreenString("✔"))
if options.PremiumPackage {
fmt.Printf("💎 Making use of the premium workflow\n")
}
var content [][]string
for _, flow := range flows {
parsedFlow, err := core.ParseFlow(flow)
if err != nil {
utils.ErrorF("Error parsing flow: %v", flow)
continue
}
if parsedFlow.SkipIndexed {
continue
}
row := []string{
parsedFlow.Name, parsedFlow.Desc,
}
content = append(content, row)
}
fmt.Printf("\nFound %v available workflows at: %s \n\n", color.HiGreenString("%v", len(content)), color.HiCyanString(options.Env.WorkFlowsFolder))
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Flow Name", "Description"})
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetColWidth(110)
table.SetAutoWrapText(true)
table.AppendBulk(content)
table.Render()
h := color.HiCyanString("\nUsage:\n")
h += color.HiGreenString(" osmedeus scan -f %v", color.HiMagentaString("[flowName]")) + color.HiGreenString(" -t ") + color.HiMagentaString("[target]") + "\n"
fmt.Printf(h)
return nil
}
func listDefaultModules() error {
defaultModule := path.Join(options.Env.WorkFlowsFolder, "default-modules")
modules := core.DefaultWorkflows(options)
if len(modules) == 0 {
return fmt.Errorf("[-] Error to list default modules: %s", defaultModule)
}
var content [][]string
for _, flow := range modules {
parsedModule, err := core.ParseModules(flow)
if err != nil {
utils.ErrorF("Error parsing flow: %v", flow)
continue
}
row := []string{
parsedModule.Name, parsedModule.Desc,
}
content = append(content, row)
}
fmt.Printf("\nFound %v default modules at: %s \n\n", color.HiGreenString("%v", len(content)), color.HiCyanString(defaultModule))
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Module Name", "Description"})
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetColWidth(110)
table.SetAutoWrapText(true)
table.AppendBulk(content)
table.Render()
h := color.HiCyanString("\nModule Usage:\n")
h += color.HiGreenString(" osmedeus scan -m %v", color.HiMagentaString("[moduleName]")) + color.HiGreenString(" -t ") + color.HiMagentaString("[target]") + "\n\n"
fmt.Printf(h)
return nil
}