Skip to content

Commit

Permalink
Fixed bugs associated with JSON file parsing and a couple other small…
Browse files Browse the repository at this point in the history
… fixes.
  • Loading branch information
JCantu248 committed Jun 23, 2023
1 parent 857603d commit 9cadcad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def get_version(version_file):


setup(
name="example",
name="rpt_phish_report",
# Versions should comply with PEP440
version=get_version("src/example/_version.py"),
description="Example Python library",
version=get_version("src/_version.py"),
description="RPT Phish Report library",
long_description=readme(),
long_description_content_type="text/markdown",
# Landing page for CISA's cybersecurity mission
Expand Down Expand Up @@ -108,5 +108,5 @@ def get_version(version_file):
]
},
# Conveniently allows one to run the CLI tool as `example`
entry_points={"console_scripts": ["example = example.example:main"]},
entry_points={"console_scripts": ["rpt_phish_report = rpt_phish_report.phish_report_generator:main"]},
)
3 changes: 3 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""The rpt_phish_report library."""
from ._version import __version__ # noqa: F401
from .phish_report_generator import main, generate_reports

__all__ = ["main", "generate_reports"]
LOGGING_FILE = "rpt_phish_report_logging.log"
64 changes: 37 additions & 27 deletions src/phish_report_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""cisagov/rpt-phish-report: A tool for creating phishing reports to support RPT.
Usage:
rpt-phish-report REPORT_DATE OUTPUT_DIRECTORY [--log-level=LEVEL]
phish_report_generator.py JSON_FILE_PATH REPORT_DATE OUTPUT_DIRECTORY [--log-level=LEVEL]
Options:
-h --help Show this message.
JSON_FILE_PATH Path to the JSON file to act as a data source.
Expand All @@ -27,9 +29,10 @@

# cisagov Libraries

from ._version import __version__
from _version import __version__

LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(logging.StreamHandler())
LOGGING_FILE = "phish_report_generator.log"

def get_json_file(phish_result_json):
Expand All @@ -45,46 +48,47 @@ def get_json_file(phish_result_json):

def parse_json(data):
"""Parse JSON object for values to report."""
report_data = {}
report_data = []
try:
if data:
for payload in data['payloads']:
if payload["border_protection"] is 'N':
border_protection = "Not blocked"
elif payload["border_protection"] is 'B':
border_protection = "Blocked"
report_data_row = {}
report_data_row["payload_description"] = payload["payload_description"]
report_data_row["c2_protocol"] = payload["c2_protocol "]

if payload["border_protection"] == 'N':
report_data_row["border_protection"] = "Not blocked"
elif payload["border_protection"] == 'B':
report_data_row["border_protection"] = "Blocked"
else:
raise ValueError("border_protection value must be either B or N")

if payload["host_protection"] is 'N':
host_protection = "Not blocked"
elif payload["host_protection"] is 'B':
host_protection = "Blocked"
if payload["host_protection"] == 'N':
report_data_row["host_protection"] = "Not blocked"
elif payload["host_protection"] == 'B':
report_data_row["host_protection"] = "Blocked"
else:
raise ValueError("host_protection value must be either B or N")

report_data.append({
"payload_description": payload["payload_description"],
"c2_protocol": payload["c2_protocol"],
"border_protection": border_protection,
"host_protection": host_protection
})
return report_data
except Exception as e:
LOGGER.error("Error parsing JSON: %s", str(e))

report_data.append(report_data_row)
except Exception as e:
LOGGER.exception(str(e))
return report_data



def generate_reports(datestring, output_directory, json_file_path):
"""Process steps for generating report data."""
data = get_json_file(json_file_path)
report_data = parse_json(data)
if data:
report_data = parse_json(data)

return True


def main():
"""Generate PDF reports."""
args: Dict[str, str] = docopt.docopt(__doc__, version=__version__)

args: Dict[str, str] = docopt.docopt(__doc__)
# Validate and convert arguments as needed
schema: Schema = Schema(
{
Expand Down Expand Up @@ -125,14 +129,20 @@ def main():
os.mkdir(validated_args["OUTPUT_DIRECTORY"])

# Generate reports
generated_reports = generate_reports(
success = generate_reports(
validated_args["REPORT_DATE"],
validated_args["OUTPUT_DIRECTORY"],
validated_args["JSON_FILE_PATH"],

)

LOGGER.info("%s reports generated", generated_reports)
if success:
LOGGER.info("RPT Phish Report written to %s%s",
validated_args["OUTPUT_DIRECTORY"],
LOGGING_FILE)

# Stop logging and clean up
logging.shutdown()
logging.shutdown()

if __name__ == '__main__':
main()

0 comments on commit 9cadcad

Please sign in to comment.