import requests
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph
from plugins.debrief.app.utility.base_report_section import BaseReportSection
from plugins.debrief.app.debrief_svc import DebriefService
class DebriefReportSection(BaseReportSection):
def __init__(self):
super().__init__()
self.id = 'tactic-technique-table'
self.display_name = 'Tactic and Technique Table'
self.section_title = 'TACTICS AND TECHNIQUES'
self.description = ''
self.api_key = "YOUR_API_KEY"
self.base_url = "https://your-qradar-server/api"
async def generate_section_elements(self, styles, **kwargs):
flowable_list = []
if 'operations' in kwargs:
operations = kwargs.get('operations', [])
ttps = DebriefService.generate_ttps(operations)
ttp_data_with_events = await self._search_qradar_events(ttps)
flowable_list.append(self.group_elements([
Paragraph(self.section_title, styles['Heading2']),
self._generate_ttps_table(ttp_data_with_events)
]))
return flowable_list
async def _search_qradar_events(self, ttps):
headers = {
"SEC": self.api_key,
"Content-Type": "application/json",
"Accept": "application/json"
}
ttp_data_with_events = []
for key, tactic in ttps.items():
technique_arr = []
for name, tid in tactic['techniques'].items():
technique_arr.append(tid + ': ' + name)
matching_events = []
for step in tactic['steps']:
query = f"SELECT * FROM events WHERE command='{step}'"
payload = {"query_expression": query}
response = requests.post(f"{self.base_url}/ariel/searches", headers=headers, json=payload)
search_id = response.json()["search_id"]
response = requests.get(f"{self.base_url}/ariel/searches/{search_id}", headers=headers)
if response.json()["status"] == "COMPLETED":
response = requests.get(f"{self.base_url}/ariel/searches/{search_id}/results", headers=headers)
matching_events.append(response.json())
ttp_data_with_events.append({
'tactic': tactic,
'matching_events': matching_events
})
return ttp_data_with_events
def _generate_ttps_table(self, ttp_data_with_events):
ttp_data = [['Tactics', 'Techniques', 'Abilities', 'Matching Events']]
for entry in ttp_data_with_events:
tactic = entry['tactic']
matching_events = entry['matching_events']
technique_arr = []
for name, tid in tactic['techniques'].items():
technique_arr.append(tid + ': ' + name)
ttp_data.append([tactic['name'].capitalize(), technique_arr, tactic['steps'], matching_events])
return self.generate_table(ttp_data, [1 * inch, 3 * inch, 1.5 * inch, 1.5 * inch])