-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #806 from blacklanternsecurity/s3_bucket_enum
Created module for enumerating AWS S3 Bucket files.
- Loading branch information
Showing
17 changed files
with
122 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,8 @@ url_extension_blacklist: | |
- woff | ||
- woff2 | ||
- ttf | ||
- sass | ||
- scss | ||
# audio | ||
- mp3 | ||
- m4a | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from bbot.modules.base import BaseModule | ||
import xml.etree.ElementTree as ET | ||
|
||
|
||
class bucket_file_enum(BaseModule): | ||
""" | ||
Enumerate files in a public bucket | ||
""" | ||
|
||
watched_events = ["STORAGE_BUCKET"] | ||
produced_events = ["URL_UNVERIFIED"] | ||
meta = { | ||
"description": "Works in conjunction with the filedownload module to download files from open storage buckets. Currently supported cloud providers: AWS" | ||
} | ||
flags = ["passive", "safe", "cloud-enum"] | ||
options = { | ||
"file_limit": 50, | ||
} | ||
options_desc = {"file_limit": "Limit the number of files downloaded per bucket"} | ||
scope_distance_modifier = 2 | ||
|
||
async def setup(self): | ||
self.file_limit = self.config.get("file_limit", 50) | ||
return True | ||
|
||
async def handle_event(self, event): | ||
cloud_tags = (t for t in event.tags if t.startswith("cloud-")) | ||
if any(t.endswith("-amazon") or t.endswith("-digitalocean") for t in cloud_tags): | ||
await self.handle_aws(event) | ||
|
||
async def handle_aws(self, event): | ||
url = event.data["url"] | ||
urls_emitted = 0 | ||
response = await self.helpers.request(url) | ||
status_code = getattr(response, "status_code", 0) | ||
if status_code == 200: | ||
content = response.text | ||
root = ET.fromstring(content) | ||
namespace = {"s3": "http://s3.amazonaws.com/doc/2006-03-01/"} | ||
keys = [key.text for key in root.findall(".//s3:Key", namespace)] | ||
for key in keys: | ||
bucket_file = url + "/" + key | ||
file_extension = self.helpers.get_file_extension(key) | ||
if file_extension not in self.scan.url_extension_blacklist: | ||
self.emit_event(bucket_file, "URL_UNVERIFIED", source=event, tags="filedownload") | ||
urls_emitted += 1 | ||
if urls_emitted >= self.file_limit: | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
bbot/test/test_step_2/module_tests/test_module_bucket_file_enum.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from .base import ModuleTestBase | ||
|
||
|
||
class TestBucket_File_Enum(ModuleTestBase): | ||
targets = ["http://127.0.0.1:8888"] | ||
modules_overrides = ["bucket_file_enum", "filedownload", "httpx", "excavate"] | ||
config_overrides = {"scope_report_distance": 5} | ||
|
||
open_bucket_url = "https://testbucket.s3.amazonaws.com/" | ||
open_bucket_body = """<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>testbucket</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>index.html</Key><LastModified>2023-05-22T23:04:38.000Z</LastModified><ETag>"4a2d2d114f3abf90f8bd127c1f25095a"</ETag><Size>5</Size><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>test.pdf</Key><LastModified>2022-04-30T21:13:40.000Z</LastModified><ETag>"723b0018c2f5a7ef06a34f84f6fa97e4"</ETag><Size>388901</Size><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>""" | ||
|
||
pdf_data = """%PDF-1. | ||
1 0 obj<</Pages 2 0 R>>endobj | ||
2 0 obj<</Kids[3 0 R]/Count 1>>endobj | ||
3 0 obj<</Parent 2 0 R>>endobj | ||
trailer <</Root 1 0 R>>""" | ||
|
||
async def setup_before_prep(self, module_test): | ||
module_test.httpserver.expect_request("/").respond_with_data(f'<a href="{self.open_bucket_url}"/>') | ||
module_test.httpx_mock.add_response( | ||
url=self.open_bucket_url, | ||
text=self.open_bucket_body, | ||
) | ||
module_test.httpx_mock.add_response( | ||
url=f"{self.open_bucket_url}test.pdf", | ||
text=self.pdf_data, | ||
headers={"Content-Type": "application/pdf"}, | ||
) | ||
module_test.httpx_mock.add_response( | ||
url=f"{self.open_bucket_url}test.css", | ||
text="", | ||
) | ||
|
||
def check(self, module_test, events): | ||
download_dir = module_test.scan.home / "filedownload" | ||
files = list(download_dir.glob("*.pdf")) | ||
assert any(e.type == "URL_UNVERIFIED" and e.data.endswith("test.pdf") for e in events) | ||
assert not any(e.type == "URL_UNVERIFIED" and e.data.endswith("test.css") for e in events) | ||
assert any(f.name.endswith("test.pdf") for f in files), "Failed to download PDF file from open bucket" | ||
assert not any(f.name.endswith("test.css") for f in files), "Unwanted CSS file was downloaded" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters