Skip to content

Commit

Permalink
Uploading to PyPi
Browse files Browse the repository at this point in the history
  • Loading branch information
kaptcha0 committed Dec 15, 2020
1 parent b503b1e commit 8101c2f
Show file tree
Hide file tree
Showing 23 changed files with 249 additions and 63 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ proxy.sh [target] [port]
- `-s` or `--serve` to start the proxy server
- `-t` or `--train` to train the model
- Default

Example:
```bash
python -m quasar_firewall [options] [target] [port]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: quasar-project-firewall
Version: 1.0.1.1
Name: quasar
Version: 1.0.0
Summary: An Artificial Intelligent firewall that detects malicious HTTP requests
Home-page: https://github.com/kyro-dev/Quasar-Project
Author: Kyro Dev
Expand Down Expand Up @@ -43,6 +43,7 @@ Description: # Quasar Firewall
- `-s` or `--serve` to start the proxy server
- `-t` or `--train` to train the model
- Default

Example:
```bash
python -m quasar_firewall [options] [target] [port]
Expand Down Expand Up @@ -70,6 +71,8 @@ Description: # Quasar Firewall


Platform: UNKNOWN
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
Classifier: Operating System :: OS Independent
Expand Down
21 changes: 21 additions & 0 deletions quasar.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
README.md
setup.py
quasar/__init__.py
quasar/__main__.py
quasar/app.py
quasar/dataset.py
quasar/detector.py
quasar/detector_flask.py
quasar/evolution.py
quasar/neuralnet.py
quasar/request.py
quasar/request_parser.py
quasar/visualize.py
quasar.egg-info/PKG-INFO
quasar.egg-info/SOURCES.txt
quasar.egg-info/dependency_links.txt
quasar.egg-info/entry_points.txt
quasar.egg-info/requires.txt
quasar.egg-info/top_level.txt
scripts/proxy.sh
scripts/train.sh
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
autopep8==1.5.4
autopep8
bleach==3.2.1
certifi==2020.12.5
chardet==4.0.0
chardet
click==7.1.2
colorama==0.4.4
cycler==0.10.0
docutils==0.16
Flask==1.1.2
Flask
graphviz==0.15
idna==2.10
itsdangerous==1.1.0
Expand All @@ -17,7 +17,7 @@ kiwisolver==1.3.1
MarkupSafe==1.1.1
matplotlib==3.3.3
neat-python==0.92
numpy==1.19.4
numpy==1.19.3
packaging==20.7
Pillow==8.0.1
pkginfo==1.6.1
Expand All @@ -27,7 +27,7 @@ pyparsing==2.4.7
python-dateutil==2.8.1
pywin32-ctypes==0.2.0
readme-renderer==28.0
requests==2.25.0
requests
requests-toolbelt==0.9.1
rfc3986==1.4.0
scikit-learn==0.23.2
Expand All @@ -36,7 +36,7 @@ six==1.15.0
threadpoolctl==2.1.0
toml==0.10.2
tqdm==4.54.1
twine==3.2.0
urllib3==1.26.2
webencodings==0.5.1
Werkzeug==1.0.1
wheel
Werkzeug
File renamed without changes.
9 changes: 9 additions & 0 deletions quasar/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Quasar
A fully artificaially intelligent firewall
"""

__version__ = "1.0.0"
__author__ = 'Kyro'
__credits__ = 'KyroDev'
2 changes: 1 addition & 1 deletion quasar/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

parser = argparse.ArgumentParser(description="AI Firewall")
parser.add_argument("-t", "--train", help="Train the model",
action="store_true")
nargs='?', const=True)
parser.add_argument("-s", "--serve", nargs=2, metavar=("target", "port"),
help="Start the proxy server on 'port' with proxy destination being 'target'")

Expand Down
26 changes: 17 additions & 9 deletions quasar/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
from .request import Request


def parse_dataset(f:str):
def get_directory(dir_name: str = ''):
from pathlib import Path

path_to_quasar = Path(__file__).parents[0]
return path_to_quasar / dir_name


def parse_dataset(f: str):
"""
- Parses the dataset
- Input dataset must be xml
Expand All @@ -27,7 +34,7 @@ def parse_dataset(f:str):
class_elem = sample.find('class')
req = sample.find('request')
data = {}

for item in req:
tag = item.tag
if tag.lower() not in ["uri", "query", "body"]:
Expand All @@ -40,7 +47,7 @@ def parse_dataset(f:str):

requests.append(Request(data).to_dict())

file = open("dataset.json", "w")
file = open("./datasets/dataset.json", "w")

file.write(json.dumps(requests))

Expand All @@ -49,26 +56,27 @@ def parse_dataset(f:str):
return requests


def load_dataset(file:str="./datasets/web-application-attacks-datasets/ecml_pkdd/learning_dataset.xml"):
def load_dataset(file: str = f"{get_directory('datasets') / 'web-application-attacks-datasets/ecml_pkdd/learning_dataset.xml'}"):
"""
Handles dataset loading, returns parsed dataset in `List[Request]` form
"""
os.chdir(str(get_directory()))

global data
data = None
reqs: List[Request] = []

if not os.path.exists("./dataset.json"):
if not os.path.exists("./datasets/dataset.json"):
data = parse_dataset(file)

try:
with open("./dataset.json") as f:
with open("./datasets/dataset.json") as f:
data = json.load(f)
for d in data:
reqs.append(Request(d))
except json.decoder.JSONDecodeError:
os.remove('./dataset.json')
raise RuntimeError("An error occured, please try again later")
except json.decoder.JSONDecodeError as e:
import traceback
traceback.print_exc()

return reqs

Expand Down
17 changes: 12 additions & 5 deletions quasar/detector_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
from .request_parser import BodyParser, QueryParser


