-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUtils.go
350 lines (303 loc) · 10.4 KB
/
Utils.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
/*
Copyright 2018 TheRedSpy15
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/daviddengcn/go-colortext"
"github.com/gocolly/colly"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"golang.org/x/crypto/ssh/terminal"
)
// Util function - check if target is empty, panic if is
func checkTarget(target string) {
if target == "" { // check if target is blank
ct.Foreground(ct.Red, true)
panic("target cannot be empty when performing this task!")
}
}
// Util function - used for getting []byte of file
func readFileIntoByte(filename string) []byte {
var data []byte // specify type
file, err := os.Open(filename) // make file object
defer file.Close() // close file on function end
if err != nil {
ct.Foreground(ct.Red, true)
panic(err.Error())
} else {
data, err = ioutil.ReadAll(file) // read all
if err != nil {
ct.Foreground(ct.Red, true)
panic(err.Error())
}
}
return data
}
// Util function - securely get password from user
func getPassword() string {
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin)) // run password command, make var with result
password := string(bytePassword) // cast to string var
return password
}
// Util function - displays banner text
func printBanner() {
ct.Foreground(ct.Red, true)
println(`
__ __ _ _ _ ____
| \/ |_ _| | |_(_) / ___| ___
| |\/| | | | | | __| | | | _ / _ \
| | | | |_| | | |_| | | |_| | (_) |
|_| |_|\__,_|_|\__|_| \____|\___/`)
ct.Foreground(ct.Yellow, false)
}
// TODO: scrape list of urls from text file
// Util function - scrapes a website link
func collyAddress(target string, savePage bool, ip bool) {
if ip {
target = "http://" + target + "/"
}
c := colly.NewCollector() // make colly object
c.IgnoreRobotsTxt = true
// configuring colly/collector object
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnError(func(_ *colly.Response, err error) { // print error message on error
ct.Foreground(ct.Red, true)
log.Println("Something went wrong:", err)
ct.ResetColor()
})
c.OnResponse(func(r *colly.Response) {
fmt.Println("Visited", r.Request.URL)
println("Response:", r.StatusCode)
})
c.OnScraped(func(r *colly.Response) { // finished with site
fmt.Println("Finished", r.Request.URL)
if savePage {
err := r.Save(r.FileName()) // saving data
if err != nil {
ct.Foreground(ct.Red, true)
panic("Error saving")
} else { // saved
ct.Foreground(ct.Green, true)
println("Saved - ", r.FileName())
ct.ResetColor()
}
}
})
// actually using colly/collector object, and visiting target
c.Visit(target)
}
// TODO: finish
// TODO: document
// BUG: exit status 1
// Util function - run system audit
func runAudit() {
const script = `#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'videns'
import inspect
import pkgutil
import json
import os
try:
import urllib.request as urllib2
except ImportError:
import urllib2
import scanModules
VULNERS_LINKS = {'pkgChecker':'https://vulners.com/api/v3/audit/audit/',
'bulletin':'https://vulners.com/api/v3/search/id/'}
VULNERS_ASCII = r"""
_
__ ___ _| |_ __ ___ _ __ ___
\ \ / / | | | | '_ \ / _ \ '__/ __|
\ V /| |_| | | | | | __/ | \__ \
\_/ \__,_|_|_| |_|\___|_| |___/
"""
class scannerEngine():
def __init__(self):
self.osInstanceClasses = self.getInstanceClasses()
def getInstanceClasses(self):
self.detectors = None
members = set()
for modPath, modName, isPkg in pkgutil.iter_modules(scanModules.__path__):
#find all classed inherited from scanner.osDetect.ScannerInterface in all files
members = members.union(inspect.getmembers(__import__('%s.%s' % ('scanModules',modName), fromlist=['scanModules']),
lambda member:inspect.isclass(member)
and issubclass(member, scanModules.osDetect.ScannerInterface)
and member.__module__ == '%s.%s' % ('scanModules',modName)
and member != scanModules.osDetect.ScannerInterface))
return members
def getInstance(self,sshPrefix):
inited = [instance[1](sshPrefix) for instance in self.osInstanceClasses]
if not inited:
raise Exception("No OS Detection classes found")
osInstance = max(inited, key=lambda x:x.osDetectionWeight)
if osInstance.osDetectionWeight:
return osInstance
def sendVulnRequest(self, url, payload):
req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')
req.add_header('User-Agent', 'vulners-scanner-v0.1')
response = urllib2.urlopen(req, json.dumps(payload).encode('utf-8'))
responseData = response.read()
if isinstance(responseData, bytes):
responseData = responseData.decode('utf8')
responseData = json.loads(responseData)
return responseData
def auditSystem(self, sshPrefix, systemInfo=None):
instance = self.getInstance(sshPrefix)
installedPackages = instance.getPkg()
print("="*42)
if systemInfo:
print("Host info - %s" % systemInfo)
print("OS Name - %s, OS Version - %s" % (instance.osFamily, instance.osVersion))
print("Total found packages: %s" % len(installedPackages))
if not installedPackages:
return instance
# Get vulnerability information
payload = {'os':instance.osFamily,
'version':instance.osVersion,
'package':installedPackages}
url = VULNERS_LINKS.get('pkgChecker')
response = self.sendVulnRequest(url, payload)
resultCode = response.get("result")
if resultCode != "OK":
print("Error - %s" % response.get('data').get('error'))
else:
vulnsFound = response.get('data').get('vulnerabilities')
if not vulnsFound:
print("No vulnerabilities found")
else:
payload = {'id':vulnsFound}
allVulnsInfo = self.sendVulnRequest(VULNERS_LINKS['bulletin'], payload)
vulnInfoFound = allVulnsInfo['result'] == 'OK'
print("Vulnerable packages:")
for package in response['data']['packages']:
print(" "*4 + package)
packageVulns = []
for vulns in response['data']['packages'][package]:
if vulnInfoFound:
vulnInfo = "{id} - '{title}', cvss.score - {score}".format(id=vulns,
title=allVulnsInfo['data']['documents'][vulns]['title'],
score=allVulnsInfo['data']['documents'][vulns]['cvss']['score'])
packageVulns.append((vulnInfo,allVulnsInfo['data']['documents'][vulns]['cvss']['score']))
else:
packageVulns.append((vulns,0))
packageVulns = sorted(packageVulns, key=lambda x:x[1])
packageVulns = [" "*8 + x[0] for x in packageVulns]
print("\n".join(packageVulns))
return instance
def scan(self, checkDocker = False):
#scan host machine
hostInstance = self.auditSystem(sshPrefix=None,systemInfo="Host machine")
#scan dockers
if checkDocker:
containers = hostInstance.sshCommand("docker ps")
if containers:
containers = containers.splitlines()[1:]
dockers = [(line.split()[0], line.split()[1]) for line in containers]
for (dockerID, dockerImage) in dockers:
sshPrefix = "docker exec %s" % dockerID
self.auditSystem(sshPrefix, "docker container \"%s\"" % dockerImage)
if __name__ == "__main__":
print('\n'.join(VULNERS_ASCII.splitlines()))
scannerInstance = scannerEngine()
scannerInstance.scan(checkDocker=False)`
cmd := exec.Command("python")
r := strings.NewReader(script)
var o bytes.Buffer
cmd.Stdin = r
cmd.Stdout = &o
if err := cmd.Run(); err != nil {
ct.Foreground(ct.Red, true)
panic(err.Error())
}
fmt.Println(o.String())
}
// TODO: rewrite in my own code
// TODO: add more comments
// Util function - returns a random string
/* Original: https://stackoverflow.com/questions/22892120
/how-to-generate-a-random-string-of-a-fixed-length-in-go#31832326 */
func randomString() string {
var charLimit = 20 // string max length
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // letters to use
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var src = rand.NewSource(time.Now().UnixNano()) // create random source
b := make([]byte, charLimit)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := charLimit-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// TODO: add more info
// Util function - prints CPU info
func printCPU() {
cpuCount, _ := cpu.Counts(false)
cpuCountLogical, _ := cpu.Counts(true)
println("\n-- CPU --\n")
println("CPU Count: (logical)", cpuCountLogical)
println("CPU Count:", cpuCount)
}
// Util function - prints info about system memory
func printMemory() {
vm, err := mem.VirtualMemory()
if err != nil {
ct.Foreground(ct.Red, true)
panic(err.Error())
}
println("\n-- Memory --\n")
println("Memory Available:", vm.Available)
println("Memory Total:", vm.Total)
}
// Util function - prints info about system host
func printHost() {
info, err := host.Info()
if err != nil {
ct.Foreground(ct.Red, true)
panic(err.Error())
}
println("\n-- Host --\n")
println("Kernal Version:", info.KernelVersion)
println("Platform:", info.Platform)
println("Platform Family:", info.PlatformFamily)
println("Platform Version:", info.PlatformVersion)
println("Uptime:", info.Uptime)
println("Host Name:", info.Hostname)
println("Host ID:", info.HostID)
println("OS:", info.OS)
}