-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
198 lines (185 loc) · 5.19 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"strings"
"github.com/lair-framework/api-server/client"
"github.com/lair-framework/go-lair"
"github.com/tomsteele/blacksheepwall/bsw"
)
const (
version = "2.1.1"
tool = "drone-blacksheepwall"
usage = `
Parses a blacksheepwall JSON file into a lair project.
Usage:
drone-blacksheepwall [options] <id> <filename>
export LAIR_ID=<id>; drone-blacksheepwall [options] <filename>
Options:
-v show version and exit
-h show usage and exit
-k allow insecure SSL connections
-force-hosts import all hosts into Lair, default behaviour is to only import
hostnames for hosts that already exist in a project
-force-ports disable data protection in the API server for excessive ports
-tags a comma separated list of tags to add to every host that is imported
`
)
func main() {
showVersion := flag.Bool("v", false, "")
insecureSSL := flag.Bool("k", false, "")
forcePorts := flag.Bool("force-ports", false, "")
forceHosts := flag.Bool("force-hosts", false, "")
tags := flag.String("tags", "", "")
flag.Usage = func() {
fmt.Println(usage)
}
flag.Parse()
if *showVersion {
log.Println(version)
os.Exit(0)
}
lairURL := os.Getenv("LAIR_API_SERVER")
if lairURL == "" {
log.Fatal("Fatal: Missing LAIR_API_SERVER environment variable")
}
lairPID := os.Getenv("LAIR_ID")
var filename string
switch len(flag.Args()) {
case 2:
lairPID = flag.Arg(0)
filename = flag.Arg(1)
case 1:
filename = flag.Arg(0)
default:
log.Fatal("Fatal: Missing required argument")
}
if lairPID == "" {
log.Fatal("Fatal: Missing LAIR_ID")
}
u, err := url.Parse(lairURL)
if err != nil {
log.Fatalf("Fatal: Error parsing LAIR_API_SERVER URL. Error %s", err.Error())
}
if u.User == nil {
log.Fatal("Fatal: Missing username and/or password")
}
user := u.User.Username()
pass, _ := u.User.Password()
if user == "" || pass == "" {
log.Fatal("Fatal: Missing username and/or password")
}
c, err := client.New(&client.COptions{
User: user,
Password: pass,
Host: u.Host,
Scheme: u.Scheme,
InsecureSkipVerify: *insecureSSL,
})
if err != nil {
log.Fatalf("Fatal: Error setting up client: Error %s", err.Error())
}
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Fatal: Could not open file. Error %s", err.Error())
}
hostTags := []string{}
if *tags != "" {
hostTags = strings.Split(*tags, ",")
}
tagSet := map[string]bool{}
bResults := bsw.Results{}
if err := json.Unmarshal(data, &bResults); err != nil {
log.Fatalf("Fatal: Could not parse JSON. Error %s", err.Error())
}
bNotFound := map[string]bsw.Results{}
exproject, err := c.ExportProject(lairPID)
if err != nil {
log.Fatalf("Fatal: Unable to export project. Error %s", err.Error())
}
project := &lair.Project{
ID: lairPID,
Tool: tool,
Commands: []lair.Command{lair.Command{
Tool: tool,
}},
}
for _, result := range bResults {
found := false
if !strings.Contains(result.Hostname, "*") {
for i := range exproject.Hosts {
h := exproject.Hosts[i]
if result.IP == h.IPv4 {
exproject.Hosts[i].Hostnames = append(exproject.Hosts[i].Hostnames, result.Hostname)
exproject.Hosts[i].LastModifiedBy = tool
found = true
if _, ok := tagSet[h.IPv4]; !ok {
tagSet[h.IPv4] = true
exproject.Hosts[i].Tags = append(exproject.Hosts[i].Tags, hostTags...)
}
}
}
if !found {
bNotFound[result.IP] = append(bNotFound[result.IP], result)
}
}
}
for _, h := range exproject.Hosts {
project.Hosts = append(project.Hosts, lair.Host{
IPv4: h.IPv4,
LongIPv4Addr: h.LongIPv4Addr,
IsFlagged: h.IsFlagged,
LastModifiedBy: h.LastModifiedBy,
MAC: h.MAC,
OS: h.OS,
Status: h.Status,
StatusMessage: h.StatusMessage,
Tags: hostTags,
Hostnames: h.Hostnames,
})
}
if *forceHosts {
for ip, results := range bNotFound {
hostnames := []string{}
for _, r := range results {
hostnames = append(hostnames, r.Hostname)
}
project.Hosts = append(project.Hosts, lair.Host{
IPv4: ip,
Hostnames: hostnames,
})
}
}
res, err := c.ImportProject(&client.DOptions{ForcePorts: *forcePorts}, project)
if err != nil {
log.Fatalf("Fatal: Unable to import project. Error %s", err)
}
defer res.Body.Close()
droneRes := &client.Response{}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("Fatal: Error %s", err.Error())
}
if err := json.Unmarshal(body, droneRes); err != nil {
log.Fatalf("Fatal: Could not unmarshal JSON. Error %s", err.Error())
}
if droneRes.Status == "Error" {
log.Fatalf("Fatal: Import failed. Error %s", droneRes.Message)
}
if len(bNotFound) > 0 {
if *forceHosts {
log.Println("Info: The following hosts had hostnames and were forced to import into lair")
} else {
log.Println("Info: The following hosts had hostnames but could not be imported because they either had wildcard hostnames or do not exist in lair")
}
}
for k := range bNotFound {
fmt.Println(k)
}
log.Println("Success: Operation completed successfully")
}