def get_directory(dir_name: str):
from pathlib import Path

path_to_quasar = Path(__file__).parents[0]
return str(path_to_quasar / dir_name)


class DetectorMiddleware(object):
"""
Middleware for detecting hacks
Expand All @@ -17,12 +24,13 @@ class DetectorMiddleware(object):
def __init__(self, app: Flask):
print("Loading models")
self.app = app
self.evolution: Evolution = Evolution.load('499', './models')
self.evolution: Evolution = Evolution.load(
'499', get_directory("models"))
self.body_parser = BodyParser.load()
self.query_parser = QueryParser.load()
self.detector = Detector(
self.evolution, self.body_parser, self.query_parser)

print("Finished loading")

try:
Expand All @@ -44,13 +52,12 @@ def __call__(self, environ, start_response):
"is_hack": None
})

valid = self.detector.predict(data, request.get_data(), request.query_string)

valid = self.detector.predict(
data, request.get_data(), request.query_string)

if valid:
# Its a hack
res = Response("ERROR 404", 404)
return res(environ, start_response)

return self.app(environ, start_response)

18 changes: 12 additions & 6 deletions quasar/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ def train(self, config_path: str):

winner = self.p.run(self.__eval_genome__, self.generations)

node_names = {-1: "method", -2: "content-type", -3: "protocol", 0: "Hack probability"}
node_names = {-1: "method", -2: "content-type", -
3: "protocol", 0: "Hack probability"}

visualize.draw_net(self.__get_config__(
config_path), winner, view=True, filename="./visualizations/model", node_names=node_names)
visualize.plot_stats(self.stats, filename="./visualizations/avg_fitness.svg", ylog=False, view=True)
visualize.plot_species(self.stats, filename="./visualizations/speciation.svg", view=True)
visualize.plot_stats(
self.stats, filename="./visualizations/avg_fitness.svg", ylog=False, view=True)
visualize.plot_species(
self.stats, filename="./visualizations/speciation.svg", view=True)

return winner

Expand All @@ -68,13 +71,16 @@ def predict(self, input: Request):

@staticmethod
def load(checkpoint: str, session: str = '.'):
from pathlib import Path
"""
Loads checkpoint with the prefix of `neat-chekpoint-`
- checkpoint: desired checkpoint id
- session: session directory (defaults to current directory)
"""
path = Path(str(session)) / f"neat-checkpoint-{str(checkpoint)}"

point: Population = neat.Checkpointer.restore_checkpoint(
f'{str(session)}/neat-checkpoint-{str(checkpoint)}')
f'{str(path)}')

return Evolution(point)

Expand Down Expand Up @@ -199,7 +205,7 @@ def train(self, config_path: str):

pe = neat.ThreadedEvaluator(
multiprocessing.cpu_count() - 1, self.__eval_genome__)

winner = self.p.run(pe.evaluate, self.generations)

node_names = {-1: "method", -2: "headers", -3: "protocol", -
Expand All @@ -211,7 +217,7 @@ def train(self, config_path: str):
visualize.plot_species(self.stats, view=True)

return winner

@staticmethod
def load(checkpoint: str, session: str = '.'):
"""
Expand Down
80 changes: 80 additions & 0 deletions quasar_project.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Metadata-Version: 2.1
Name: quasar-project
Version: 1.0.0
Summary: An Artificial Intelligent firewall that detects malicious HTTP requests
Home-page: https://github.com/kyro-dev/Quasar-Project
Author: Kyro Dev
Author-email: [email protected]
License: UNKNOWN
Description: # Quasar Firewall
An AI powered firewall designed to detect mallicious HTTP requests and decide weather to process them or not.

```
pip install quasar-project-firewall
```

# Usage
## As Flask Middleware
*Note: Model must be trained before starting proxy server*
```python
from detector_flask import DetectorMiddleware
...
app.wsgi_app = DetectorMiddleware(app.wsgi_app)
...
```
## Starting Proxy From Command Line
*Note: Must have python installed on local machine and added to PATH*<br>
*Note: Model must be trained before starting proxy server*

### **With Bash**
Call `proxy.sh` with paramaters `target` and `port`
- `target` is the proxy destination
- defaults to `http://localhost:8080`
- `port` is the local port to run the server on
- defaults to 5000

Example:
```bash
proxy.sh [target] [port]
```

### **As Python Module**
- `options`
- `-s` or `--serve` to start the proxy server
- `-t` or `--train` to train the model
- Default

Example:
```bash
python -m quasar_firewall [options] [target] [port]
```

Redirect requests from original server to proxy server


## Training the Model
*Note: Must have python installed on local machine and added to PATH*

### **With Bash**
Call `train.sh`

Example:
```bash
train.sh
```

### **As Python Module**
Example:
```bash
python -m quasar_firewall -t
```


Platform: UNKNOWN
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
21 changes: 21 additions & 0 deletions quasar_project.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
README.md
setup.py
quasar/__init__.py
quasar/__main__.py
quasar/app.py
quasar/dataset.py
quasar/detector.py
quasar/detector_flask.py
quasar/evolution.py
quasar/neuralnet.py
quasar/request.py
quasar/request_parser.py
quasar/visualize.py
quasar_project.egg-info/PKG-INFO
quasar_project.egg-info/SOURCES.txt
quasar_project.egg-info/dependency_links.txt
quasar_project.egg-info/entry_points.txt
quasar_project.egg-info/requires.txt
quasar_project.egg-info/top_level.txt
scripts/proxy.sh
scripts/train.sh
1 change: 1 addition & 0 deletions quasar_project.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions quasar_project.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[console_scripts]
quasar = quasar.__main__:main

Loading

0 comments on commit 8101c2f

Please sign in to comment.