# NIDS Transformers

`nids-transformers` is a Python package used for generating tags and descriptive text for network packets. This is part of our research project utilizing transformer models in network security and Network Intrusion Detection Systems (NIDS). We have developed the PADEC (Packet Describer) module that generates tags and text for network packets using BERT and Falcon LLMs (Large Language Models).

## Installation

Install the package with pip:

```shell
pip install nids-transformers
```

## Usage

First, import the PADEC module and initialize the models and tokenizers:

```python
from nids_transformers import PADEC

padec = PADEC()
```

To generate only text, you can pass `text=False`.

### Preparing the Input

Prepare the flow information (from the Wireshark Conversations Window). Use 0 if flow information is not available:

```python
forward_packets_per_second = 0
backward_packets_per_second = 4
bytes_transferred_per_second = 5493
```

Prepare the packet data in hexadecimal (from the Wireshark Hexadecimal View, copy as Hex Stream):

```python
packet_hex = '...'  # your hexadecimal data here
```

### Generating Tags

You can generate tags for a network packet with the `GenerateTags` function:

```python
tags = padec.GenerateTags(packet_hex_stream=packet_hex,
                          forward_packets_per_second=forward_packets_per_second,
                          backward_packets_per_second=backward_packets_per_second,
                          bytes_transferred_per_second=bytes_transferred_per_second,
                          total_tags=10)
```

This will output a dictionary with the generated tags and their corresponding scores.

You can also pass `context_similarity_factor`=(default: 0), `output_words_ngram`=(default: 0), `uncased_lemmatization`=(default: True), `single_word_split`=(default: False), `output_filter_factor`=(default: 1) to control the generation of tags. Refer to [BERTSimilar](https://github.com/rdpahalavan/BERTSimilar#find-similar-words) package for more details about these parameters.

### Generating Text

You can generate a descriptive text explaining the generated tags:

```python
text = padec.GenerateText(explain_tags=True, max_new_tokens=250)
```

You can control the randomness by passing a temperature value from 0 to 1, where 0 means less random and 1 means more random (default is `temperature=0`).

You can also generate a descriptive text explaining the network packet:

```python
text = padec.GenerateText(explain_packet=True, max_new_tokens=250)
```

## Demo

### Input

The following is an example of how to prepare and use data for the PADEC module:

```python
from nids_transformers import PADEC

# Initialize PADEC
padec = PADEC()

# Flow Information (From Wireshark Conversations Window)
forward_packets_per_second = 0
backward_packets_per_second = 4
bytes_transferred_per_second = 5493

# Packet Data in Hexadecimal (From Wireshark Hexadecimal View. Copy as Hex Stream)
packet_hex = " "

# Generate 10 Tags
tags = padec.GenerateTags(packet_hex_stream=packet_hex,
                          forward_packets_per_second=forward_packets_per_second,
                          backward_packets_per_second=backward_packets_per_second,
                          bytes_transferred_per_second=bytes_transferred_per_second,
                          total_tags=10)

print(tags)

tags_text = padec.GenerateText(explain_tags=True, max_new_tokens=250)

print(tags_text)

packet_text = padec.GenerateText(explain_packet=True, max_new_tokens=250)

print(packet_text)
```

### Output

```
# tags
{
  'considered regular expected': 0.9683609588041633,  
  'malicious intent': 0.9615794189652873,  
  'typical': 0.961300843268933,  
  'reference point': 0.9590238971149821,  
  'label signifies normal network behavior': 0.9575988655384243,  
  'standard network protocols': 0.9566183076210247,  
  'baseline': 0.9542804079487445,  
  'Average': 0.9535212932036061,  
  'abnormal traffic patterns enabling': 0.9528368377318245,  
  'expected traffic patterns': 0.952349645227717
}

# tags_text
Based on the majority of the tags, the network packet appears to be a normal packet. It is considered regular and expected, with typical behavior and standard network protocols. The label signifies normal network behavior, and the packet follows expected traffic patterns. There is no indication of malicious intent or abnormal traffic patterns. Overall, it is a regular and normal packet. However, further analysis may be required to ensure its security.

# packet_text
This network packet is an IPv4 packet with a length of 1480 bytes. The packet has a Time-to-Live (TTL) value of 245, indicating that it has been forwarded through 245 routers. The packet is using the TCP protocol and has a source IP address of 13.35.93.43 and a destination IP address of 134.88.139.51. The source port is 443 and the destination port is 63834. The packet has the ACK flag set, indicating that it is acknowledging a previous packet. The payload of the packet contains various words and phrases, such as "VPqy," "qqcy," "KdTFBn," and "DmY." These words do not provide much context or meaning, but they could be part of a message or data being transmitted. Overall, there are no abnormalities in the packet, and it appears to be a normal TCP packet with a specific payload. However, further analysis would be required to determine if any security issues or anomalies are present. The packet does not exhibit any suspicious or malicious behavior. The IP version is 4, indicating that it is an IPv4 packet.
```

## Notes

It's recommended to ensure you have sufficient disk space and GPU memory when using this package.