Skip to content
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

QRadarEpochToTimestamp for exponential notation #1352

Merged
merged 3 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,24 @@
"starttime": [
{
"key": "first_observed",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"cybox": false
},
{
"key": "x-ibm-finding.start",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"object": "finding"
}
],
"endtime": [
{
"key": "last_observed",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"cybox": false
},
{
"key": "x-ibm-finding.end",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"object": "finding"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,24 @@
"starttime": [
{
"key": "first_observed",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"cybox": false
},
{
"key": "x-ibm-finding.start",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"object": "finding"
}
],
"endtime": [
{
"key": "last_observed",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"cybox": false
},
{
"key": "x-ibm-finding.end",
"transformer": "EpochToTimestamp",
"transformer": "QRadarEpochToTimestamp",
"object": "finding"
}
],
Expand Down
11 changes: 11 additions & 0 deletions stix_shifter_modules/qradar/stix_translation/transformers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from stix_shifter_utils.stix_translation.src.utils.transformers import ValueTransformer
from stix_shifter_utils.utils import logger
from datetime import datetime, timezone

LOGGER = logger.set_logger(__name__)

Expand Down Expand Up @@ -34,3 +35,13 @@ def transform(value):
return [{ 'name': value }]
except ValueError:
LOGGER.error("Cannot convert root key to Stix formatted windows registry key")

class QRadarEpochToTimestamp(ValueTransformer):
"""A value transformer for the 13-digit timestamps, uses float(epoch) to handle exponential notation"""

@staticmethod
def transform(epoch):
try:
return (datetime.fromtimestamp(float(epoch) / 1000, timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z')
except ValueError:
LOGGER.error("Cannot convert epoch value {} to timestamp".format(epoch))
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,25 @@ def test_unmapped_fallback(self):
assert(custom_objects['unmapped1'] == "value1")
assert(custom_objects['unmapped2'] == "value2")
assert 'unmapped3' not in custom_objects.keys()
assert 'unmapped4' not in custom_objects.keys()
assert 'unmapped4' not in custom_objects.keys()

def test_epoch_exponent_notation(self):

data = [{
"qidname": "Information Message",
"eventcount": "12912.0",
"starttime": "0.001531169112E12",
"endtime": "0.001531169254E12"
}]

result_bundle = entry_point.translate_results(json.dumps(DATA_SOURCE), json.dumps(data))
observed_data = result_bundle['objects'][1]

assert(observed_data['first_observed'] == START_TIMESTAMP)
assert(observed_data['last_observed'] == END_TIMESTAMP)

objects = observed_data['objects']
finding = TestTransform.get_first_of_type(objects.values(), 'x-ibm-finding')

assert(finding['start'] == START_TIMESTAMP)
assert(finding['end'] == END_TIMESTAMP)