From 595e26db41fd0dfbb9164ca0e834bb9610007abc Mon Sep 17 00:00:00 2001 From: kidokidofire <52121973+kidokidofire@users.noreply.github.com> Date: Wed, 24 Aug 2022 17:39:45 +0900 Subject: [PATCH] Enable to get EC2 instance ID by IMDSv2. (#1520) Co-authored-by: kido3160 --- scanner/base.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/scanner/base.go b/scanner/base.go index cffd5e48..ba86b77b 100644 --- a/scanner/base.go +++ b/scanner/base.go @@ -404,6 +404,21 @@ func (l *base) detectRunningOnAws() (ok bool, instanceID string, err error) { return true, id, nil } + cmd = "curl -X PUT --max-time 1 --noproxy 169.254.169.254 -H \"X-aws-ec2-metadata-token-ttl-seconds: 300\" http://169.254.169.254/latest/api/token" + r = l.exec(cmd, noSudo) + if r.isSuccess() { + token := strings.TrimSpace(r.Stdout) + cmd = fmt.Sprintf("curl -H \"X-aws-ec2-metadata-token: %s\" --max-time 1 --noproxy 169.254.169.254 http://169.254.169.254/latest/meta-data/instance-id", token) + r = l.exec(cmd, noSudo) + if r.isSuccess() { + id := strings.TrimSpace(r.Stdout) + if !l.isAwsInstanceID(id) { + return false, "", nil + } + return true, id, nil + } + } + switch r.ExitStatus { case 28, 7: // Not running on AWS