Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldax committed Oct 12, 2023
0 parents commit b1ea578
Show file tree
Hide file tree
Showing 8 changed files with 903 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
app.log
__pycache__
network_hierarchy.csv
config
serverchain.pem
error.log
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-2023 Pascal Weber (zoldax)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
60 changes: 60 additions & 0 deletions NHSuite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3

"""
NHSuite.py
A utility script to export, import and fetch domain from the QRadar Network Hierarchy.
Author: Pascal Weber (zoldax)
Requirements:
- qradarzoldaxlib: A library to interact with QRadar API.
- qradarzoldaxclass : A library containing my NH Class and methods
Author : Pascal Weber
"""

import csv
import re
import json
import argparse
import ipaddress
import os
import qradarzoldaxlib
from qradarzoldaxclass import QRadarNetworkHierarchy
from datetime import datetime
from typing import Union

def main():
"""Main function to handle command-line arguments and execute desired actions."""
qradar_nh = QRadarNetworkHierarchy()

parser = argparse.ArgumentParser(description="QRadar Network Hierarchy Suite by Pascal Weber (zoldax) / Abakus Sécurité")
parser.add_argument('-e', '--export-file', nargs='?', const="network_hierarchy.csv", default=None, metavar="FILENAME", help="Export network hierarchy to a CSV file. If no filename is provided, it will default to 'network_hierarchy.csv'.")
parser.add_argument('-i', '--import-file', type=str, metavar="IMPORT_FILENAME", help="Import network hierarchy from a CSV file")
parser.add_argument('--check-domain', action='store_true', help="Fetch and display domain information from QRadar")
parser.add_argument('--check-version', action='store_true', help="Retrieve and display QRadar current system information")

args = parser.parse_args()

if args.export_file:
lines_exported = qradar_nh.write_network_hierarchy_to_csv(args.export_file)
print(f"{lines_exported} lines exported successfully! (including col headers)")

elif args.import_file:
lines_imported = qradar_nh.import_csv_to_qradar(args.import_file)
if isinstance(lines_imported, int) and lines_imported > 0:
print(f"{lines_imported} lines imported successfully!")
else:
print("Data import failed.")

elif args.check_domain:
qradar_nh.check_domain()

elif args.check_version:
qradarzoldaxlib.print_qradar_version()

else:
parser.print_help()

if __name__ == "__main__":
main()
111 changes: 111 additions & 0 deletions README-ERROR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# ⚙️ QRadar Interaction Library: Error Logging

### 🖋️ Author
- **Pascal Weber (zoldax)**

## 🚫 Errors Logged in `qradarzoldaxlib`
The library, `qradarzoldaxlib.py`, offers functionalities for interaction with the QRadar API. Below is a breakdown of potential errors that may be logged:

### 1. 📑 `read_config` Function

This function reads configuration data from a JSON file.

#### Error Scenarios:

- **🚫 File Not Found**:
```
Error reading configuration file [filename] : [filename] does not exist
```
- **🔣 JSON Decoding Error**:
```
Error reading configuration file [filename] : [specific JSON decoding error message]
```
- **❗ Unexpected Errors**:
```
Unexpected error occurred while reading [filename] : [error details]
```
### 2. 🌐 `make_request` Function
This function sends an HTTP request (either GET or PUT) to a specified URL.
#### Error Scenarios:
- **❌ Unsupported HTTP Method**:
```
Unsupported HTTP method: [method]
```
- **⚠️ Request Exception or HTTP Error**:
```
Error occurred during request: [error details]
```
- **❗ Unexpected Errors**:
```
An unexpected error occurred: [error details]
```
### 3. 🆔 `get_app_id` Function
This function fetches the application ID for a given app name from QRadar.
#### Note:
If the function fails to fetch applications from the QRadar API, no specific error message is logged, but the function returns an empty string.
## 🚫 Errors Logged in `qradarzoldaxclass.py`
### 📢 Errors
- **Configuration Reading 📚**
- `config.txt not found.`
- `Failed to decode JSON from config.txt.`
- **Network Hierarchy Fetching 🌐**
- `Unexpected data format received: {hierarchy_data}`
- **CSV Import to QRadar 📤**
- 🚫 `Backup failed. Aborting the import process for safety.`
- When: The safety mode is ON, and backup of the current network hierarchy fails.
- Location: Method `import_csv_to_qradar`.
- ❌ `Invalid cidr {row['cidr']} for id {row['id']} - abort import.`
- When: The provided CIDR in the CSV is invalid.
- Location: Method `import_csv_to_qradar`.
- ❌ `Invalid group name {row['group']} for id {row['id']} - abort import -.`
- When: The provided group name in the CSV is invalid.
- Location: Method `import_csv_to_qradar`.
- ⚠️ `Invalid location format {location_val} for id {row['id']} - import continue - but value set to null.`
- When: The provided location format in the CSV is invalid.
- Location: Method `import_csv_to_qradar`.
- ⚠️ `Invalid country code {country_code_val} for id {row['id']} - import continue - but value set to null.`
- When: The provided country code in the CSV is invalid.
- Location: Method `import_csv_to_qradar`.
- 🚫 `Failed to import data from {csv_filename}`
- When: There's an error while making a request to import the data.
- Location: Method `import_csv_to_qradar`.
- ❗ `File {csv_filename} not found.`
- When: The specified CSV file for import is not found.
- Location: Method `import_csv_to_qradar`.
- ❗ `Error reading CSV file {csv_filename}.`
- When: There's an error reading the CSV file.
- Location: Method `import_csv_to_qradar`.
- 🚫 `An unexpected error occurred: {e}`
- When: Any unexpected error occurs.
- Location: Method `import_csv_to_qradar`.
- **Domain Information Checking 🌍**
- `Unexpected data format received: {domain_data}`
- **Backup of Current Network Hierarchy 💾**
- `Failed to create a backup due to the following error: {e}`
Loading

0 comments on commit b1ea578

Please sign in to comment.