æè¡ä¸èª²ã®ææã§ãã2019å¹´11æãAmazon EC2ã®Instance Metadata Service v2(IMDSv2)ãçºè¡¨ããã¾ããã
ã»ãã¥ãªãã£å¼·åã®ããã®ã¢ããã§ãããã©ããã¦ãã©ã®ããã«ãã¦ã»ãã¥ãªãã£å¼·åã«ãªãã®ãããã³ã¨ããªãæ¹ãããããããã¾ããã
å½æ稿ã§ã¯ä¸è¨ã®å ¬å¼ããã°ãæ訳ãã¦ãIMDSv2ãã©ã®ããã«ã»ãã¥ãªãã£å¼·åã«ã¤ãªããã®ãç°¡åã«è§£èª¬ãããã¨æãã¾ãã Add defense in depth against open firewalls, reverse proxies, and SSRF vulnerabilities with enhancements to the EC2 Instance Metadata Service
åºæ¬çã«ä¸è¨ããã°ã®æ訳ï¼æç²ãã¦è¨³ãããã®ï¼ã§ãããä¸é¨è£è¶³ããã¦ãã¾ãã
1. ãã¤ã³ã
- ããã©ã«ãã§ã¯IMDSv1ã¨IMDSv2ã®ä¸¡æ¹ã使ããç¶æ
- v1ãç¡å¹åãã¾ãã¯v1ã¨v2ã®ä¸¡æ¹ãç¡å¹åãããã¨ãã§ãã
- IMDSv2ã§ã¯ã¡ã¿ãã¼ã¿ã¸ã®ã¢ã¯ã»ã¹ã®åã«ã»ãã·ã§ã³ãã¼ã¯ã³ãåå¾ããå¿ è¦ããã
- ã»ãã·ã§ã³ãã¼ã¯ã³ã®åå¾ã¯ã¡ã¿ãã¼ã¿ã¸ã®HTTP PUTãªã¯ã¨ã¹ãã§è¡ã
- ããã«ãã以ä¸ã®æ»æã®ãªã¹ã¯ãä¸ãããã
- è¨å®ã«ç©´ã®ããWAFçµç±ã§ã®ãã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
- è¨å®ã«ç©´ã®ãããªãã¼ã¹ãããã·çµç±ã§ã®ãã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
- SSRFèå¼±æ§ãçªãããã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
- è¨å®ã«ç©´ã®ããL3ãã¡ã¤ã¢ã¦ã©ã¼ã«åã¯NATçµç±ã§ã®ãã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
2. v1ã¨v2ã¯ã©ãéãï¼
2-1. å¾æ¥ã¾ã§ï¼IMDSv1ï¼
ãåç¥ã®éããã¤ã³ã¹ã¿ã³ã¹ã¡ã¿ãã¼ã¿ã¯EC2å é¨ãã http://169.254.169.254/latest/meta-data/ ã«HTTPãªã¯ã¨ã¹ããéããã¨ã§èª°ã§ãåå¾ã§ãã¾ããã
$ curl http://169.254.169.254/latest/meta-data/instance-id i-0123*************
ããã¯ãIAM Roleã®Credentialãç°¡åã«åå¾ã§ãããã¨ãæå³ãã¦ãã¾ããã
$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/your-role-name { "Code" : "Success", "LastUpdated" : "2019-11-26T07:27:05Z", "Type" : "AWS-HMAC", "AccessKeyId" : "ASIA****************", "SecretAccessKey" : "CCZmï¼ä¸ç¥ï¼3w==", "Expiration" : "2019-11-26T13:37:29Z" }
2-2. ããããï¼IMDSv2ï¼
以ä¸ã®ããã«ãã¾ãã»ãã·ã§ã³ãã¼ã¯ã³ï¼æéä»ãããã¹ã¯ã¼ãã®ãããªæ±ãï¼ãåå¾ãã¾ããPUTãªã¯ã¨ã¹ãã§ãªãã¨ãã¼ã¯ã³åå¾ã§ããªã決ã¾ãã«ãªã£ã¦ããã®ããã¢ã§ããçç±ã¯å¾è¿°ã ï¼1è¡ç®: ã»ãã·ã§ã³ãã¼ã¯ã³ãPUTãªã¯ã¨ã¹ãã§åå¾ãã¦ãTOKENå¤æ°ã«ä»£å ¥ï¼
ãã®æ¬¡ã«ãcurlã®GETãªã¯ã¨ã¹ãã§ã¡ã¿ãã¼ã¿ããªã¯ã¨ã¹ããã¾ããããããã«ã¯å ã»ã©ã®ãã¼ã¯ã³ãå«ã¾ãã¾ãã ï¼2è¡ç®: -Hãªãã·ã§ã³ã§ãããæå®ãå ã»ã©ã®ãã¼ã¯ã³ãå ¥ã£ãå¤æ°ãå ¥ãã¦ããï¼
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` curl http://169.254.169.254/latest/meta-data/instance-id -H "X-aws-ec2-metadata-token: $TOKEN"
3. IMDSv2ã¯ä½ãå¬ããï¼
以ä¸ã®çç±ãããIMDSv2ã®ã»ããã»ãã¥ã¢ã«ãªãã¾ãã
3-1. è¨å®ã«ç©´ã®ããWAFçµç±ã§ã®ãã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
WAFã®è¨å®ã誤ã£ã¦ããå ´åãæ»æè ãã¤ã³ã¹ã¿ã³ã¹ã¡ã¿ãã¼ã¿ãæªç¨ã§ãã¦ãã¾ãå¯è½æ§ãããã¾ãã â»å¼ç¨å ããã°ã§ã¯ãã®ãããªè¨å®ã誤ã£ã¦ããWAFã®ãã¨ãopen WAFsã¨è¡¨ç¾ãã¦ãã¾ãããAWSç´æ£WAFã§ãã AWS WAF ã§ã¯ãã®ãããªãã¨ã¯çºçããªãã¨ã®ãã¨ãï¼"open WAFã¨ãã¦æ¯ãèãããã«ã¯è¨å®ã§ããªã"ï¼
AWSã3rd Party製ã®WAFã調æ»ããã¨ããã§ã¯ãè¨å®ã誤ã£ã¦ããWAFã§ãPUTãªã¯ã¨ã¹ãã許å¯ããã¦ããå¯è½æ§ã¯ä½ãã¨ã®ãã¨ã
ãã®ããã¡ã¿ãã¼ã¿ã¸ã®ã¢ã¯ã»ã¹ã®å段ã¨ãã¦PUTãªã¯ã¨ã¹ãã§ã®ãã¼ã¯ã³åå¾ããã¹ãã«ãããã¨ã§ãè¨å®ãã¹ã®ããWAFãä»ãã¦ããã¤ã³ã¹ã¿ã³ã¹ã¡ã¿ãã¼ã¿ãæªç¨ãããå±éºæ§ãä¸ããããã¨ããçè«ã§ããã
3-2. è¨å®ã«ç©´ã®ãããªãã¼ã¹ãããã·çµç±ã§ã®ãã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
WAFã¨åãããä¸è¬çã«ã¯PUTãªã¯ã¨ã¹ãããã®ã¾ã¾éãã¦ãã¾ããããªãªãããã¯ãªããã¨ã®ãã¨ã§ãã
ã¾ãIMDSv2ã§ã¯X-Forwarded-Forããããå«ãã ãªã¯ã¨ã¹ããæå¦ããããã«ãªã£ã¦ãã¾ãã X-Forwarded-Forã¯ããªããããªã©ãä»ãã¦ãå ã ã®æ¥ç¶å IPã¢ãã¬ã¹ãåããããã«ãå ã®ãªã¯ã¨ã¹ãè ã®IPã¢ãã¬ã¹ãè¨é²ããããã®ãããã§ãããªããããä»ããå ´åãX-Forwarded-Forãä»ä¸ããã¦ããå¯è½æ§ãé«ãããããã®ãããªãªã¯ã¨ã¹ããæå¦ããããã«ãªã£ã¦ãã¾ãã
3-3. SSRFèå¼±æ§ãçªãããã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
SSRF(Server Side Request Forgery)ã¯ãæ»æ対象ã®ãµã¼ãã«å¯¾ãã¦ç´æ¥æ»æããã®ã§ã¯ãªããã¤ã³ã¿ã¼ãããã«å ¬éããã¦ãããããª"ã¢ã¯ã»ã¹å¯è½ãªãµã¼ã"ã®èå¼±æ§ãä»ãããã¨ã§ãæ»æ対象ã®ãµã¼ãã«ãªã¼ãããæ»æã§ãã 端çã«è¨ãã¨ãã¤ã³ã¿ã¼ãããã«å ¬éããã¦ãããµã¼ããè¸ã¿å°ã¨ãã¦ä½¿ã£ã¦éå ¬éé åã®ãµã¼ãã«æ»æãä»æããæ¹å¼ã®ç·ç§°ã§ãã
ã1.ã»ãã·ã§ã³ãã¼ã¯ã³åå¾ãã2.ãªã¯ã¨ã¹ããããã«ãã¼ã¯ã³ãå«ã¾ãã¦ãªã¯ã¨ã¹ããã¨ããå¤æ®µæ§æã«ãããã¨ããAWSã®åæã«ããã¨å¤ãã®SSRFèå¼±æ§ã«å¯¾ãã¦æå¹ã§ããã¨ã®ãã¨ã§ãã IMDSv2ã§ã¯PUTãªã¯ã¨ã¹ãã§ã»ãã·ã§ã³ãã¼ã¯ã³ãåå¾ãã¦ãããããããã«å«ã¾ããå¿ è¦ãããããã決ãæã¡ã®ãããã§ã®ãªã¯ã¨ã¹ãã§ã¯ã¡ã¿ãã¼ã¿ãåå¾ã§ãã¾ããã ãããSSRFã®èå¼±æ§ã«å¯¾ãã¦æå¹ã§ãã
3-4. è¨å®ã«ç©´ã®ããL3ãã¡ã¤ã¢ã¦ã©ã¼ã«åã¯NATçµç±ã§ã®ãã¡ã¿ãã¼ã¿ãå©ç¨ããæ»æ
IMDSv2ã§ã®ãã¼ã¯ã³åå¾æã®PUTãªã¯ã¨ã¹ãã«å¯¾ããã¬ã¹ãã³ã¹ã§ã¯ããã±ããã®TTLã1ã«è¨å®ããã¦ãã¾ãã
IPãã±ããã®TTLã¯ããã±ããããããã¯ã¼ã¯æ©å¨ãçµç±ãããã¨ã«1ãã¤æ¸è¡°ãã¦ããã0ã«ãªãã¨ãã±ããã¯ç ´æ£ããã¾ãããã±ãããã«ã¼ãã«ã¯ã¾ã£ãæã«ç¡éã«ã¼ãããªãããã®è¨å®ã§ãã ãã±ããã®TTLã¯é常ããã±ãããéåºãã主ä½ã決å®ãã¾ããIMDSv2ã®ä»çµã¿ã§ã¯ããã¼ã¯ã³ãè¿ãã¨ãã®ãã±ããã®TTLã1ã«ãã¦ããã¨ããããã§ãã
ããã®ä½ãè¯ãã®ãã¨ããã¨ãTTLã1ã®ããããã¼ã¯ã³ãå«ãã¬ã¹ãã³ã¹ã®ãã±ããã¯EC2ã¤ã³ã¹ã¿ã³ã¹ã®å¤ã«åºããªããã¨ã«ãªãã¾ãã å¥ã®æ©å¨ã«ãã±ãããé£ã¶ã¨ãTTLã0ã«ãªããã±ãããå»æ£ãããããã§ããã ããã«ãããä½ããã®è¨å®ãã¹ãèå¼±æ§ãå©ç¨ãã¦æ»æè ãEC2ã¤ã³ã¹ã¿ã³ã¹ã®å¤ããã»ãã·ã§ã³ãã¼ã¯ã³ãå¾ããã¨ãé²ããã¨ãã§ãã¾ãã
4. IMDSv1ããIMDSv2ã¸ã®ç§»è¡
ããã©ã«ãã§ã¯IMDSv1ã¨IMDSv2ã®ä¸¡æ¹ã使ããç¶æ ã§ãã IMDSv2ã ã使ããç¶æ ã«ãããã¨ããIMDSv1ã¨IMDSv2ã®ä¸¡æ¹ãç¡å¹åãããã¨ãã§ãã¾ãã
ä¸è¨ããã¥ã¡ã³ãã«ããã¨ãAWS CLIãSDKãå©ç¨ãããã¨ã§ãæ¢åã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã¿ãã¼ã¿ã®æå¹å/ç¡å¹åè¨å®ãå¤æ´ã§ãã¾ãã ã¾ãã¤ã³ã¹ã¿ã³ã¹ãã¼ã³ãæã«ãæå®å¯è½ã§ãã Configuring the Instance Metadata Options
aws ec2 modify-instance-metadata-options --instance-id i-1234************* --http-tokens required
ã¾ããCloudWatchã®MetadataNoTokenã¡ããªã¯ã¹ã§ãã»ãã·ã§ã³ãã¼ã¯ã³ç¡ãã®ã¡ã¿ãã¼ã¿å¼ã³åºããããªãã¡IMDSv1ã§ã®å¼ã³åºããä½åè¡ãããããã¦ã©ãããããã¨ãã§ãã¾ãã ãã®å¤ã確èªãããã¨ã§ããã®EC2ã¤ã³ã¹ã¿ã³ã¹ã§IMDSv1ãç¡å¹åããã¿ã¤ãã³ã°ãè¨ããã¨ãå¯è½ã§ãã
ããã«ãIAM Roleã«ããAPIå¼ã³åºãã®éã®Credentialã®IAM context keyã¨ãã¦ec2:RoleDeliveryã追å ããã¾ããã å¤ã¯"1.0"ã¾ãã¯"2.0"ãåãã¾ãã ããã«ãããAPIå¼ã³åºããIMDSv1ã«ãã£ã¦è¡ããããIMDSv2ã«ãã£ã¦å¼ã°ããããå¤æã§ãããããä¾ãã°S3ã®ãã±ããããªã·ã¼ã«ãec2:RoleDeliveryã2.0ãããªãã¨Denyãããã¨æ¸ãã¦ããã°ãIMDSv2ã«ãã£ã¦ã»ãã¥ã¢ã«åå¾ãããã¯ã¬ãã³ã·ã£ã«ã ãã®ã¢ã¯ã»ã¹ã«å¶éã§ãã¾ãã
ææ å馬 (è¨äºä¸è¦§)
ãµã¼ãã¼ã¯ã¼ã¯ã¹ â æ ªå¼ä¼ç¤¾G-gen å·è¡å½¹å¡CTO
2021 Japan APN Ambassadors / 2021 APN All AWS Certifications Engineers
ãã«ãAWSã¢ã«ã¦ã³ã管çéç¨ããããã¯ã¼ã¯é¢ä¿ã®AWSãµã¼ãã¹ã«é¢ããããã°è¨äºãéå»ã«å·çã
2021å¹´09æããæ ªå¼ä¼ç¤¾G-genã«åºåãGoogle Cloud(GCP)ãå°éã«ãG-genã§ãGoogle Cloud (GCP) ã®æè¡ããã°ãå·çä¸ã