Skip to content

Commit

Permalink
add cli interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Flanker-shyam committed Jul 12, 2023
1 parent b80db28 commit 7f7c3c2
Show file tree
Hide file tree
Showing 13 changed files with 847 additions and 13 deletions.
337 changes: 337 additions & 0 deletions flow.csv

Large diffs are not rendered by default.

Binary file not shown.
Binary file added pcap_files/test2.pcapng
Binary file not shown.
81 changes: 81 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
altair==5.0.1
appdirs==1.4.4
asttokens==2.2.1
attrs==23.1.0
backcall==0.2.0
blinker==1.6.2
cachetools==5.3.1
certifi==2023.5.7
charset-normalizer==3.2.0
click==8.1.4
comm==0.1.3
debugpy==1.6.7
decorator==5.1.1
executing==1.2.0
gitdb==4.0.10
GitPython==3.1.31
idna==3.4
importlib-metadata==6.8.0
ipykernel==6.24.0
ipython==8.14.0
jedi==0.18.2
Jinja2==3.1.2
joblib==1.3.1
jsonschema==4.18.0
jsonschema-specifications==2023.6.1
jupyter_client==8.3.0
jupyter_core==5.3.1
lxml==4.9.3
markdown-it-py==3.0.0
MarkupSafe==2.1.3
matplotlib-inline==0.1.6
mdurl==0.1.2
nest-asyncio==1.5.6
numpy==1.25.0
packaging==23.1
pandas==2.0.3
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.5.0
platformdirs==3.8.1
prompt-toolkit==3.0.39
protobuf==4.23.4
psutil==5.9.5
ptyprocess==0.7.0
pure-eval==0.2.2
pyarrow==12.0.1
pydeck==0.8.1b0
Pygments==2.15.1
Pympler==1.0.1
pyshark==0.6
python-dateutil==2.8.2
pytz==2023.3
pytz-deprecation-shim==0.1.0.post0
pyzmq==25.1.0
referencing==0.29.1
requests==2.31.0
rich==13.4.2
rpds-py==0.8.10
scapy==2.4.3
scikit-learn==1.3.0
scipy==1.11.1
six==1.16.0
smmap==5.0.0
stack-data==0.6.2
streamlit==1.24.1
tenacity==8.2.2
termcolor==2.3.0
threadpoolctl==3.1.0
toml==0.10.2
toolz==0.12.0
tornado==6.3.2
traitlets==5.9.0
typing_extensions==4.7.1
tzdata==2023.3
tzlocal==4.3.1
urllib3==2.0.3
validators==0.20.0
watchdog==3.0.0
wcwidth==0.2.6
zipp==3.16.0
337 changes: 337 additions & 0 deletions result.csv

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/FlowDirection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""This function divide packets into forward and backward direction to understand flow pattern"""

def flow_direction(flow_dict):
flow_with_direction = dict()
Expand Down
2 changes: 2 additions & 0 deletions src/extractFlow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pandas as pd

"""This function extracts flow info from the pcap file and seperate them for futher processing"""

def extract_flow_info(pcap):
flow_dictionary = dict()
columns = ['key','TimeStamp', 'srcIp', 'dstIp','srcPort', 'dstPort']
Expand Down
10 changes: 10 additions & 0 deletions src/extractTimeStamps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"""Extract the information regarding the time features from the flow
FWD IAT max
BWD IAT max
Idle Mean time
Active Max
Active Mean
Flow Bytes/s
Fwd Avg Bulk Rate
"""

def custom_sort(packet): #a custome function to sort the packets w.r.t to their timestamp
if 'tcp' in packet:
Expand Down
4 changes: 2 additions & 2 deletions src/extract_flags.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


"""This file extracts information regarding the flags in the packets
# ACK Flag Count
# Bwd URG Flags
# PSH Flag Count
# URG Flag Count
"""

#extract all flags from the packets;

Expand Down
11 changes: 11 additions & 0 deletions src/extract_size.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@

"""This file contains functions that extract various information regarding
the size of the packets and segment lengths from the flow
Bwd Packet Length Max
Bwd Packet Length Mean
Bwd Packet Length std
Total Backward Packets
Avg Fwd Segment Size
"""


def packet_length(flow_with_direction, packet_df):
row = 0
for key in flow_with_direction: #extract a key from the dictionary
Expand Down
4 changes: 4 additions & 0 deletions src/extract_window_size.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

"""This file contains functions to extract following given features from the flow
Init_Win_bytes_forward(Intial window bytes size in forward direction)
act_data_pkt_fwd (actual data packets in forward direction)
"""
def init_window_size(flow_with_direction, packet_df):
row = 0
for key in flow_with_direction:
Expand Down
69 changes: 58 additions & 11 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pandas as pd
import argparse
import pyshark
import os
from extractFlow import extract_flow_info
from FlowDirection import flow_direction
from extractTimeStamps import Time_main
Expand All @@ -9,7 +11,7 @@
from predict_model import predict_output


def main():
def flow_file(pcap_file, flag):
#define columns in the dataframe
columns = [' Bwd IAT Max', ' Bwd URG Flags', ' Total Backward Packets',
' Fwd IAT Max', ' Active Max', ' URG Flag Count',
Expand All @@ -22,7 +24,7 @@ def main():
try:
packet_df = pd.DataFrame(columns=columns)

pcap = pyshark.FileCapture('pcap_files/example.pcap')
pcap = pyshark.FileCapture(pcap_file)
flow_dictionary, features_df = extract_flow_info(pcap)
flow_with_direction = flow_direction(flow_dictionary)
Time_main(flow_with_direction, packet_df)
Expand All @@ -34,21 +36,66 @@ def main():

#drop extra columns from the dataframe !!
packet_df.drop(['dummy',18],axis = 1,inplace=True)
# print(packet_df)

predict_output(packet_df, features_df)
if flag == "flow":
print("Output generated Successfully\n", packet_df.describe())

flow_csv_path = 'flow.csv'

mapping_dict = {0:"BENIGN",4:"DoS Hulk",2:"DDoS", 10:"PortScan",3:"DoS GoldenEye",5:"DoS Slowhttptest",
6:"DoS slowloris", 7:"FTP-Patator",11:"SSH-Patator",1:"Bot",12:"Web Attack � Brute Force",
8:"Heartbleed ", 9:"Infiltration",13:"Web Attack � Sql Injection",14:"Web Attack � XSS"}
packet_df.to_csv(flow_csv_path, index=False)
print(f"Flow CSV file generated: {flow_csv_path}")

features_df['result'] = features_df['result'].replace(mapping_dict)

"""----------------------saving to csv file------------------------"""
features_df.to_csv("ansDF.csv",index=False)
except Exception as e:
print("An error occured: ", e.args[0])

return packet_df,features_df

def result_file(packet_df, features_df):
# print(packet_df)
try:
predict_output(packet_df, features_df)

mapping_dict = {0:"BENIGN",4:"DoS Hulk",2:"DDoS", 10:"PortScan",3:"DoS GoldenEye",5:"DoS Slowhttptest",
6:"DoS slowloris", 7:"FTP-Patator",11:"SSH-Patator",1:"Bot",12:"Web Attack � Brute Force",
8:"Heartbleed ", 9:"Infiltration",13:"Web Attack � Sql Injection",14:"Web Attack � XSS"}

features_df['result'] = features_df['result'].replace(mapping_dict)

"""----------------------saving to csv file------------------------"""

print("Output generated Successfully\n",features_df['result'].value_counts())

flow_csv_path = 'result.csv'

features_df.to_csv(flow_csv_path, index=False)
print(f"Result CSV file generated: {flow_csv_path}")

except Exception as e:
print("An error occured: ", e.args[0])

def main():

parser = argparse.ArgumentParser(description="Network Intrusion Tool/flanker-toolX")
parser.add_argument('-f', '--flow', action='store_true', help="Generate flow.csv file")
parser.add_argument('-r', '--result', action='store_true', help="Get result")
parser.add_argument('--pcap', type=str, help="Path to the uploaded pcap/pcapng file")

args = parser.parse_args()

if args.flow and args.pcap:
if os.path.isfile(args.pcap) and args.pcap.endswith('.pcap') or args.pcap.endswith('.pcapng'):
pkt_df, ft_df = flow_file(args.pcap, "flow")
else:
print("Invalid pcap file. Please provide a valid path to a .pcap file.")
elif args.result and args.pcap:
if os.path.isfile(args.pcap) and args.pcap.endswith('.pcap') or args.pcap.endswith('.pcapng'):
pkt_df, ft_df = flow_file(args.pcap,"res")
result_file(pkt_df, ft_df)
else:
print("Invalid pcap file. Please provide a valid path to a .pcap/.pcapng file.")
else:
print("Invalid option. Please choose either --flow or --result, and provide a valid --pcap file path.\nuse --help for more information")

return

"""---------------------------------------------------------------------------------------------"""
Expand Down
4 changes: 4 additions & 0 deletions src/predict_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

"""This Fcuntion takes the extracted flow and predict in the pre-trained model
and Will resturn the result to the user"""

import joblib
def predict_output(packet_df, features_df):
model = joblib.load('ML_model/saved_model.pkl')
Expand Down

0 comments on commit 7f7c3c2

Please sign in to comment.