-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(hackertarget): support Hackertarget API key via api-keys.yml (fixes #2122) #2142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raghavendrakari
wants to merge
3
commits into
laramies:master
Choose a base branch
from
raghavendrakari:feat/hackertarget-apikey
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,50 @@ | ||
| import requests | ||
| from theHarvester.discovery import hackertarget as ht_mod | ||
|
|
||
| class DummyResp: | ||
| def __init__(self, text, status_code=200): | ||
| self.text = text | ||
| self.status_code = status_code | ||
| def raise_for_status(self): | ||
| if self.status_code != 200: | ||
| raise requests.HTTPError() | ||
|
|
||
| def test_append_apikey_to_url(): | ||
| base = "https://api.hackertarget.com/hostsearch/?q=example.com" | ||
| out = ht_mod._append_apikey_to_url(base, "MYKEY") | ||
| assert "apikey=MYKEY" in out | ||
|
|
||
| def test_do_search_with_apikey(monkeypatch): | ||
| # make _get_hackertarget_key return a known key | ||
| monkeypatch.setattr(ht_mod, "_get_hackertarget_key", lambda: "TESTKEY") | ||
|
|
||
| # monkeypatch AsyncFetcher.fetch_all to capture requested URLs | ||
| async def fake_fetch_all(urls, headers=None, proxy=False): | ||
| # ensure apikey present in each URL | ||
| assert all(("apikey=TESTKEY" in u or "apikey=TESTKEY" in (u.split("?", 1)[1] if "?" in u else "")) for u in urls) | ||
| return ["1.2.3.4,host.example.com\n", "No PTR records found\n"] | ||
|
|
||
| monkeypatch.setattr(ht_mod.AsyncFetcher, "fetch_all", fake_fetch_all) | ||
|
|
||
| s = ht_mod.SearchHackerTarget("example.com") | ||
|
|
||
| # run the coroutine | ||
| import asyncio | ||
| asyncio.get_event_loop().run_until_complete(s.do_search()) | ||
|
|
||
| # after do_search, total_results should include our fake response (commas replaced by colons) | ||
| assert "1.2.3.4:host.example.com" in s.total_results | ||
|
|
||
| def test_do_search_without_apikey(monkeypatch): | ||
| monkeypatch.setattr(ht_mod, "_get_hackertarget_key", lambda: None) | ||
|
|
||
| async def fake_fetch_all(urls, headers=None, proxy=False): | ||
| assert all("apikey=" not in u for u in urls) | ||
| return ["1.2.3.4,host.example.com\n"] | ||
|
|
||
| monkeypatch.setattr(ht_mod.AsyncFetcher, "fetch_all", fake_fetch_all) | ||
|
|
||
| s = ht_mod.SearchHackerTarget("example.com") | ||
| import asyncio | ||
| asyncio.get_event_loop().run_until_complete(s.do_search()) | ||
| assert "1.2.3.4:host.example.com" in s.total_results | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.