-
-
Notifications
You must be signed in to change notification settings - Fork 932
Improve robustness: canonicalize & validate targets before scan #1155
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
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
8d5f302
File named hostcheck.py is added to check whether target is buggy or…
Aarush289 aa994c5
Update app.py
Aarush289 ed7c81e
Update hostcheck.py , removed unnecessary logs
Aarush289 ccd870e
Updated hostcheck.py to use allow_single_label and return the lower-c…
Aarush289 b5da801
Docstring added
Aarush289 c1a9201
app.py updated to remove noise in exception handling
Aarush289 b66a598
Multi-threading issue is resolved
Aarush289 949c911
chore: test signed commit (SSH)
Aarush289 50374b9
chore: test signed commit (SSH) #2
Aarush289 2389890
chore: test signed commit (SSH) #3
Aarush289 859b850
Merge branch 'master' into feature/my-change
Aarush289 a83c17f
Removed unnecessary print statements
Aarush289 d852609
Indentation done
Aarush289 4de4cca
trim dot before checking the length
Aarush289 c472e71
Update hostcheck.py
Aarush289 fb43add
Logging exceptions for better debugging
Aarush289 8102395
Hostcheck.py OS-independent; add validate_before_scan
Aarush289 cd4e5ab
Hostchecker is made OS independent and validate_before_scan is adde…
Aarush289 c92c7f3
Update hostcheck.py to make it OS independent
Aarush289 8752881
Indentation done
Aarush289 bd762cd
removed the duplicate key
Aarush289 c23507e
unused parameter removed
Aarush289 7de608e
"Fix import order (ruff E402), isort formatting; run pre-commit"
Aarush289 87b773c
Per-pass timeout added
Aarush289 0ac3a96
Deadline removed
Aarush289 8565db6
Indentation done
Aarush289 ec97266
Suggested changes are done
Aarush289 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
|
|
@@ -31,3 +31,6 @@ results.* | |
| coverage.xml | ||
|
|
||
| venv | ||
| Public_sign | ||
| Public_sign.pub | ||
| cks_proxy | ||
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
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
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
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,129 @@ | ||
| # nettacker/core/hostcheck.py | ||
| from __future__ import annotations | ||
| import re | ||
| import socket | ||
| import time | ||
| import concurrent.futures | ||
| from nettacker import logger | ||
| from nettacker.core.ip import ( | ||
| get_ip_range, | ||
| generate_ip_range, | ||
| is_single_ipv4, | ||
| is_ipv4_range, | ||
| is_ipv4_cidr, | ||
| is_single_ipv6, | ||
| is_ipv6_range, | ||
| is_ipv6_cidr, | ||
| ) | ||
| log = logger.get_logger() | ||
|
|
||
| _LABEL = re.compile(r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)$") | ||
|
|
||
| _IPV4_VALID_RE = re.compile( | ||
| r'^(?:' | ||
| r'(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}' | ||
| r'(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)' | ||
| r'$' | ||
| ) | ||
|
|
||
| def is_valid_ipv4(s: str) -> bool: | ||
| return bool(_IPV4_VALID_RE.match(s)) | ||
|
|
||
| def is_ip_literal(name: str) -> bool: | ||
| """Return True if name is a valid IPv4 or IPv6 address literal.""" | ||
| if is_single_ipv4(name): | ||
| if is_valid_ipv4(name): | ||
| return True | ||
| else: | ||
| return False | ||
| else: | ||
| try: | ||
| socket.inet_pton(socket.AF_INET6, name) | ||
| return True | ||
| except OSError: | ||
| return False | ||
|
|
||
| def valid_hostname( | ||
| host: str, | ||
| allow_single_label: bool = True | ||
| ) -> bool: | ||
| """ | ||
| Validate hostname syntax per RFC 1123. | ||
| Args: | ||
| host: Hostname to validate. | ||
| allow_single_label: If True, accept single-label names (e.g., "localhost"). | ||
|
|
||
| Returns: | ||
| True if the hostname is syntactically valid. | ||
| """ | ||
| if host.endswith("."): # From RFC 1123 ,the number of characters can be 250 at max (without dots) and 253 with dots | ||
| host = host[:-1] | ||
| if len(host) > 253: | ||
| return False | ||
| parts = host.split(".") | ||
| if len(parts) < 2 and not allow_single_label: | ||
| return False | ||
| return all(_LABEL.match(p) for p in parts) | ||
|
|
||
|
|
||
| def _gai_once(name: str, use_ai_addrconfig: bool, port): | ||
| flags = getattr(socket, "AI_ADDRCONFIG", 0) if use_ai_addrconfig else 0 | ||
| return socket.getaddrinfo( | ||
| name, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, flags | ||
| ) | ||
|
|
||
| def _clean_host(s: str) -> str: | ||
| # remove surrounding quotes and whitespaces | ||
| s = s.strip().strip('"').strip("'") | ||
| s = s.strip() # again, after quote strip | ||
| # drop trailing commas that often sneak in from CSV-like inputs | ||
| if s.endswith(","): | ||
| s = s[:-1].rstrip() | ||
| # collapse accidental spaces inside | ||
| return s | ||
|
|
||
| def resolve_quick( | ||
| host: str, | ||
| timeout_sec: float = 2.0, | ||
| allow_single_label: bool = True | ||
| ) -> tuple[bool, str | None]: | ||
| """ | ||
| Perform fast DNS resolution with timeout. | ||
| Args: | ||
| host: Hostname or IP literal to resolve. | ||
| timeout_sec: Maximum time to wait for resolution. | ||
| allow_single_label: If True, allow single-label hostnames (e.g., "intranet"). | ||
|
|
||
| Returns: | ||
| (True, host_name) on success, (False, None) on failure/timeout. | ||
| """ | ||
| host = _clean_host(host) | ||
| if is_single_ipv4(host) or is_single_ipv6(host): | ||
| if is_ip_literal(host): | ||
| return True, host | ||
| return False, None | ||
|
|
||
| if host.endswith("."): | ||
| host = host[:-1] | ||
|
|
||
| if not valid_hostname(host, allow_single_label=allow_single_label): | ||
| return False, None | ||
|
|
||
| def _call(use_ai_addrconfig: bool): | ||
| return _gai_once(host, use_ai_addrconfig, None) | ||
|
|
||
| for use_ai in (True, False): | ||
| try: | ||
| # Run getaddrinfo in a thread so we can enforce timeout | ||
| with concurrent.futures.ThreadPoolExecutor(max_workers=1) as ex: | ||
| fut = ex.submit(_call, use_ai) | ||
| fut.result(timeout=timeout_sec) # raises on timeout or error | ||
| return True, host.lower() | ||
| except concurrent.futures.TimeoutError: | ||
| continue | ||
| except (OSError, socket.gaierror): | ||
| # DNS resolution failed for this candidate, try next | ||
| continue | ||
| return False, None | ||
|
|
||
|
|
||
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
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.