Script de Análisis de Tráfico de Red
Este script está diseñado para analizar el tráfico de red capturado en un archivo .pcap utilizando la biblioteca pyshark. Los resultados del análisis se exportan luego a un informe PDF utilizando la biblioteca reportlab.
- Análisis de Paquetes: El script analiza cada paquete en el archivo
.pcapy extrae información relevante como las direcciones IP de origen y destino, los puertos TCP/UDP y las consultas DNS. - Generación de Informe PDF: Los resultados del análisis se guardan en un informe PDF llamado
network_analysis_report.pdfubicado en el directoriodocs. - Interfaz de Línea de Comandos: Los usuarios pueden especificar el archivo
.pcapa analizar a través de la línea de comandos. Si no se especifica ningún archivo, el script utiliza por defectodata/network_capture.pcap.
Antes de ejecutar el script, asegúrate de tener instaladas las siguientes dependencias:
- Python 3.x
- Biblioteca
pyshark - Biblioteca
reportlab
Puedes instalar las bibliotecas requeridas utilizando pip:
pip install pyshark reportlabAsegúrate de que la estructura de directorios sea la siguiente:
Packet capture/
├── docs/
├── scripts/
│ ├── analyze_traffic.py
│ └── data/
│ └── network_capture.pcap
Para ejecutar el script, navega al directorio scripts y ejecuta:
python analyze_traffic.pySi deseas especificar un archivo .pcap diferente, puedes hacerlo pasando el nombre de archivo como argumento:
python analyze_traffic.py another_capture.pcapEsto buscará another_capture.pcap en el directorio data y generará un informe PDF llamado network_analysis_report.pdf en el directorio docs.
El script emitirá lo siguiente:
- Un mensaje que indica el archivo que se está analizando.
- Un mensaje que indica la finalización del análisis y el número de paquetes analizados.
- Un mensaje que indica la ubicación del informe PDF generado.
Analizando archivo: data/network_capture.pcap
Análisis de red completado. Se analizaron 500 paquetes. Informe guardado como docs/network_analysis_report.pdfEsta función analiza un solo paquete y extrae información relevante como:
- Número de paquete y capa más alta.
- Direcciones IP de origen y destino.
- Puertos TCP/UDP de origen y destino.
- Paquetes TCP SYN.
- Paquetes UDP grandes.
- Consultas DNS.
La información extraída se escribe luego en el informe PDF.
Esta función coordina el proceso de análisis:
- Abre el archivo
.pcapespecificado utilizandopyshark.FileCapture. - Inicializa el informe PDF y establece la fuente.
- Itera sobre cada paquete en el archivo de captura, llamando a
analyze_packetpara cada paquete. - Guarda el informe PDF en el directorio
docs.
El script admite argumentos de línea de comandos para especificar el archivo .pcap a analizar. Si no se especifica ningún archivo, se utiliza por defecto data/network_capture.pcap.
Si experimentas problemas con dependencias faltantes, asegúrate de que tanto pyshark como reportlab estén instalados. Puedes instalarlos utilizando pip como se describe en la sección de Requisitos Previos.
¡Siéntete libre de contribuir a este proyecto enviando pull requests o informando problemas! ¡Tus contribuciones son bienvenidas!
- La biblioteca
pysharkpara el análisis de paquetes de red. - La biblioteca
reportlabpara la generación de PDF.
This script is designed to analyze network traffic captured in a .pcap file using the pyshark library. The analysis results are then exported to a PDF report using the reportlab library.
- Packet Analysis: The script analyzes each packet in the
.pcapfile and extracts relevant information such as source and destination IP addresses, TCP/UDP ports, and DNS queries. - PDF Report Generation: The analysis results are saved in a PDF report named
network_analysis_report.pdflocated in thedocsdirectory. - Command-Line Interface: Users can specify the
.pcapfile to analyze via the command line. If no file is specified, the script defaults todata/network_capture.pcap.
Before running the script, ensure you have the following dependencies installed:
- Python 3.x
pysharklibraryreportlablibrary
You can install the required libraries using pip:
pip install pyshark reportlabEnsure your directory structure looks like this:
Packet capture/
├── docs/
├── scripts/
│ ├── analyze_traffic.py
│ └── data/
│ └── network_capture.pcap
To run the script, navigate to the scripts directory and execute:
python analyze_traffic.pyIf you want to specify a different .pcap file, you can do so by passing the filename as an argument:
python analyze_traffic.py another_capture.pcapThis will look for another_capture.pcap in the data directory and generate a PDF report named network_analysis_report.pdf in the docs directory.
The script will output the following:
- A message indicating the file being analyzed.
- A message indicating the completion of the analysis and the number of packets analyzed.
- A message indicating the location of the generated PDF report.
Analyzing file: data/network_capture.pcap
Network analysis completed. Analyzed 500 packets. Report saved as docs/network_analysis_report.pdfThis function analyzes a single packet and extracts relevant information such as:
- Packet number and highest layer.
- Source and destination IP addresses.
- TCP/UDP source and destination ports.
- TCP SYN packets.
- Large UDP packets.
- DNS queries.
The extracted information is then written to the PDF report.
This function orchestrates the analysis process:
- It opens the specified
.pcapfile usingpyshark.FileCapture. - It initializes the PDF report and sets the font.
- It iterates over each packet in the capture file, calling
analyze_packetfor each packet. - It saves the PDF report to the
docsdirectory.
The script supports command-line arguments to specify the .pcap file to analyze. If no file is specified, it defaults to data/network_capture.pcap.
If you encounter issues with missing dependencies, ensure that both pyshark and reportlab are installed. You can install them using pip as described in the Prerequisites section.
Feel free to contribute to this project by submitting pull requests or reporting issues. Your contributions are welcome!
- The
pysharklibrary for network packet analysis. - The
reportlablibrary for PDF generation.