1 | /* Vuls - Vulnerability Scanner |
||
2 | Copyright (C) 2016 Future Corporation , Japan. |
||
3 | |||
4 | This program is free software: you can redistribute it and/or modify |
||
5 | it under the terms of the GNU General Public License as published by |
||
6 | the Free Software Foundation, either version 3 of the License, or |
||
7 | (at your option) any later version. |
||
8 | |||
9 | This program is distributed in the hope that it will be useful, |
||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
12 | GNU General Public License for more details. |
||
13 | |||
14 | You should have received a copy of the GNU General Public License |
||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | package commands |
||
19 | |||
20 | import ( |
||
21 | "context" |
||
22 | "flag" |
||
23 | "fmt" |
||
24 | "os" |
||
25 | "strings" |
||
26 | "text/template" |
||
27 | |||
28 | "github.com/google/subcommands" |
||
29 | |||
30 | ps "github.com/kotakanbe/go-pingscanner" |
||
31 | "github.com/sirupsen/logrus" |
||
32 | ) |
||
33 | |||
34 | // DiscoverCmd is Subcommand of host discovery mode |
||
35 | type DiscoverCmd struct { |
||
36 | } |
||
37 | |||
38 | // Name return subcommand name |
||
39 | func (*DiscoverCmd) Name() string { return "discover" } |
||
40 | |||
41 | // Synopsis return synopsis |
||
42 | func (*DiscoverCmd) Synopsis() string { return "Host discovery in the CIDR" } |
||
43 | |||
44 | // Usage return usage |
||
45 | func (*DiscoverCmd) Usage() string { |
||
46 | return `discover: |
||
47 | discover 192.168.0.0/24 |
||
48 | |||
49 | ` |
||
50 | } |
||
51 | |||
52 | // SetFlags set flag |
||
53 | func (p *DiscoverCmd) SetFlags(f *flag.FlagSet) { |
||
54 | } |
||
55 | |||
56 | // Execute execute |
||
57 | func (p *DiscoverCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { |
||
58 | // validate |
||
59 | if len(f.Args()) == 0 { |
||
60 | logrus.Errorf("Usage: " + p.Usage()) |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
61 | return subcommands.ExitUsageError |
||
62 | } |
||
63 | |||
64 | for _, cidr := range f.Args() { |
||
65 | scanner := ps.PingScanner{ |
||
66 | CIDR: cidr, |
||
67 | PingOptions: []string{ |
||
68 | "-c1", |
||
69 | }, |
||
70 | NumOfConcurrency: 100, |
||
71 | } |
||
72 | hosts, err := scanner.Scan() |
||
73 | |||
74 | if err != nil { |
||
75 | logrus.Errorf("Host Discovery failed. err: %s", err) |
||
76 | return subcommands.ExitFailure |
||
77 | } |
||
78 | |||
79 | if len(hosts) < 1 { |
||
80 | logrus.Errorf("Active hosts not found in %s", cidr) |
||
81 | return subcommands.ExitSuccess |
||
82 | } else if err := printConfigToml(hosts); err != nil { |
||
83 | logrus.Errorf("Failed to parse template. err: %s", err) |
||
84 | return subcommands.ExitFailure |
||
85 | } |
||
86 | } |
||
87 | return subcommands.ExitSuccess |
||
88 | } |
||
89 | |||
90 | // Output the template of config.toml |
||
91 | func printConfigToml(ips []string) (err error) { |
||
92 | const tomlTemplate = ` |
||
93 | |||
94 | # https://vuls.io/docs/en/usage-settings.html |
||
95 | [cveDict] |
||
96 | type = "sqlite3" |
||
97 | sqlite3Path = "/path/to/cve.sqlite3" |
||
98 | #url = "" |
||
99 | |||
100 | [ovalDict] |
||
101 | type = "sqlite3" |
||
102 | sqlite3Path = "/path/to/oval.sqlite3" |
||
103 | #url = "" |
||
104 | |||
105 | [gost] |
||
106 | type = "sqlite3" |
||
107 | sqlite3Path = "/path/to/gost.sqlite3" |
||
108 | #url = "" |
||
109 | |||
110 | [exploit] |
||
111 | type = "sqlite3" |
||
112 | sqlite3Path = "/path/to/go-exploitdb.sqlite3" |
||
113 | #url = "" |
||
114 | |||
115 | # https://vuls.io/docs/en/usage-settings.html#slack-section |
||
116 | #[slack] |
||
117 | #hookURL = "https://hooks.slack.com/services/abc123/defghijklmnopqrstuvwxyz" |
||
118 | ##legacyToken = "xoxp-11111111111-222222222222-3333333333" |
||
119 | #channel = "#channel-name" |
||
120 | ##channel = "${servername}" |
||
121 | #iconEmoji = ":ghost:" |
||
122 | #authUser = "username" |
||
123 | #notifyUsers = ["@username"] |
||
124 | |||
125 | # https://vuls.io/docs/en/usage-settings.html#email-section |
||
126 | #[email] |
||
127 | #smtpAddr = "smtp.example.com" |
||
128 | #smtpPort = "587" |
||
129 | #user = "username" |
||
130 | #password = "password" |
||
131 | #from = "[email protected]" |
||
132 | #to = ["[email protected]"] |
||
133 | #cc = ["[email protected]"] |
||
134 | #subjectPrefix = "[vuls]" |
||
135 | |||
136 | # https://vuls.io/docs/en/usage-settings.html#http-section |
||
137 | #[http] |
||
138 | #url = "http://localhost:11234" |
||
139 | |||
140 | # https://vuls.io/docs/en/usage-settings.html#syslog-section |
||
141 | #[syslog] |
||
142 | #protocol = "tcp" |
||
143 | #host = "localhost" |
||
144 | #port = "514" |
||
145 | #tag = "vuls" |
||
146 | #facility = "local0" |
||
147 | #severity = "alert" |
||
148 | #verbose = false |
||
149 | |||
150 | # https://vuls.io/docs/en/usage-report.html#example-put-results-in-s3-bucket |
||
151 | #[aws] |
||
152 | #profile = "default" |
||
153 | #region = "ap-northeast-1" |
||
154 | #s3Bucket = "vuls" |
||
155 | #s3ResultsDir = "/path/to/result" |
||
156 | #s3ServerSideEncryption = "AES256" |
||
157 | |||
158 | # https://vuls.io/docs/en/usage-report.html#example-put-results-in-azure-blob-storage<Paste> |
||
159 | #[azure] |
||
160 | #accountName = "default" |
||
161 | #accountKey = "xxxxxxxxxxxxxx" |
||
162 | #containerName = "vuls" |
||
163 | |||
164 | # https://vuls.io/docs/en/usage-settings.html#stride-section |
||
165 | #[stride] |
||
166 | #hookURL = "xxxxxxxxxxxxxxx" |
||
167 | #authToken = "xxxxxxxxxxxxxx" |
||
168 | |||
169 | # https://vuls.io/docs/en/usage-settings.html#hipchat-section |
||
170 | #[hipchat] |
||
171 | #room = "vuls" |
||
172 | #authToken = "xxxxxxxxxxxxxx" |
||
173 | |||
174 | # https://vuls.io/docs/en/usage-settings.html#chatwork-section |
||
175 | #[chatwork] |
||
176 | #room = "xxxxxxxxxxx" |
||
177 | #apiToken = "xxxxxxxxxxxxxxxxxx" |
||
178 | |||
179 | # https://vuls.io/docs/en/usage-settings.html#telegram-section |
||
180 | #[telegram] |
||
181 | #chatID = "xxxxxxxxxxx" |
||
182 | #token = "xxxxxxxxxxxxxxxxxx" |
||
183 | |||
184 | # https://vuls.io/docs/en/usage-settings.html#default-section |
||
185 | [default] |
||
186 | #port = "22" |
||
187 | #user = "username" |
||
188 | #keyPath = "/home/username/.ssh/id_rsa" |
||
189 | #scanMode = ["fast", "fast-root", "deep", "offline"] |
||
190 | #cpeNames = [ |
||
191 | # "cpe:/a:rubyonrails:ruby_on_rails:4.2.1", |
||
192 | #] |
||
193 | #owaspDCXMLPath = "/tmp/dependency-check-report.xml" |
||
194 | #ignoreCves = ["CVE-2014-6271"] |
||
195 | #containerType = "docker" #or "lxd" or "lxc" default: docker |
||
196 | #containersIncluded = ["${running}"] |
||
197 | #containersExcluded = ["container_name_a"] |
||
198 | |||
199 | # https://vuls.io/docs/en/usage-settings.html#servers-section |
||
200 | [servers] |
||
201 | {{- $names:= .Names}} |
||
202 | {{range $i, $ip := .IPs}} |
||
203 | [servers.{{index $names $i}}] |
||
204 | host = "{{$ip}}" |
||
205 | #port = "22" |
||
206 | #user = "root" |
||
207 | #keyPath = "/home/username/.ssh/id_rsa" |
||
208 | #scanMode = ["fast", "fast-root", "deep", "offline"] |
||
209 | #type = "pseudo" |
||
210 | #memo = "DB Server" |
||
211 | #cpeNames = [ "cpe:/a:rubyonrails:ruby_on_rails:4.2.1" ] |
||
212 | #owaspDCXMLPath = "/path/to/dependency-check-report.xml" |
||
213 | #ignoreCves = ["CVE-2014-0160"] |
||
214 | #containerType = "docker" #or "lxd" or "lxc" default: docker |
||
215 | #containersIncluded = ["${running}"] |
||
216 | #containersExcluded = ["container_name_a"] |
||
217 | |||
218 | #[servers.{{index $names $i}}.containers.container_name_a] |
||
219 | #cpeNames = [ "cpe:/a:rubyonrails:ruby_on_rails:4.2.1" ] |
||
220 | #owaspDCXMLPath = "/path/to/dependency-check-report.xml" |
||
221 | #ignoreCves = ["CVE-2014-0160"] |
||
222 | |||
223 | #[servers.{{index $names $i}}.githubs."owner/repo"] |
||
224 | #token = "yourToken" |
||
225 | |||
226 | #[servers.{{index $names $i}}.wordpress] |
||
227 | #cmdPath = "/usr/local/bin/wp" |
||
228 | #osUser = "wordpress" |
||
229 | #docRoot = "/path/to/DocumentRoot/" |
||
230 | #wpVulnDBToken = "xxxxTokenxxxx" |
||
231 | #ignoreInactive = true |
||
232 | |||
233 | #[servers.{{index $names $i}}.optional] |
||
234 | #key = "value1" |
||
235 | |||
236 | {{end}} |
||
237 | |||
238 | ` |
||
239 | var tpl *template.Template |
||
240 | if tpl, err = template.New("template").Parse(tomlTemplate); err != nil { |
||
241 | return |
||
242 | } |
||
243 | |||
244 | type activeHosts struct { |
||
245 | IPs []string |
||
246 | Names []string |
||
247 | } |
||
248 | |||
249 | a := activeHosts{IPs: ips} |
||
250 | names := []string{} |
||
251 | for _, ip := range ips { |
||
252 | // TOML section header must not contain "." |
||
253 | name := strings.Replace(ip, ".", "-", -1) |
||
254 | names = append(names, name) |
||
255 | } |
||
256 | a.Names = names |
||
257 | |||
258 | fmt.Println("# Create config.toml using below and then ./vuls -config=/path/to/config.toml") |
||
259 | if err = tpl.Execute(os.Stdout, a); err != nil { |
||
260 | return |
||
261 | } |
||
262 | return |
||
263 | } |
||
264 |