-
Notifications
You must be signed in to change notification settings - Fork 0
/
cracker.go
81 lines (73 loc) · 1.46 KB
/
cracker.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
package ncrack
import (
"bytes"
"context"
"encoding/xml"
"errors"
"os/exec"
"strings"
)
type Cracker struct {
args []string
ctx context.Context
filterFunc func(*Service) bool
}
func New(opts ...Option) *Cracker {
c := &Cracker{}
for _, opt := range opts {
opt(c)
}
return c
}
// Args method outputs arguments passed to ncrack
func (c Cracker) Args() []string {
return c.args
}
// RunBytes synchronously executes ncrack and get
func (c Cracker) runBytes() ([]byte, error) {
cmd := exec.CommandContext(c.returnCtx(), "ncrack", c.args...)
buf, errBuf := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
cmd.Stdout = buf
cmd.Stderr = errBuf
if err := cmd.Run(); err != nil {
if errBuf.Len() != 0 {
return nil, errors.New(strings.TrimSpace(errBuf.String()))
}
return nil, err
}
return buf.Bytes(), nil
}
// Run synchronously executes ncrack
func (c Cracker) Run() (res *Run, err error) {
data, err := c.runBytes()
if err != nil {
return nil, err
}
res = &Run{
rawXML: data,
}
err = xml.Unmarshal(data, res)
if err != nil {
return nil, err
}
res.Services = c.filterServices(res.Services)
return res, nil
}
func (c Cracker) returnCtx() context.Context {
if c.ctx == nil {
return context.Background()
}
return c.ctx
}
func (c Cracker) filterServices(ss []Service) []Service {
if c.filterFunc == nil {
return ss
}
nss := []Service{}
for i := range ss {
if c.filterFunc(&ss[i]) {
nss = append(nss, ss[i])
}
}
return nss
}