-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
215 lines (198 loc) · 4.55 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
package main
import (
"flag"
"fmt"
"log"
"net/url"
"os"
"strings"
"github.com/lair-framework/api-server/client"
"github.com/lair-framework/go-lair"
"github.com/tealeg/xlsx"
)
const (
version = "0.1.0"
tool = "drone-xlsx"
usage = `
Usage:
drone-issues-xlsx <id> <filename>
export LAIR_ID=<id>; drone-issues-xlsx <filename>
Options:
-v show version and exit
-h show usage and exit
-k allow insecure SSL connections
`
)
func getcomment(hosts *[]lair.Host, title, ip string, port int) string {
for _, host := range *hosts {
if host.IPv4 == ip {
for _, service := range host.Services {
if service.Port == port {
for _, note := range service.Notes {
if strings.Contains(note.Title, title) {
return note.Content
}
}
}
}
}
}
return ""
}
func gethostname(ip string, hosts *[]lair.Host) string {
for _, host := range *hosts {
if host.IPv4 == ip {
return strings.Join(host.Hostnames, "\n")
}
}
return ""
}
func getos(ip string, hosts *[]lair.Host) string {
for _, host := range *hosts {
if host.IPv4 == ip {
if host.OS.Weight == 100 {
return host.OS.Fingerprint
}
}
}
return ""
}
func writesheet(project *lair.Project, outfile string) {
header := []string{
"#",
"Title",
"CVSS",
"Rating",
"Description",
"Evidence",
"Solution",
"CVEs",
"References",
"Operating System",
"Host",
"Hostname(s)",
"Port",
"Service Note",
"Issue Note(s)",
}
var file *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var cell *xlsx.Cell
file = xlsx.NewFile()
sheet, err := file.AddSheet(project.Name)
if err != nil {
log.Printf(err.Error())
}
row = sheet.AddRow()
for _, h := range header {
cell = row.AddCell()
cell.Value = h
}
for count, issue := range project.Issues {
var issuenote string
for _, note := range issue.Notes {
issuenote += note.Title
issuenote += note.Content + "\n"
}
for _, host := range issue.Hosts {
var refs []string
for _, ref := range issue.References {
refs = append(refs, ref.Link)
}
row = sheet.AddRow()
cell = row.AddCell()
cell.SetInt(count + 1)
cell = row.AddCell()
cell.Value = issue.Title
cell = row.AddCell()
cell.SetFloat(issue.CVSS)
cell = row.AddCell()
cell.Value = issue.Rating
cell = row.AddCell()
cell.Value = issue.Description
cell = row.AddCell()
cell.Value = issue.Evidence
cell = row.AddCell()
cell.Value = issue.Solution
cell = row.AddCell()
cell.Value = strings.Join(issue.CVEs, "\n")
cell = row.AddCell()
cell.Value = strings.Join(refs, "\n")
cell = row.AddCell()
cell.Value = getos(host.IPv4, &project.Hosts)
cell = row.AddCell()
cell.Value = host.IPv4
cell = row.AddCell()
cell.Value = gethostname(host.IPv4, &project.Hosts)
cell = row.AddCell()
cell.SetInt(host.Port)
cell = row.AddCell()
cell.Value = getcomment(&project.Hosts, issue.Title, host.IPv4, host.Port)
cell = row.AddCell()
cell.Value = issuenote
}
}
err = file.Save(outfile)
if err != nil {
log.Fatal("Fatal: Unable to write file")
}
}
func main() {
showVersion := flag.Bool("v", false, "")
insecureSSL := flag.Bool("k", false, "")
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())
}
project, err := c.ExportProject(lairPID)
if err != nil {
log.Fatalf("Fatal: Unable to export project. Error %s", err.Error())
}
writesheet(&project, filename)
log.Println("Success: Operation completed successfully")
}