Skip to content

Commit

Permalink
Added IAM/account_id_from_access_key.py (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyhau authored Nov 3, 2023
1 parent 82131f9 commit 508e876
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## 2023-11-03

### Added

* Added IAM/account_id_from_access_key.py


## 2023-10-25

### Changed
Expand Down
34 changes: 34 additions & 0 deletions IAM/account_id_from_access_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Retrieve account ID from the given AWS access key ID(s).
Modified from https://medium.com/@TalBeerySec/a-short-note-on-aws-key-id-f88cc4317489
"""
import base64
import binascii

import click


def get_account_id_from_access_key_id(key_id):
# Remove ID prefix and do base32 decode
x = base64.b32decode(key_id[4:])

# The account ID is 5 bytes but data is shifted by one bit
y = x[0:6]

z = int.from_bytes(y, byteorder="big", signed=False)

mask = int.from_bytes(binascii.unhexlify(b"7fffffffff80"), byteorder="big", signed=False)

return (z & mask) >> 7


@click.command()
@click.option("--key", "-k", multiple=True, required=True, help="AWS access key ID")
def main(key):
for k in key:
print("{:012d}".format(get_account_id_from_access_key_id(k)))


if __name__ == "__main__":
main()

0 comments on commit 508e876

Please sign in to comment.