@@ -31,11 +31,12 @@ import (
31
31
type linux struct {
32
32
ServerInfo config.ServerInfo
33
33
34
- Family string
35
- Release string
34
+ Family string
35
+ Release string
36
+ Platform models.Platform
36
37
osPackages
37
- log * logrus.Entry
38
38
39
+ log * logrus.Entry
39
40
errs []error
40
41
}
41
42
@@ -56,10 +57,18 @@ func (l *linux) setDistributionInfo(fam, rel string) {
56
57
l .Release = rel
57
58
}
58
59
59
- func (l * linux ) getDistributionInfo () string {
60
+ func (l linux ) getDistributionInfo () string {
60
61
return fmt .Sprintf ("%s %s" , l .Family , l .Release )
61
62
}
62
63
64
+ func (l * linux ) setPlatform (p models.Platform ) {
65
+ l .Platform = p
66
+ }
67
+
68
+ func (l linux ) getPlatform () models.Platform {
69
+ return l .Platform
70
+ }
71
+
63
72
func (l linux ) allContainers () (containers []config.Container , err error ) {
64
73
switch l .ServerInfo .Container .Type {
65
74
case "" , "docker" :
@@ -131,6 +140,56 @@ func (l *linux) parseDockerPs(stdout string) (containers []config.Container, err
131
140
return
132
141
}
133
142
143
+ func (l * linux ) detectPlatform () error {
144
+ ok , instanceID , err := l .detectRunningOnAws ()
145
+ if err != nil {
146
+ return err
147
+ }
148
+ if ok {
149
+ l .setPlatform (models.Platform {
150
+ Name : "aws" ,
151
+ InstanceID : instanceID ,
152
+ })
153
+ return nil
154
+ }
155
+
156
+ //TODO Azure, GCP...
157
+ l .setPlatform (models.Platform {
158
+ Name : "other" ,
159
+ })
160
+ return nil
161
+ }
162
+
163
+ func (l linux ) detectRunningOnAws () (ok bool , instanceID string , err error ) {
164
+ if r := l .ssh ("type curl" , noSudo ); r .isSuccess () {
165
+ cmd := "curl --max-time 1 --retry 3 --noproxy 169.254.169.254 http://169.254.169.254/latest/meta-data/instance-id"
166
+ if r := l .ssh (cmd , noSudo ); r .isSuccess () {
167
+ id := strings .TrimSpace (r .Stdout )
168
+ return true , id , nil
169
+ } else if r .ExitStatus == 28 || r .ExitStatus == 7 {
170
+ // Not running on AWS
171
+ // 7 Failed to connect to host.
172
+ // 28 operation timeout.
173
+ return false , "" , nil
174
+ }
175
+ }
176
+
177
+ if r := l .ssh ("type wget" , noSudo ); r .isSuccess () {
178
+ cmd := "wget --tries=3 --timeout=1 --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id"
179
+ if r := l .ssh (cmd , noSudo ); r .isSuccess () {
180
+ id := strings .TrimSpace (r .Stdout )
181
+ return true , id , nil
182
+ } else if r .ExitStatus == 4 {
183
+ // Not running on AWS
184
+ // 4 Network failure
185
+ return false , "" , nil
186
+ }
187
+ }
188
+ return false , "" , fmt .Errorf (
189
+ "Failed to curl or wget to AWS instance metadata on %s. container: %s" ,
190
+ l .ServerInfo .ServerName , l .ServerInfo .Container .Name )
191
+ }
192
+
134
193
func (l * linux ) convertToModel () (models.ScanResult , error ) {
135
194
var scoredCves , unscoredCves models.CveInfos
136
195
for _ , p := range l .UnsecurePackages {
@@ -171,6 +230,7 @@ func (l *linux) convertToModel() (models.ScanResult, error) {
171
230
Family : l .Family ,
172
231
Release : l .Release ,
173
232
Container : container ,
233
+ Platform : l .Platform ,
174
234
KnownCves : scoredCves ,
175
235
UnknownCves : unscoredCves ,
176
236
}, nil
0 commit comments