Skip to content

Commit 76728a4

Browse files
author
github-actions
committed
ensure 1 second delay between requests
1 parent e5a0455 commit 76728a4

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

bbot/modules/shodan_idb.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import time
2+
13
from bbot.modules.base import BaseModule
24

35

@@ -50,11 +52,16 @@ class shodan_idb(BaseModule):
5052
# we get lots of 404s, that's normal
5153
_api_failure_abort_threshold = 9999999999
5254

53-
# there aren't any rate limits to speak of, so our outgoing queue can be pretty big
54-
_qsize = 500
55+
# since there are rate limits, we set a lower qsize
56+
# this way when our queue is full, we don't overwhelm the API
57+
_qsize = 100
5558

5659
base_url = "https://internetdb.shodan.io"
5760

61+
async def setup(self):
62+
await super().setup()
63+
self.last_request_time = 0
64+
5865
def _incoming_dedup_hash(self, event):
5966
return hash(self.get_ip(event))
6067

@@ -63,6 +70,16 @@ async def handle_event(self, event):
6370
if ip is None:
6471
return
6572
url = f"{self.base_url}/{ip}"
73+
74+
# Rate limiting: ensure at least 1 second between requests
75+
current_time = time.time()
76+
time_since_last = current_time - self.last_request_time
77+
if time_since_last < 1:
78+
await self.helpers.sleep(1 - time_since_last)
79+
80+
# Update the last request time
81+
self.last_request_time = time.time()
82+
6683
r = await self.api_request(url)
6784
if r is None:
6885
self.debug(f"No response for {event.data}")

bbot/test/test_step_2/module_tests/test_module_shodan_idb.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,26 @@ def check(self, module_test, events):
5858
if e.type == "TECHNOLOGY" and e.data["technology"] == "cpe:/a:microsoft:outlook_web_access:15.0.1367"
5959
]
6060
)
61+
62+
63+
class TestShodan_IDB_RateLimit(ModuleTestBase):
64+
config_overrides = {"dns": {"minimal": False}}
65+
module_name = "shodan_idb"
66+
67+
async def setup_before_prep(self, module_test):
68+
await module_test.mock_dns(
69+
{
70+
"blacklanternsecurity.com": {"A": ["1.2.3.4"]},
71+
"autodiscover.blacklanternsecurity.com": {"A": ["2.3.4.5"]},
72+
"mail.blacklanternsecurity.com": {"A": ["3.4.5.6"]},
73+
}
74+
)
75+
76+
module_test.httpx_mock.add_response(
77+
url="https://internetdb.shodan.io/1.2.3.4",
78+
status_code=429,
79+
json={"error": "Rate limit exceeded"},
80+
)
81+
82+
def check(self, module_test, events):
83+
pass

0 commit comments

Comments
 (0)