This project is divede in two parts: the actual S3 Ransomware Simulator, and a Cloud Detection and Response (CDR) suggested Infrastructure as Code for this type of attack.
This Python script simulates a ransomware attack on Amazon S3 buckets by encrypting all objects in a bucket using Server-Side Encryption with Customer-Provided Keys (SSE-C). It demonstrates how to programmatically encrypt objects, check permissions, and drop a ransom note.
- Bucket Listing: Lists all S3 buckets in the AWS account.
- Permission Check: Verifies if the bucket has
GetObjectandPutObjectpermissions. - Object Encryption: Encrypts all objects in a bucket using SSE-C with a randomly generated AES-256 key.
- Ransom Note: Drops a ransom note in the bucket after encryption.
- Key Management: Saves the encryption key to a file for recovery purposes.
- Python Version: Python 3.x
- AWS Credentials: Ensure AWS credentials are configured in your environment (e.g.,
~/.aws/credentialsor environment variables). - Dependencies: Install the required Python libraries:
pip install boto3 botocoreRun the script with the following options:
python3 attacker.py [OPTIONS]Options
--bucket-name <bucket_name>: Specify a single bucket to process.--all-buckets: Process all buckets in the AWS account.--encrypt-objects: Encrypt all objects in the specified bucket(s) using SSE-C.
Encrypt Objects in a Specific Bucket
python3 attacker.py --bucket-name my-bucket --encrypt-objectsEncrypt Objects in All Buckets
python attacker.py --all-buckets --encrypt-objectsCheck a Bucket, but not encrypt the object
python3 attacker.py --bucket-name my-bucketThe script provides detailed output during execution, including:
- Generated AES-256 encryption key and its MD5 hash.
- Permissions check results for each bucket.
- Number of files encrypted in each bucket.
- Confirmation of the ransom note being dropped.
- Location of the saved encryption key.
Example output:
S3 Bucket Encryption Tool with SSE-C
Processing specified bucket: my-bucket
Generated AES-256 encryption key for SSE-C: M+a4reQycj3pBBZyYs1KE9XpOcdyT7kGq1Mu+q5u+vM=
Key MD5: S2k8nSe8W9C7A2JO+Nr4mw==
Checking bucket: my-bucket
GetObject permission: Yes
PutObject permission: Yes
Processing bucket: my-bucket
Encrypting: file1.txt
Encrypting: file2.txt
Encrypted 2 files in my-bucket using SSE-C
Ransom note dropped in my-bucket.
Encryption key saved to [encryption_key.bin](http://_vscodecontentref_/3)
WARNING: This key is required to decrypt your files. Store it securely!
Encryption complete. Total files encrypted: 2
Warning: Without the encryption key, your files cannot be recovered!The encryption key is saved to a file named encryption_key.bin in the current directory. This key is required to decrypt the files. Store it securely!
The script is intended for educational purposes only. Ensure you have appropriate permissions to access and modify the S3 buckets you are working with. The script skips objects that are already encrypted or are directories.
This CloudFormation template (cdr.yaml) defines a Cloud Detection and Response (CDR) infrastructure to monitor and respond to suspicious S3 copy operations. It leverages AWS services such as CloudTrail, EventBridge, and Step Functions to detect and mitigate potential security threats.
- CloudTrail Monitoring: Tracks S3 copy operations (
CopyObjectandCompleteMultipartUpload) in a specified bucket. - EventBridge Rule: Filters CloudTrail events for S3 copy operations and triggers a Step Function.
- Step Function Workflow: Handles detected events by:
- Disabling IAM user access keys.
- Quarantining IAM roles by attaching a restrictive policy.
- Blocking
s3:PutObjectactions for compromised roles.
- Secure Logging: Stores CloudTrail logs in an encrypted S3 bucket with restricted public access.
BucketName: The name of the S3 bucket to monitor for copy events. Defaults toraphabot-no-ransomware.
- EventBridgeStepFunctionRole: Grants EventBridge permission to invoke the Step Function.
- StateMachineExecutionRole: Grants the Step Function permission to manage IAM users and roles.
CopyObjectEventHandlerStateMachine: A state machine that:- Identifies the principal type (IAM user or assumed role).
- Disables active access keys for IAM users.
- Quarantines IAM roles by attaching a restrictive policy.
- Blocks
s3:PutObjectactions for compromised roles.
S3CopyActivityTrail: Monitors S3 copy operations and logs them to an encrypted S3 bucket.
CloudTrailBucket: Stores CloudTrail logs securely with encryption and public access restrictions.CloudTrailBucketPolicy: Grants CloudTrail permission to write logs to the bucket.
S3CopyEventRule: Filters S3 copy operations from CloudTrail and triggers the Step Function.
StateMachineArn: ARN of the Step Function handling copy events.CloudTrailName: Name of the CloudTrail monitoring copy operations.EventRuleARN: ARN of the EventBridge rule.
- Deploy the CloudFormation template using the AWS Management Console, AWS CLI, or SDKs.
- Provide the
BucketNameparameter to specify the S3 bucket to monitor.
aws cloudformation deploy \
--template-file cdr.yaml \
--stack-name CDR-Infrastructure \
--parameter-overrides BucketName=my-s3-bucket- Event Detection: CloudTrail logs S3 copy operations in the specified bucket.
- Event Filtering: EventBridge filters relevant events and triggers the Step Function.
- Response: The Step Function disables IAM user keys or quarantines IAM roles involved in the operation.
This project is licensed under the MIT License. See the LICENSE file for details.