-
Notifications
You must be signed in to change notification settings - Fork 59
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
Port https_availability bit → nibble #4023
Open
originalsouth
wants to merge
13
commits into
feature/nibbles
Choose a base branch
from
feature/nibbles-https-availability
base: feature/nibbles
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
Show all changes
13 commits
Select commit
Hold shift + click to select a range
89cf4f0
DEMO
originalsouth abb845d
DEMO2
originalsouth 8f13d8f
Merge branch 'feature/nibbles' into feature/nibbles-https-availability
originalsouth 6c51943
Make precommit happy
originalsouth 1439194
Merge remote-tracking branch 'origin/main' into feature/nibbles-https…
originalsouth ca29c3e
Finalize
originalsouth 9e2d961
Merge branch 'feature/nibbles' into feature/nibbles-https-availability
originalsouth 2373d0c
Close the corners
originalsouth 368bf69
Fix unit test
originalsouth d5980ee
Merge remote-tracking branch 'origin/feature/nibbles' into feature/ni…
originalsouth 681d050
Integrate feedback
originalsouth 3e6c87e
Fix nibble
originalsouth 99cc00c
Fix nibble test
originalsouth 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,19 @@ | ||
from collections.abc import Iterator | ||
|
||
from octopoes.models import OOI | ||
from octopoes.models.ooi.findings import Finding, KATFindingType | ||
from octopoes.models.ooi.network import IPAddress, IPPort | ||
from octopoes.models.ooi.web import Website | ||
|
||
|
||
def nibble(ipaddress: IPAddress, ipport80: IPPort, website: Website, port443s: int) -> Iterator[OOI]: | ||
_ = ipaddress | ||
_ = ipport80 | ||
if port443s < 1: | ||
ft = KATFindingType(id="KAT-HTTPS-NOT-AVAILABLE") | ||
yield ft | ||
yield Finding( | ||
ooi=website.reference, | ||
finding_type=ft.reference, | ||
description="HTTP port is open, but HTTPS port is not open", | ||
) |
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,66 @@ | ||
from nibbles.definitions import NibbleDefinition, NibbleParameter | ||
from octopoes.models import Reference | ||
from octopoes.models.ooi.network import IPAddress, IPPort | ||
from octopoes.models.ooi.web import Website | ||
|
||
|
||
def query(targets: list[Reference | None]) -> str: | ||
def pull(statements: list[str]) -> str: | ||
return f""" | ||
{{ | ||
:query {{ | ||
:find [(pull ?ipaddress [*]) (pull ?ipport80 [*]) (pull ?website [*]) (- (count ?ipport443) 1)] | ||
:where [ | ||
{" ".join(statements)} | ||
] | ||
}} | ||
}} | ||
""" | ||
|
||
base_query = [ | ||
""" | ||
[?website :Website/ip_service ?ip_service] | ||
[?ipservice :IPService/ip_port ?ipport80] | ||
[?ipport80 :IPPort/port 80] | ||
[?ipport80 :IPPort/address ?ipaddress] | ||
(or | ||
(and [?ipport443 :IPPort/address ?ipaddress][?ipport443 :IPPort/port 443]) | ||
[(identity nil) ?ipport443] | ||
) | ||
""" | ||
] | ||
|
||
ref_queries = [ | ||
f'[?ipaddress :IPAddress/primary_key "{str(targets[0])}"]', | ||
f'[?ipport80 :IPPort/primary_key "{str(targets[1])}"]', | ||
f'[?website :Website/primary_key "{str(targets[2])}"]', | ||
] | ||
|
||
sgn = "".join(str(int(isinstance(target, Reference))) for target in targets) | ||
if sgn == "1000": | ||
return pull(ref_queries[0:1] + base_query) | ||
elif sgn == "0100": | ||
if int(str(targets[1]).split("|")[-1]) == 80: | ||
return pull(ref_queries[1:2] + base_query) | ||
else: | ||
return pull(base_query) | ||
elif sgn == "0010": | ||
return pull(ref_queries[2:3] + base_query) | ||
elif sgn == "1110": | ||
return pull(ref_queries + base_query) | ||
else: | ||
return pull(base_query) | ||
|
||
|
||
NIBBLE = NibbleDefinition( | ||
id="https_availability", | ||
signature=[ | ||
NibbleParameter( | ||
object_type=IPAddress, parser="[*][?object_type == 'IPAddressV6' || object_type == 'IPAddressV4'][]" | ||
), | ||
NibbleParameter(object_type=IPPort, parser="[*][?object_type == 'IPPort'][]"), | ||
NibbleParameter(object_type=Website, parser="[*][?object_type == 'Website'][]"), | ||
NibbleParameter(object_type=int, parser="[*][-1][]"), | ||
], | ||
query=query, | ||
) |
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
76 changes: 76 additions & 0 deletions
76
octopoes/tests/integration/test_https_availability_nibble.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,76 @@ | ||
import os | ||
from datetime import datetime | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from nibbles.https_availability.nibble import NIBBLE as https_availability | ||
|
||
from octopoes.core.service import OctopoesService | ||
from octopoes.models.ooi.dns.zone import Hostname | ||
from octopoes.models.ooi.findings import Finding, KATFindingType | ||
from octopoes.models.ooi.network import IPAddressV4, IPPort, Network, Protocol | ||
from octopoes.models.ooi.service import IPService, Service | ||
from octopoes.models.ooi.web import HostnameHTTPURL, HTTPHeader, HTTPResource, WebScheme, Website | ||
|
||
if os.environ.get("CI") != "1": | ||
pytest.skip("Needs XTDB multinode container.", allow_module_level=True) | ||
|
||
STATIC_IP = ".".join((4 * "1 ").split()) | ||
|
||
|
||
def test_https_availability_query(xtdb_octopoes_service: OctopoesService, event_manager: Mock, valid_time: datetime): | ||
originalsouth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
xtdb_octopoes_service.nibbler.nibbles = {https_availability.id: https_availability} | ||
|
||
network = Network(name="internet") | ||
xtdb_octopoes_service.ooi_repository.save(network, valid_time) | ||
|
||
hostname = Hostname(name="example.com", network=network.reference) | ||
xtdb_octopoes_service.ooi_repository.save(hostname, valid_time) | ||
|
||
web_url = HostnameHTTPURL( | ||
network=network.reference, netloc=hostname.reference, port=443, path="/", scheme=WebScheme.HTTP | ||
) | ||
xtdb_octopoes_service.ooi_repository.save(web_url, valid_time) | ||
|
||
service = Service(name="https") | ||
xtdb_octopoes_service.ooi_repository.save(service, valid_time) | ||
|
||
ip_address = IPAddressV4(address=STATIC_IP, network=network.reference) | ||
xtdb_octopoes_service.ooi_repository.save(ip_address, valid_time) | ||
|
||
port = IPPort(port=80, address=ip_address.reference, protocol=Protocol.TCP) | ||
xtdb_octopoes_service.ooi_repository.save(port, valid_time) | ||
|
||
ip_service = IPService(ip_port=port.reference, service=service.reference) | ||
xtdb_octopoes_service.ooi_repository.save(ip_service, valid_time) | ||
|
||
website = Website(ip_service=ip_service.reference, hostname=hostname.reference) | ||
xtdb_octopoes_service.ooi_repository.save(website, valid_time) | ||
|
||
resource = HTTPResource(website=website.reference, web_url=web_url.reference) | ||
xtdb_octopoes_service.ooi_repository.save(resource, valid_time) | ||
|
||
header = HTTPHeader( | ||
resource=resource.reference, key="strict-transport-security", value="max-age=21536000; includeSubDomains" | ||
) | ||
xtdb_octopoes_service.ooi_repository.save(header, valid_time) | ||
|
||
event_manager.complete_process_events(xtdb_octopoes_service) | ||
|
||
assert xtdb_octopoes_service.ooi_repository.list_oois({Finding}, valid_time).count == 1 | ||
assert ( | ||
xtdb_octopoes_service.ooi_repository.list_oois({Finding}, valid_time).items[0].description | ||
== "HTTP port is open, but HTTPS port is not open" | ||
) | ||
assert xtdb_octopoes_service.ooi_repository.list_oois({KATFindingType}, valid_time).count == 1 | ||
originalsouth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert ( | ||
xtdb_octopoes_service.ooi_repository.list_oois({KATFindingType}, valid_time).items[0].id | ||
== "KAT-HTTPS-NOT-AVAILABLE" | ||
) | ||
|
||
port443 = IPPort(port=443, address=ip_address.reference, protocol=Protocol.TCP) | ||
xtdb_octopoes_service.ooi_repository.save(port443, valid_time) | ||
event_manager.complete_process_events(xtdb_octopoes_service) | ||
|
||
assert xtdb_octopoes_service.ooi_repository.list_oois({Finding}, valid_time).count == 0 | ||
assert xtdb_octopoes_service.ooi_repository.list_oois({KATFindingType}, valid_time).count == 0 |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I found a bug in this query while trying to modify it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right that is the whole point, 1 is really 0 as you are counting the nil