-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
executable file
·243 lines (211 loc) · 5.47 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/moby/moby/client"
)
const URL = "https://vulners.com/api/v3/audit/audit/"
var (
OSVersion = []string{"cat", "/etc/os-release"}
UbuntuPackages = []string{"dpkg-query", "-W", "-f=${Package} ${Version} ${Architecture}\n"}
CentOSPackages = []string{"rpm", "-qa"}
AlpinePackages = []string{"apk", "-v", "info"}
UbuntuOS = []string{"debian", "ubuntu", "kali"}
CentOS = []string{"rhel", "centos", "oraclelinux", "suse", "fedora"}
AlpineOS = []string{"alpine"}
)
// RequestBody describe JSON for request
type RequestBody struct {
Os string `json:"os"`
Version string `json:"version"`
Package []string `json:"package"`
}
// ResponseBody contains response from vulners.com
type ResponseBody struct {
Result string `json:"result"`
Data struct {
Error string `json:"error"`
ErrorCode int `json:"errorCode"`
Vulnerabilities []string `json:"vulnerabilities"`
Reasons []struct {
Package string `json:"package"`
ProvidedVersion string `json:"providedVersion"`
BulletinVersion string `json:"bulletinVersion"`
ProvidedPackage string `json:"providedPackage"`
BulletinPackage string `json:"bulletinPackage"`
Operator string `json:"operator"`
BulletinID string `json:"bulletinID"`
} `json:"reasons"`
Cvss struct {
Score float64 `json:"score"`
Vector string `json:"vector"`
} `json:"cvss"`
Cvelist []string `json:"cvelist"`
ID string `json:"id"`
} `json:"data"`
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
log.Fatal(err)
}
resp, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
log.Fatal(err)
}
for _, v := range resp {
getInfo(cli, ctx, v)
}
}
func getInfo(cli *client.Client, ctx context.Context, container types.Container) {
fmt.Println("For container with ID:", container.ID)
osver := executeCmd(cli, ctx, container.ID, OSVersion)
var pkgs []string
if checkOS(osver, UbuntuOS) {
temp := executeCmd(cli, ctx, container.ID, UbuntuPackages)
pkgs = strings.Split(temp, "\r\n")
} else if checkOS(osver, CentOS) {
temp := executeCmd(cli, ctx, container.ID, CentOSPackages)
pkgs = strings.Split(temp, "\r\n")
} else if checkOS(osver, AlpineOS) {
temp := executeCmd(cli, ctx, container.ID, AlpinePackages)
temp2 := strings.Split(temp, "\r\n")
for _, v := range temp2 {
if !strings.Contains(v, "WARNING") {
pkgs = append(pkgs, v)
}
}
} else {
log.Fatal("Can't determine type of OS or OS is not supported: ", osver)
}
name, ver := getOSNameAndVersion(osver)
fmt.Println("OS:", name+" "+ver)
body := &RequestBody{
Os: name,
Version: ver,
Package: pkgs,
}
_, err := getVulnerabilities(body)
if err != nil {
log.Fatal(err)
}
}
func checkOS(text string, options []string) bool {
var res bool
for _, v := range options {
if strings.Contains(strings.ToLower(text), v) {
res = true
break
}
}
return res
}
func getOSNameAndVersion(text string) (string, string) {
var name string
var version string
if i := strings.Index(text, "ID="); i > -1 {
name = assign(text[i+3:])
}
if i := strings.Index(text, "VERSION_ID="); i > -1 {
version = assign(text[i+11:])
}
return name, version
}
func assign(text string) string {
var res string
if string(text[0]) == "\"" {
i := strings.Index(text[1:], "\"")
res = text[1 : i+1]
} else {
i := strings.Index(text, "\r")
res = text[:i]
}
return res
}
func executeCmd(cli *client.Client, ctx context.Context, ID string, cmd []string) string {
params := types.ExecConfig{
AttachStderr: true,
AttachStdout: true,
Tty: true,
Cmd: cmd,
}
resp, err := cli.ContainerExecCreate(ctx, ID, params)
if err != nil {
log.Fatal(err)
}
hijack, err := cli.ContainerExecAttach(ctx, resp.ID, params)
if err != nil {
log.Fatal(err)
}
defer hijack.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(hijack.Reader)
return buf.String()
}
func getVulnerabilities(rb *RequestBody) ([]string, error) {
client := http.Client{
Timeout: 30 * time.Second,
}
data, err := json.Marshal(rb)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, URL, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
body := &ResponseBody{}
err = json.Unmarshal(data, body)
if err != nil {
return nil, err
}
return extractVulnerabilitiesFromResponse(body), nil
}
func extractVulnerabilitiesFromResponse(body *ResponseBody) []string {
var result []string
if body.Result != "OK" {
log.Println("Vulners err0r:", body.Data.Error)
} else {
if len(body.Data.Cvelist) > 0 || len(body.Data.Reasons) > 0 {
fmt.Println("Achtung! Vulnerabilities were found!")
if len(body.Data.Cvelist) > 0 {
fmt.Println("List of CVE:")
for _, v := range body.Data.Cvelist {
fmt.Println(v)
}
}
if len(body.Data.Reasons) > 0 {
fmt.Println("List of Bulletin ID:")
for _, v := range body.Data.Reasons {
fmt.Println(v.BulletinID)
}
}
} else {
fmt.Println("Container is clean, congratulations!")
}
}
return result
}