Skip to content

Commit

Permalink
Merge pull request #16 from InQuest/issue/15-yara-cidr-to-regex
Browse files Browse the repository at this point in the history
Add YARA CIDR to Regex functionality
  • Loading branch information
pedramamini committed Jul 24, 2023
2 parents 4245c08 + 42de1e6 commit 8c3d4f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Usage:
inquestlabs [options] yara hexcase <instring>
inquestlabs [options] yara uint <instring> [--offset=<offset>] [--hex]
inquestlabs [options] yara widere <regex> [(--big-endian|--little-endian)]
inquestlabs [options] yara cidr <ipv4>
inquestlabs [options] lookup ip <ioc>
inquestlabs [options] lookup domain <ioc>
inquestlabs [options] report <ioc>
Expand Down
31 changes: 28 additions & 3 deletions inquestlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
inquestlabs [options] yara hexcase <instring>
inquestlabs [options] yara uint <instring> [--offset=<offset>] [--hex]
inquestlabs [options] yara widere <regex> [(--big-endian|--little-endian)]
inquestlabs [options] yara cidr <ipv4>
inquestlabs [options] lookup ip <ioc>
inquestlabs [options] lookup domain <ioc>
inquestlabs [options] report <ioc>
Expand Down Expand Up @@ -83,7 +84,7 @@

# extract version from installed package metadata
__application_name__ = "inquestlabs"
__version__ = "1.2.3"
__version__ = "1.2.4"
# __version__ = version(__application_name__)
__full_version__ = f"{__application_name__} {__version__}"

Expand Down Expand Up @@ -144,7 +145,7 @@ def __init__ (self, api_key=None, config=None, proxies=None, base_url=None, retr
self.api_key = api_key
self.base_url = base_url
self.config_file = config
self.retries = retries
self.retries = retries
self.proxies = proxies
self.verify_ssl = verify_ssl
self.verbosity = verbose
Expand Down Expand Up @@ -214,7 +215,7 @@ def __init__ (self, api_key=None, config=None, proxies=None, base_url=None, retr
self.__VERBOSE("api_key_source=%s" % self.api_key_source, INFO)

####################################################################################################################
def API (self, api, data=None, path=None, method="GET", raw=False):
def API (self, api, data=None, path=None, method="GET", raw=False, params=None):
"""
Internal API wrapper.
Expand All @@ -228,6 +229,8 @@ def API (self, api, data=None, path=None, method="GET", raw=False):
:param method: API method, one of "GET" or "POST".
:type raw: bool
:param raw: Default behavior is to expect JSON encoded content, raise this flag to expect raw data.
:type method: str
:param method: Set a parameter for the request.
:rtype: dict | str
:return: Response dictionary or string if 'raw' flag is raised.
Expand Down Expand Up @@ -258,6 +261,7 @@ def API (self, api, data=None, path=None, method="GET", raw=False):
"headers" : headers,
"proxies" : self.proxies,
"verify" : self.verify_ssl,
"params" : params
}

# make attempts to dance with the API endpoint, use a jittered exponential back-off delay.
Expand Down Expand Up @@ -1243,6 +1247,23 @@ def yara_uint (self, magic, offset=0, is_hex=False):

return self.API("/yara/trigger", dict(trigger=magic, offset=offset, is_hex=is_hex))

####################################################################################################################
def cidr_to_regex (self, data):
"""
Produce a regular expression from a IPv4 CIDR notation in a form suitable for usage as a YARA string.
:type regex: str
:param regex: Regular expression to convert.
:rtype: str
:return: Regex string suitable for YARA.
"""

# dance with the API and return results.
return self.API("/yara/cidr2regex", params={
"cidr": data
})

########################################################################################################################
########################################################################################################################
########################################################################################################################
Expand Down Expand Up @@ -1414,6 +1435,10 @@ def main ():
elif args['widere']:
print(labs.yara_widere(args['<regex>'], endian))

# inquestlabs [options] yara cidr <ipv4>
elif args['cidr']:
print(labs.cidr_to_regex(args['<ipv4>']))

# huh?
else:
raise inquestlabs_exception("yara argument parsing fail.")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "inquestlabs"
version = "1.2.3"
version = "1.2.4"
license = {file = "LICENSE"}
authors = [
{ name="InQuest", email="labs@inquest.net" },
Expand Down

0 comments on commit 8c3d4f6

Please sign in to comment.