1 | | - | package core |
2 | | - | |
3 | | - | import ( |
4 | | - | "context" |
5 | | - | "errors" |
6 | | - | "fmt" |
7 | | - | "path" |
8 | | - | "strings" |
9 | | - | "sync" |
10 | | - | "time" |
11 | | - | |
12 | | - | "github.com/spf13/cast" |
13 | | - | |
14 | | - | "github.com/fatih/color" |
15 | | - | "github.com/j3ssie/osmedeus/execution" |
16 | | - | "github.com/j3ssie/osmedeus/libs" |
17 | | - | "github.com/j3ssie/osmedeus/utils" |
18 | | - | "github.com/jinzhu/copier" |
19 | | - | "github.com/panjf2000/ants" |
20 | | - | ) |
21 | | - | |
22 | | - | // RunModule run the module |
23 | | - | func (r *Runner) RunModule(module libs.Module, options libs.Options) { |
24 | | - | // get more params |
25 | | - | MoreParams(module, &options) |
26 | | - | // get reports path |
27 | | - | options.Module = ResolveReports(module, options) |
28 | | - | r.LoadEngineScripts() |
29 | | - | |
30 | | - | // check if resume enable or not |
31 | | - | if (options.Resume || module.Resume) && !module.Forced { |
32 | | - | if CheckResume(options) { |
33 | | - | utils.BlockF(module.Name, "Resume detected") |
34 | | - | return |
35 | | - | } |
36 | | - | } |
37 | | - | |
38 | | - | r.CurrentModule = module.Name |
39 | | - | timeStart := time.Now() |
40 | | - | utils.BannerF("MODULES", fmt.Sprintf("%v - %v", module.Name, options.Module.Desc)) |
41 | | - | |
42 | | - | // create report record first because I don't want to wait for them to show up in UI until the module done |
43 | | - | r.DBNewReports(module) |
44 | | - | |
45 | | - | // pre-run |
46 | | - | utils.BlockF(module.Name, "Running prepare scripts") |
47 | | - | r.RunScripts(module.PreRun, options) |
48 | | - | |
49 | | - | // main part |
50 | | - | utils.BlockF(module.Name, "Start run main steps") |
51 | | - | err := r.RunSteps(module.Steps, options) |
52 | | - | if err != nil { |
53 | | - | utils.BadBlockF(module.Name, fmt.Sprintf("got exit call")) |
54 | | - | } |
55 | | - | |
56 | | - | // post-run |
57 | | - | utils.BlockF(module.Name, "Running conclusion scripts") |
58 | | - | r.RunScripts(module.PostRun, options) |
59 | | - | |
60 | | - | // print the reports file |
61 | | - | utils.PrintLine() |
62 | | - | printReports(options) |
63 | | - | |
64 | | - | // create report record first because we don't want to wait it show up in UI until the module done |
65 | | - | r.DBNewReports(module) |
66 | | - | |
67 | | - | // estimate time |
68 | | - | elapsedTime := time.Since(timeStart).Seconds() |
69 | | - | utils.BlockF("Elapsed Time", fmt.Sprintf("Done module %v in %v", color.HiCyanString(module.Name), color.HiMagentaString("%vs", elapsedTime))) |
70 | | - | r.RunningTime += cast.ToInt(elapsedTime) |
71 | | - | utils.PrintLine() |
72 | | - | r.DBUpdateScan() |
73 | | - | r.DBUpdateTarget() |
74 | | - | } |
75 | | - | |
76 | | - | // RunScripts run list of scripts |
77 | | - | func (r *Runner) RunScripts(scripts []string, options libs.Options) string { |
78 | | - | if options.Timeout != "" { |
79 | | - | timeout := utils.CalcTimeout(options.Timeout) |
80 | | - | utils.DebugF("Run scripts with %v seconds timeout", timeout) |
81 | | - | r.RunScriptsWithTimeOut(options.Timeout, scripts, options) |
82 | | - | return "" |
83 | | - | } |
84 | | - | |
85 | | - | for _, script := range scripts { |
86 | | - | outScript := r.RunScript(script, options) |
87 | | - | if strings.Contains(outScript, "exit") { |
88 | | - | return outScript |
89 | | - | } |
90 | | - | } |
91 | | - | return "" |
92 | | - | } |
93 | | - | |
94 | | - | // RunScriptsWithTimeOut run list of scripts with timeout |
95 | | - | func (r *Runner) RunScriptsWithTimeOut(timeoutRaw string, scripts []string, options libs.Options) string { |
96 | | - | timeout := utils.CalcTimeout(timeoutRaw) |
97 | | - | utils.DebugF("Run scripts with %v seconds timeout", timeout) |
98 | | - | |
99 | | - | c := context.Background() |
100 | | - | deadline := time.Now().Add(time.Duration(timeout) * time.Second) |
101 | | - | c, cancel := context.WithDeadline(c, deadline) |
102 | | - | defer cancel() |
103 | | - | |
104 | | - | go func() { |
105 | | - | for _, script := range scripts { |
106 | | - | outScript := r.RunScript(script, options) |
107 | | - | if strings.Contains(outScript, "exit") { |
108 | | - | return |
109 | | - | } |
110 | | - | } |
111 | | - | cancel() |
112 | | - | }() |
113 | | - | |
114 | | - | select { |
115 | | - | case <-c.Done(): |
116 | | - | utils.DebugF("Scripts done") |
117 | | - | return "" |
118 | | - | case <-time.After(time.Duration(timeout) * time.Second): |
119 | | - | utils.BadBlockF("timeout", fmt.Sprintf("Scripts got timeout after %v", color.HiMagentaString(timeoutRaw))) |
120 | | - | } |
121 | | - | return "" |
122 | | - | } |
123 | | - | |
124 | | - | // RunScript really run a script |
125 | | - | func (r *Runner) RunScript(script string, options libs.Options) string { |
126 | | - | execScript := ResolveData(script, options.Scan.ROptions) |
127 | | - | return r.ExecScript(execScript) |
128 | | - | } |
129 | | - | |
130 | | - | // RunSteps run list of steps |
131 | | - | func (r *Runner) RunSteps(steps []libs.Step, options libs.Options) error { |
132 | | - | var stepOut string |
133 | | - | for _, step := range steps { |
134 | | - | r.DoneStep += 1 |
135 | | - | if step.Timeout != "" { |
136 | | - | step.Timeout = ResolveData(step.Timeout, options.Scan.ROptions) |
137 | | - | // timeout should be: 30, 30m, 1h |
138 | | - | timeout := utils.CalcTimeout(step.Timeout) |
139 | | - | if timeout != 0 { |
140 | | - | stepOut, _ = r.RunStepWithTimeout(timeout, step, options) |
141 | | - | if strings.Contains(stepOut, "exit") { |
142 | | - | return errors.New("got exit call") |
143 | | - | } |
144 | | - | continue |
145 | | - | } |
146 | | - | } |
147 | | - | |
148 | | - | stepOut, _ = r.RunStep(step, options) |
149 | | - | if strings.Contains(stepOut, "exit") { |
150 | | - | return errors.New("got exit call") |
151 | | - | } |
152 | | - | } |
153 | | - | return nil |
154 | | - | } |
155 | | - | |
156 | | - | // RunStepWithTimeout run step with timeout |
157 | | - | func (r *Runner) RunStepWithTimeout(timeout int, step libs.Step, options libs.Options) (string, error) { |
158 | | - | utils.DebugF("Run step with %v seconds timeout", timeout) |
159 | | - | prefix := fmt.Sprintf("timeout -k 1m %vs ", timeout) |
160 | | - | |
161 | | - | // prepare the os command with prefix timeout first |
162 | | - | var preFixCommands []string |
163 | | - | for _, command := range step.Commands { |
164 | | - | preFixCommand := command |
165 | | - | if !strings.Contains(command, "timeout") { |
166 | | - | preFixCommand = prefix + command |
167 | | - | } |
168 | | - | preFixCommands = append(preFixCommands, preFixCommand) |
169 | | - | } |
170 | | - | step.Commands = preFixCommands |
171 | | - | |
172 | | - | // override global timeout |
173 | | - | options.Timeout = step.Timeout |
174 | | - | output, _ := r.RunStep(step, options) |
175 | | - | return output, nil |
176 | | - | } |
177 | | - | |
178 | | - | // RunStep really run a step |
179 | | - | func (r *Runner) RunStep(step libs.Step, options libs.Options) (string, error) { |
180 | | - | var output string |
181 | | - | if step.Label != "" { |
182 | | - | utils.BlockF("Start-Step", color.HiCyanString(step.Label)) |
183 | | - | } |
184 | | - | |
185 | | - | // checking required file |
186 | | - | err := r.CheckRequired(step.Required, r.Opt) |
187 | | - | if err != nil { |
188 | | - | return output, errors.New("missing requirements") |
189 | | - | } |
190 | | - | |
191 | | - | // check conditions and run reverse step |
192 | | - | err = r.CheckCondition(step.Conditions, r.Opt) |
193 | | - | if err != nil { |
194 | | - | if len(step.RCommands) == 0 && len(step.RScripts) == 0 { |
195 | | - | return output, errors.New("conditions not met") |
196 | | - | } |
197 | | - | |
198 | | - | // run reverse commands |
199 | | - | utils.InforF("Condition false, run the reverse commands") |
200 | | - | if len(step.RCommands) > 0 { |
201 | | - | RunCommands(step.RCommands, step.Std, r.Opt) |
202 | | - | } |
203 | | - | // run reverse scripts |
204 | | - | if len(step.RScripts) > 0 { |
205 | | - | output = r.RunScripts(step.RScripts, r.Opt) |
206 | | - | if strings.Contains(output, "exit") { |
207 | | - | return output, nil |
208 | | - | } |
209 | | - | } |
210 | | - | return output, nil |
211 | | - | } |
212 | | - | |
213 | | - | if step.Source == "" { |
214 | | - | if len(step.Commands) > 0 { |
215 | | - | RunCommands(step.Commands, step.Std, r.Opt) |
216 | | - | } |
217 | | - | if len(step.Scripts) > 0 { |
218 | | - | output = r.RunScripts(step.Scripts, r.Opt) |
219 | | - | if strings.Contains(output, "exit") { |
220 | | - | return output, nil |
221 | | - | } |
222 | | - | } |
223 | | - | |
224 | | - | // run ose here |
225 | | - | if len(step.Ose) > 0 { |
226 | | - | for _, ose := range step.Ose { |
227 | | - | r.RunOse(ose) |
228 | | - | } |
229 | | - | } |
230 | | - | |
231 | | - | // post scripts |
232 | | - | if len(step.PConditions) > 0 || len(step.PScripts) > 0 { |
233 | | - | err := r.CheckCondition(step.PConditions, r.Opt) |
234 | | - | if err == nil { |
235 | | - | if len(step.PScripts) > 0 { |
236 | | - | r.RunScripts(step.PScripts, r.Opt) |
237 | | - | } |
238 | | - | } |
239 | | - | } |
240 | | - | if step.Label != "" { |
241 | | - | utils.BlockF("Done-Step", color.HiCyanString(step.Label)) |
242 | | - | } |
243 | | - | return output, nil |
244 | | - | } |
245 | | - | |
246 | | - | ////// Start to run step but in loop mode |
247 | | - | |
248 | | - | source := ResolveData(step.Source, r.Target) |
249 | | - | utils.DebugF("Run step with Source: %v", source) |
250 | | - | data := utils.ReadingLines(source) |
251 | | - | if len(data) <= 0 { |
252 | | - | return output, errors.New("missing source") |
253 | | - | } |
254 | | - | if step.Threads != "" { |
255 | | - | step.Threads = ResolveData(step.Threads, r.Target) |
256 | | - | step.Parallel = cast.ToInt(step.Threads) |
257 | | - | } |
258 | | - | if step.Parallel == 0 { |
259 | | - | step.Parallel = 1 |
260 | | - | } |
261 | | - | |
262 | | - | // skip concurrency part |
263 | | - | if step.Parallel == 1 { |
264 | | - | for index, line := range data { |
265 | | - | r.Target["line"] = line |
266 | | - | r.Target["line_id"] = fmt.Sprintf("%v-%v", path.Base(line), index) |
267 | | - | r.Target["_id_"] = fmt.Sprintf("%v", index) |
268 | | - | r.Target["_line_"] = execution.StripName(line) |
269 | | - | if len(step.Commands) > 0 { |
270 | | - | RunCommands(step.Commands, step.Std, options) |
271 | | - | } |
272 | | - | |
273 | | - | if len(step.Ose) > 0 { |
274 | | - | for _, ose := range step.Ose { |
275 | | - | r.RunOse(ose) |
276 | | - | } |
277 | | - | } |
278 | | - | |
279 | | - | if len(step.Scripts) > 0 { |
280 | | - | r.RunScripts(step.Scripts, options) |
281 | | - | } |
282 | | - | |
283 | | - | // post scripts |
284 | | - | if len(step.PConditions) > 0 || len(step.PScripts) > 0 { |
285 | | - | err := r.CheckCondition(step.PConditions, options) |
286 | | - | if err == nil { |
287 | | - | if len(step.PScripts) > 0 { |
288 | | - | r.RunScripts(step.PScripts, options) |
289 | | - | } |
290 | | - | } |
291 | | - | } |
292 | | - | |
293 | | - | } |
294 | | - | |
295 | | - | if step.Label != "" { |
296 | | - | utils.BlockF("Done-Step", color.HiCyanString(step.Label)) |
297 | | - | } |
298 | | - | return output, nil |
299 | | - | } |
300 | | - | |
301 | | - | ///////////// |
302 | | - | // run multiple steps in concurrency mode |
303 | | - | |
304 | | - | utils.DebugF("Run step in Parallel: %v", step.Parallel) |
305 | | - | var wg sync.WaitGroup |
306 | | - | p, _ := ants.NewPoolWithFunc(step.Parallel, func(i interface{}) { |
307 | | - | r.startStepJob(i) |
308 | | - | wg.Done() |
309 | | - | }, ants.WithPreAlloc(true)) |
310 | | - | defer p.Release() |
311 | | - | |
312 | | - | var mu sync.Mutex |
313 | | - | for index, line := range data { |
314 | | - | mu.Lock() |
315 | | - | localOptions := libs.Options{} |
316 | | - | copier.Copy(&localOptions, &options) |
317 | | - | localOptions.Scan.ROptions["line"] = line |
318 | | - | localOptions.Scan.ROptions["line_id"] = fmt.Sprintf("%v-%v", path.Base(line), index) |
319 | | - | localOptions.Scan.ROptions["_id_"] = fmt.Sprintf("%v", index) |
320 | | - | localOptions.Scan.ROptions["_line_"] = execution.StripName(line) |
321 | | - | |
322 | | - | // make completely new Step |
323 | | - | localStep := libs.Step{} |
324 | | - | |
325 | | - | for _, cmd := range step.Commands { |
326 | | - | localStep.Commands = append(localStep.Commands, ResolveData(cmd, localOptions.Scan.ROptions)) |
327 | | - | } |
328 | | - | for _, cmd := range step.RCommands { |
329 | | - | localStep.RCommands = append(localStep.RCommands, ResolveData(cmd, localOptions.Scan.ROptions)) |
330 | | - | } |
331 | | - | |
332 | | - | if len(step.Ose) > 0 { |
333 | | - | for _, ose := range step.Ose { |
334 | | - | localStep.Ose = append(localStep.Ose, ResolveData(ose, localOptions.Scan.ROptions)) |
335 | | - | } |
336 | | - | } |
337 | | - | |
338 | | - | for _, script := range step.RScripts { |
339 | | - | localStep.RScripts = append(localStep.RScripts, ResolveData(script, localOptions.Scan.ROptions)) |
340 | | - | } |
341 | | - | |
342 | | - | for _, script := range step.Scripts { |
343 | | - | localStep.Scripts = append(localStep.Scripts, ResolveData(script, localOptions.Scan.ROptions)) |
344 | | - | } |
345 | | - | |
346 | | - | for _, script := range step.PConditions { |
347 | | - | localStep.PConditions = append(localStep.PConditions, ResolveData(script, localOptions.Scan.ROptions)) |
348 | | - | } |
349 | | - | for _, script := range step.PScripts { |
350 | | - | localStep.PScripts = append(localStep.PScripts, ResolveData(script, localOptions.Scan.ROptions)) |
351 | | - | } |
352 | | - | |
353 | | - | job := stepJob{ |
354 | | - | options: localOptions, |
355 | | - | step: localStep, |
356 | | - | } |
357 | | - | wg.Add(1) |
358 | | - | _ = p.Invoke(job) |
359 | | - | mu.Unlock() |
360 | | - | } |
361 | | - | |
362 | | - | wg.Wait() |
363 | | - | if step.Label != "" { |
364 | | - | utils.BlockF("Done-Step", color.HiCyanString(step.Label)) |
365 | | - | } |
366 | | - | return output, nil |
367 | | - | } |
368 | | - | |
369 | | - | type stepJob struct { |
370 | | - | options libs.Options |
371 | | - | step libs.Step |
372 | | - | } |
373 | | - | |
374 | | - | func (r *Runner) startStepJob(j interface{}) { |
375 | | - | job := j.(stepJob) |
376 | | - | localOptions := job.options |
377 | | - | localStep := job.step |
378 | | - | |
379 | | - | err := r.CheckCondition(localStep.Conditions, localOptions) |
380 | | - | |
381 | | - | if err != nil { |
382 | | - | // run reverse commands |
383 | | - | if len(localStep.RCommands) > 0 { |
384 | | - | RunCommands(localStep.RCommands, localStep.Std, localOptions) |
385 | | - | } |
386 | | - | if len(localStep.RScripts) > 0 { |
387 | | - | r.RunScripts(localStep.RScripts, localOptions) |
388 | | - | } |
389 | | - | } else { |
390 | | - | if len(localStep.Commands) > 0 { |
391 | | - | RunCommands(localStep.Commands, localStep.Std, localOptions) |
392 | | - | } |
393 | | - | } |
394 | | - | |
395 | | - | if len(localStep.Ose) > 0 { |
396 | | - | for _, ose := range localStep.Ose { |
397 | | - | r.RunOse(ose) |
398 | | - | } |
399 | | - | } |
400 | | - | |
401 | | - | if len(localStep.Scripts) > 0 { |
402 | | - | r.RunScripts(localStep.Scripts, localOptions) |
403 | | - | } |
404 | | - | |
405 | | - | // post scripts |
406 | | - | if len(localStep.PConditions) > 0 || len(localStep.PScripts) > 0 { |
407 | | - | err := r.CheckCondition(localStep.PConditions, localOptions) |
408 | | - | if err == nil { |
409 | | - | if len(localStep.PScripts) > 0 { |
410 | | - | r.RunScripts(localStep.PScripts, localOptions) |
411 | | - | } |
412 | | - | } |
413 | | - | } |
414 | | - | } |
415 | | - | |