2024-01-20 07:04:50 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Elasticsearch Recon Ingestion Scripts (ERIS) - Developed by Acidvegas (https://git.acid.vegas/eris)
|
2024-02-02 05:11:18 +00:00
|
|
|
# ingest_massdns.py
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-03-06 19:33:21 +00:00
|
|
|
import logging
|
2024-01-20 07:04:50 +00:00
|
|
|
import time
|
|
|
|
|
2024-03-06 03:19:11 +00:00
|
|
|
try:
|
|
|
|
import aiofiles
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError('Missing required \'aiofiles\' library. (pip install aiofiles)')
|
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
|
2024-03-08 03:55:48 +00:00
|
|
|
default_index = 'eris-massdns'
|
2024-03-08 02:57:10 +00:00
|
|
|
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-02-02 05:11:18 +00:00
|
|
|
def construct_map() -> dict:
|
|
|
|
'''Construct the Elasticsearch index mapping for MassDNS records'''
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-03-06 20:07:52 +00:00
|
|
|
keyword_mapping = { 'type': 'text', 'fields': { 'keyword': { 'type': 'keyword', 'ignore_above': 256 } } }
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-02-02 05:11:18 +00:00
|
|
|
mapping = {
|
2024-03-06 19:33:21 +00:00
|
|
|
'mappings': {
|
2024-03-08 02:57:10 +00:00
|
|
|
'properties': {
|
|
|
|
'ip' : { 'type': 'ip' },
|
|
|
|
'record' : keyword_mapping,
|
|
|
|
'seen' : { 'type': 'date' }
|
2024-01-20 07:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-08 02:57:10 +00:00
|
|
|
}
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-02-02 05:11:18 +00:00
|
|
|
return mapping
|
2024-01-27 06:13:11 +00:00
|
|
|
|
|
|
|
|
2024-03-06 03:19:11 +00:00
|
|
|
async def process_data(file_path: str):
|
2024-01-27 06:13:11 +00:00
|
|
|
'''
|
2024-02-02 05:11:18 +00:00
|
|
|
Read and process Massdns records from the log file.
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-02-02 05:11:18 +00:00
|
|
|
:param file_path: Path to the Massdns log file
|
|
|
|
'''
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-03-08 05:07:26 +00:00
|
|
|
async with aiofiles.open(file_path) as input_file:
|
2024-03-08 02:57:10 +00:00
|
|
|
|
|
|
|
last = None
|
|
|
|
|
2024-03-06 03:19:11 +00:00
|
|
|
async for line in input_file:
|
2024-02-02 05:11:18 +00:00
|
|
|
line = line.strip()
|
2024-01-27 06:13:11 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Sentinel value to indicate the end of a process (for closing out a FIFO stream)
|
|
|
|
if line == '~eof':
|
2024-03-08 17:13:57 +00:00
|
|
|
yield last
|
|
|
|
break
|
2024-03-06 19:33:21 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Skip empty lines
|
2024-02-02 05:11:18 +00:00
|
|
|
if not line:
|
|
|
|
continue
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Split the line into its parts
|
2024-02-02 05:11:18 +00:00
|
|
|
parts = line.split()
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Ensure the line has at least 3 parts
|
2024-02-02 05:11:18 +00:00
|
|
|
if len(parts) < 3:
|
2024-03-08 02:57:10 +00:00
|
|
|
logging.warning(f'Invalid PTR record: {line}')
|
|
|
|
continue
|
2024-03-08 17:13:57 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Split the PTR record into its parts
|
2024-03-06 19:33:21 +00:00
|
|
|
name, record_type, record = parts[0].rstrip('.'), parts[1], ' '.join(parts[2:]).rstrip('.')
|
2024-01-20 07:04:50 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Do not index other records
|
2024-02-02 05:11:18 +00:00
|
|
|
if record_type != 'PTR':
|
|
|
|
continue
|
2024-01-27 06:13:11 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Do not index PTR records that do not have a record
|
|
|
|
if not record:
|
2024-03-06 19:33:21 +00:00
|
|
|
continue
|
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Let's not index the PTR record if it's the same as the in-addr.arpa domain
|
|
|
|
if record == name:
|
2024-02-02 05:11:18 +00:00
|
|
|
continue
|
2024-03-08 17:13:57 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Get the IP address from the in-addr.arpa domain
|
2024-02-02 05:11:18 +00:00
|
|
|
ip = '.'.join(name.replace('.in-addr.arpa', '').split('.')[::-1])
|
2024-03-08 02:57:10 +00:00
|
|
|
|
|
|
|
# Check if we are still processing the same IP address
|
|
|
|
if last:
|
|
|
|
if ip == last['_id']:
|
2024-03-08 17:13:57 +00:00
|
|
|
last_record = last['doc']['record']
|
2024-03-08 02:57:10 +00:00
|
|
|
if isinstance(last_record, list):
|
|
|
|
if record not in last_record:
|
2024-03-08 17:13:57 +00:00
|
|
|
last['doc']['record'].append(record)
|
2024-03-08 02:57:10 +00:00
|
|
|
else:
|
|
|
|
logging.warning(f'Duplicate PTR record: {line}')
|
|
|
|
else:
|
|
|
|
if record != last_record:
|
2024-03-08 17:13:57 +00:00
|
|
|
last['doc']['record'] = [last_record, record] # IP addresses with more than one PTR record will turn into a list
|
2024-03-08 02:57:10 +00:00
|
|
|
continue
|
|
|
|
else:
|
2024-03-08 17:13:57 +00:00
|
|
|
yield last # Return the last document and start a new one
|
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
# Cache the this document in-case we have more for the same IP address
|
|
|
|
last = {
|
|
|
|
'_op_type' : 'update',
|
|
|
|
'_id' : ip,
|
|
|
|
'_index' : default_index,
|
2024-03-08 17:13:57 +00:00
|
|
|
'doc' : {
|
2024-03-08 02:57:10 +00:00
|
|
|
'ip' : ip,
|
|
|
|
'record' : record,
|
|
|
|
'seen' : time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
|
|
|
|
},
|
|
|
|
'doc_as_upsert' : True # This will create the document if it does not exist
|
2024-02-02 05:11:18 +00:00
|
|
|
}
|
2024-01-27 06:13:11 +00:00
|
|
|
|
2024-02-02 05:11:18 +00:00
|
|
|
|
2024-03-08 03:55:48 +00:00
|
|
|
async def test(input_path: str):
|
|
|
|
'''
|
|
|
|
Test the MassDNS ingestion process
|
2024-03-08 17:13:57 +00:00
|
|
|
|
2024-03-08 03:55:48 +00:00
|
|
|
:param input_path: Path to the MassDNS log file
|
|
|
|
'''
|
|
|
|
async for document in process_data(input_path):
|
|
|
|
print(document)
|
2024-02-02 05:11:18 +00:00
|
|
|
|
2024-03-08 02:57:10 +00:00
|
|
|
|
|
|
|
|
2024-03-08 03:55:48 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import argparse
|
|
|
|
import asyncio
|
2024-03-08 02:57:10 +00:00
|
|
|
|
2024-03-08 03:55:48 +00:00
|
|
|
parser = argparse.ArgumentParser(description='MassDNS Ingestor for ERIS')
|
|
|
|
parser.add_argument('input_path', help='Path to the input file or directory')
|
|
|
|
args = parser.parse_args()
|
2024-03-08 17:13:57 +00:00
|
|
|
|
2024-03-08 04:31:30 +00:00
|
|
|
asyncio.run(test(args.input_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
Deployment:
|
2024-03-08 17:13:57 +00:00
|
|
|
sudo apt-get install build-essential gcc make
|
2024-03-08 04:31:30 +00:00
|
|
|
git clone --depth 1 https://github.com/blechschmidt/massdns.git $HOME/massdns && cd $HOME/massdns && make
|
|
|
|
curl -s https://public-dns.info/nameservers.txt | grep -v ':' > $HOME/massdns/nameservers.txt
|
2024-03-08 17:13:57 +00:00
|
|
|
python3 ./scripts/ptr.py | ./bin/massdns -r $HOME/massdns/nameservers.txt -t PTR --filter NOERROR-s 500 -o S -w $HOME/massdns/fifo.json
|
2024-03-08 04:31:30 +00:00
|
|
|
or...
|
|
|
|
while true; do python ./scripts/ptr.py | ./bin/massdns -r $HOME/massdns/nameservers.txt -t PTR --filter NOERROR -s 1000 -o S -w $HOME/massdns/fifo.json; done
|
|
|
|
|
|
|
|
Output:
|
|
|
|
0.6.229.47.in-addr.arpa. PTR 047-229-006-000.res.spectrum.com.
|
|
|
|
0.6.228.75.in-addr.arpa. PTR 0.sub-75-228-6.myvzw.com.
|
|
|
|
0.6.207.73.in-addr.arpa. PTR c-73-207-6-0.hsd1.ga.comcast.net.
|
|
|
|
|
|
|
|
Input:
|
|
|
|
{
|
|
|
|
"_id" : "47.229.6.0"
|
2024-03-08 05:07:26 +00:00
|
|
|
"_index" : "eris-massdns",
|
2024-03-08 04:31:30 +00:00
|
|
|
"_source" : {
|
|
|
|
"ip" : "47.229.6.0",
|
|
|
|
"record" : "047-229-006-000.res.spectrum.com", # This will be a list if there are more than one PTR record
|
|
|
|
"seen" : "2021-06-30T18:31:00Z"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Notes:
|
2024-03-08 05:07:26 +00:00
|
|
|
Why do some IP addresses return a CNAME from a PTR request
|
|
|
|
What is dns-servfail.net (Frequent CNAME response from PTR requests)
|
2024-03-08 17:13:57 +00:00
|
|
|
'''
|