'Fetch ID from txt file or dictionary

Guys I have been working on this for 2 weeks without any progress. I am trying to fetch the "_id", "name", "ancestor index", "ancestor ID", and "Group index" from a text file for each index.

below is the content of the file:

{'_index': '.siem-signals-default-000001', '_type': '_doc', '_id': 'bc15aa137633f96651a888e40732d0bf52d488b3f0f6e51e22e2face26058e9b', '_score': None, '_source': {'signal': {'rule': {'query': 'event.type : "info" and event.category : "process" and powershell.file.script_block_text : "Start-Service BluetoothUserService_550a4"', 'name': 'Start Bluetooth Service Alert'}, 'ancestors': [{'index': '.ds-winlogbeat-8.0.0-2022.02.17-000001', 'id': 'r3SmTX8BwcmlMA-NS8VJ', 'type': 'event'}]}}, 'sort': [0]}

{'_index': '.siem-signals-default-000001', '_type': '_doc', '_id': 'db0b57ee7d3e179e03cbd4d3b1ea3e7256921a2c1baa29cbd37c8656c037b6d8', '_score': None, '_source': {'signal': {'rule': {'query': 'sequence by host.id with maxspan = 1m\n[ network where event.type: ("connection", "protocol") and network.protocol: "HTTP" and\nserver.port == "5985" and network.direction in ("ingress")  and query: ("POST /wsman") ]\n[process where event.type in ("start", "process_started", "info", "end") and powershell.command.name: "Get-Service"]', 'name': 'GET SERVICES ALERT!!'}, 'ancestors': [{'index': '.ds-packetbeat-8.0.0-2022.02.17-000001', 'id': 'O3SpTX8BwcmlMA-NIMfH', 'type': 'event'}], 'group': {'index': 0, 'id': 'fa2c20c10d2b4e6f188255cd8cdfe3dd86f72fcd12cebbdc6d7371e32a83eb3e'}}}, 'sort': [1]}
 
{'_index': '.siem-signals-default-000001', '_type': '_doc', '_id': '402172c03a4a3997858ecf1da0ae8c23a226c264c8d183fd55f831e8aca0fe44', '_score': None, '_source': {'signal': {'rule': {'query': 'sequence by host.id with maxspan = 1m\n[ network where event.type: ("connection", "protocol") and network.protocol: "HTTP" and\nserver.port == "5985" and network.direction in ("ingress")  and query: ("POST /wsman") ]\n[process where event.type in ("start", "process_started", "info", "end") and powershell.command.name: "Get-Service"]', 'name': 'GET SERVICES ALERT!!'}, 'ancestors': [{'index': '.ds-winlogbeat-8.0.0-2022.02.17-000001', 'id': 'PHSpTX8BwcmlMA-NIccG', 'type': 'event'}], 'group': {'index': 1, 'id': 'fa2c20c10d2b4e6f188255cd8cdfe3dd86f72fcd12cebbdc6d7371e32a83eb3e'}}}, 'sort': [2]}

I tried the following code

text = open('/Users/Maani/Desktop/Doc1.txt').read()
pattern = re.compile('([a-z]+[1-9]?[:]?)+\': \'[a-z0-9]')
headings = []
for match in pattern.finditer(text):
s = match.start()
e = match.end()
headings.append(text[s:e]) 
print(headings)

this is the output:

["id': 'b", "query': 'e", "id': 'r", "type': 'e", "id': 'd", "query': 's", "type': 'e", "id': 'f", "id': '4", "query': 's", "type': 'e", "id': 'f", "id': 'f", "query': 's", "type': 'e", "id': 'd", "type': 's", "type': 'e", "id': '4", "type': 's", "id': 'f", "id': '2", "query': 'e", "id': 'l", "type': 'e", "id': '8", "query': 's", "type': 'e", "id': '1", "type': 's", "type': 'e", "id': '6", "type': 's", "id': '8", "id': 'b", "query': 's", "type': 'e", "id': 'c", "type': 's", "type': 'e", "id': 'c", "type': 's", "id': 'b", "id': '2", "query': 'e", "type': 'e", "id': '1", "query': 's", "type': 'e", "id': '8", "id': '6", "query': 's", "type': 'e", "id': '8", "id': 'c", "query': 's", "type': 'e", "id': 'b", "id': 'c", "query': 's", "type': 'e", "id': 'b", "id': 'e", "query': 's", "id': 'u", "type': 'e", "id': '8", "id': 'd", "query': 's", "id': 'u", "type': 'e", "id': '8", "id': '8", "query': 's", "name': 't", "id': 'o", "type': 'e", "id': 'b", "id': '4", "query': 's", "name': 't", "id': 't", "type': 'e", "id': 'b", "id': '3", "query': 's", "name': 't", "id': '7", "type': 'e", "id': '4", "id': '8", "query': 's", "name': 't", "id': '9", "type': 'e", "id': '4", "id': 'a", "query': 's", "type': 'e", "id': '9", "id': '1", "query': 's", "type': 'e", "id': '9"]

I tried also the split and find functions without any positive results.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source