-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
229 lines (222 loc) · 5.75 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"strconv"
"strings"
"github.com/lair-framework/api-server/client"
"github.com/lair-framework/go-lair"
lv1 "gopkg.in/lair-framework/go-lair.v1"
)
const (
version = "1.0.1"
tool = "rawv1"
usage = `
Imports a lair v1 JSON file into a lair v2 project.
Usage:
drone-raw-v1 [option] <id> <filename>
export LAIR_ID=<id>; drone-raw-v1 [option] <filename>
Options:
-v show version and exit
-h show usage and exit
-k allow insecure SSL connections
-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, "")
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, ",")
}
l1 := lv1.Project{}
if err := json.Unmarshal(data, &l1); err != nil {
log.Fatalf("Fatal: Could not parse JSON. Error %s", err.Error())
}
l2 := lair.Project{
ID: lairPID,
CreatedAt: l1.CreationDate,
DroneLog: l1.DroneLog,
Tool: tool,
}
for _, h := range l1.Hosts {
l2Host := lair.Host{
Status: h.Status,
LongIPv4Addr: h.LongAddr,
IPv4: h.StringAddr,
MAC: h.MacAddr,
IsFlagged: h.Flag,
Hostnames: h.Hostnames,
Tags: hostTags,
LastModifiedBy: h.LastModifiedBy,
}
for _, o := range h.OS {
if o.Weight > l2Host.OS.Weight {
l2Host.OS.Weight = o.Weight
l2Host.OS.Fingerprint = o.Fingerprint
l2Host.OS.Tool = o.Tool
}
}
for _, n := range h.Notes {
l2Host.Notes = append(l2Host.Notes, lair.Note{
Title: n.Title,
Content: n.Content,
})
}
for _, p := range h.Ports {
l2Service := lair.Service{
IsFlagged: p.Flag,
Status: p.Status,
Port: p.Port,
Service: p.Service,
Protocol: p.Protocol,
LastModifiedBy: p.LastModifiedBy,
}
for _, c := range p.Credentials {
l2Credential := lair.Credential{
Hash: c.Hash,
Password: c.Password,
Username: c.Username,
Host: h.StringAddr,
Service: strconv.Itoa(p.Port),
}
l2.Credentials = append(l2.Credentials, l2Credential)
}
for _, n := range p.Notes {
l2Service.Notes = append(l2Service.Notes, lair.Note{
Title: n.Title,
Content: n.Content,
})
}
l2Host.Services = append(l2Host.Services, l2Service)
}
l2.Hosts = append(l2.Hosts, l2Host)
}
for _, n := range l1.Notes {
l2.Notes = append(l2.Notes, lair.Note{
Title: n.Title,
Content: n.Content,
})
}
for _, c := range l1.Commands {
l2.Commands = append(l2.Commands, lair.Command{
Command: c.Command,
Tool: c.Tool,
})
}
for _, v := range l1.Vulnerabilities {
l2Issue := lair.Issue{
Title: v.Title,
Status: v.Status,
IsConfirmed: v.Confirmed,
CVEs: v.Cves,
CVSS: v.Cvss,
Description: v.Description,
Evidence: v.Evidence,
Solution: v.Solution,
IsFlagged: v.Flag,
LastModifiedBy: v.LastModifiedBy,
}
for _, i := range v.IdentifiedBy {
l2Issue.IdentifiedBy = append(l2Issue.IdentifiedBy, lair.IdentifiedBy{Tool: i.Tool})
}
for _, h := range v.Hosts {
l2Issue.Hosts = append(l2Issue.Hosts, lair.IssueHost{
IPv4: h.StringAddr,
Port: h.Port,
Protocol: h.Protocol,
})
}
for _, n := range v.Notes {
l2Issue.Notes = append(l2Issue.Notes, lair.Note{
Title: n.Title,
Content: n.Content,
})
}
for _, p := range v.PluginIds {
l2Issue.PluginIDs = append(l2Issue.PluginIDs, lair.PluginID{
ID: p.Id,
Tool: p.Tool,
})
}
l2.Issues = append(l2.Issues, l2Issue)
}
res, err := c.ImportProject(&client.DOptions{ForcePorts: *forcePorts}, &l2)
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)
}
log.Println("Success: Operation completed successfully")
}