From fd55a78a5a67147770560d80c2f56015f438f4b8 Mon Sep 17 00:00:00 2001 From: Paul SQUIZZATO Date: Fri, 17 Mar 2023 14:18:15 +0100 Subject: [PATCH 01/17] Ajouter 'Documentation/Compte rendus/CR-17-03-2023' --- Documentation/Compte rendus/CR-17-03-2023 | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Documentation/Compte rendus/CR-17-03-2023 diff --git a/Documentation/Compte rendus/CR-17-03-2023 b/Documentation/Compte rendus/CR-17-03-2023 new file mode 100644 index 0000000..d418964 --- /dev/null +++ b/Documentation/Compte rendus/CR-17-03-2023 @@ -0,0 +1,9 @@ +## Rendez-vous du 17 Mars + +- Il nous reste 14h (sans compter la séance du 17/03) + +- Une fois la BDD qui marche, comment à tester : + - Utiliser des scripts de client pymodbus; + - ou avec des envois de paquets en dur directement dans le decodeur. + +#### Prochain rendez-vous le Mardi 21 Mars à 10h15 (Démo du code) \ No newline at end of file From 63699b8c795d1a3f887e8230c74196a39bfb322d Mon Sep 17 00:00:00 2001 From: Paul SQUIZZATO Date: Fri, 17 Mar 2023 14:18:26 +0100 Subject: [PATCH 02/17] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'Documentatio?= =?UTF-8?q?n/Compte=20rendus/CR-17-03-2023.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Documentation/Compte rendus/{CR-17-03-2023 => CR-17-03-2023.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Documentation/Compte rendus/{CR-17-03-2023 => CR-17-03-2023.md} (100%) diff --git a/Documentation/Compte rendus/CR-17-03-2023 b/Documentation/Compte rendus/CR-17-03-2023.md similarity index 100% rename from Documentation/Compte rendus/CR-17-03-2023 rename to Documentation/Compte rendus/CR-17-03-2023.md From a8e4ba4cd8dbf8207a7e7bfbaa95c434fed767e9 Mon Sep 17 00:00:00 2001 From: louwar Date: Fri, 17 Mar 2023 14:57:40 +0100 Subject: [PATCH 03/17] Update doc bug --- src/readme.md | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/src/readme.md b/src/readme.md index ac2184a..dee36af 100644 --- a/src/readme.md +++ b/src/readme.md @@ -1,32 +1,3 @@ -# Install - -```sh -python -m pip install pipenv -pipenv update -pipenv shell -``` - -# Run - -Within pipenv, run in two terminals: - -```sh -./start_server.sh -./start_client.sh -``` - -# Example commands - -Within client run: - -``` -client.read_coils slave=1 address=0 -client.write_coil slave=1 address=0 value=1 -client.read_coils slave=1 address=0 -``` - -See the first boolean was false in the first read, was written to true, and appears at true in second read. - # Client commands : ``` @@ -44,4 +15,5 @@ client.write_registers address=0 values=845,123,0,427,4 slave=1 client.read_holding_registers address=0 count=1 slave=1 client.read_holding_registers address=0 count=100 slave=1 -``` \ No newline at end of file +``` + From fde227311aaccc9fe72e7c7b0de4b3f4e678ab82 Mon Sep 17 00:00:00 2001 From: louwar Date: Fri, 17 Mar 2023 14:59:52 +0100 Subject: [PATCH 04/17] Update doc bug --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 00b53de..1afd78b 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,10 @@ ## Utilisation ### Install + ```sh // Install d'outil -python -m pip install pipenv +python3 -m pip install pipenv // Install module pip install typer @@ -18,6 +19,24 @@ pipenv update // Lancement du shell pipenv pipenv shell ``` +**ATTENTION** : lorsque vous voulez faire un pipenv update cela vous donne une version buguée de pymodub qui est là `3.2.0` *(pour voir votre version faite un `pip freeze`)* + +#### Marche à suivre: +1) Désinstallez la bibliothèque pymodbus à l'aide de la commande suivante: +> pipenv uninstall pymodbus + +2) Installez une version précédente de la bibliothèque pymodbus en utilisant la commande suivante: +> pipenv install pymodbus==3.1.3 + +### Lancement +il faut lancé 3 terminal pipenv pour : + +```sh +./start_server.sh +./start_client.sh +sudo python3 ./decoder.py +``` + ### Côté BDD Il faudra que vous connectiez à votre BDD PostgreSQL. Exécuter le script `Table.sql` qui se trouve dans src avec la commande ci-dessous. @@ -39,21 +58,34 @@ psql -h londres -d -U -W **ATTENTION** `londres` est un serveur héberger dans l'infrastructure de notre établissement universitaire. -### Lancement -il faut lancé 3 terminal pipenv pour : - -```sh -./start_server.sh -./start_client.sh -sudo python3 ./decoder.py -``` - ## Notre configuration * Python (3.9) * PostgreSQL * pip (22.0.2) * pymodbus (3.1.3) +Notre pipfile: +``` +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +redis = "*" +click = "*" +prompt-toolkit = "*" +pymodbus = {extras = ["repl"], version = "*"} +sqlalchemy = "*" +scapy = "*" +ipython = "*" + +[dev-packages] + +[requires] +python_version = "3.9" +``` + ## Développeurs * [Louis](https://codefirst.iut.uca.fr/git/louis.dufour) * [Paul](https://codefirst.iut.uca.fr/git/paul.squizzato) From 6939b6f107de1627ed9807054b830654d4aeaa10 Mon Sep 17 00:00:00 2001 From: Darius Bertrand Date: Fri, 17 Mar 2023 15:01:21 +0000 Subject: [PATCH 05/17] q --- src/Main.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Main.py b/src/Main.py index 8194541..35942b0 100644 --- a/src/Main.py +++ b/src/Main.py @@ -19,21 +19,36 @@ import getpass ################# Fin Tips ################# -def insert_Status(connection,flow): - cur = connection.cursor() +def insert_Status(type, adresse, valeur): + +if __name__ == '__main__': + db_host = input('Nom d\'hôte : ') + if not db_host: + db_host = 'londres' + db_name = input('Nom de la base de données : ') + if not db_name: + db_name = 'dblodufour1' + db_user = input('Utilisateur : ') + if not db_user: + db_user = 'ladufour1' + db_password = getpass('Mot de passe : ') + connection = psy.connect(host=db_host, database=db_name, user=db_user, password=db_password) + cur = connection.cursor() + for row in df.itertuples(): cur.execute("INSERT INTO Status VALUES (%s,%s,%s );", - (row.addresse, - row.type, - row.valeur) + (type, + adresse, + valeur) ) connection.commit() cur.close() + def status_State(connection): cur = connection.cursor() pd.read_sql(''' SELECT adresse, type, valeur FROM Status''' From 937e87db89d477142288acc8e3e0abf11103f2fc Mon Sep 17 00:00:00 2001 From: Paul Squizzato Date: Fri, 17 Mar 2023 15:10:39 +0000 Subject: [PATCH 06/17] CA MARCHE CA MARCHE YOUHOU LE DECODERBRUT EST INSANE --- src/decoderBrut.py | 10 ++++++---- src/triPacket.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/triPacket.py diff --git a/src/decoderBrut.py b/src/decoderBrut.py index 571c681..2284178 100644 --- a/src/decoderBrut.py +++ b/src/decoderBrut.py @@ -3,6 +3,7 @@ import scapy.all as scapy import scapy.contrib.modbus as mb +from triPacket import triPacket def decode(pkt): prt=0 @@ -44,12 +45,12 @@ def decode(pkt): valInter2=valInter[1:-1] miniL[2]=valInter2 if miniL.count(0)==0: - if(type(miniL[2])==list): + if type(miniL[2])==list: if miniL[0]=="r": for i in range(len(miniL[2])): - if("0x" in miniL[2][i]): + if "0x" in miniL[2][i]: miniL[2][i]=int(miniL[2][i],16) - if("0x" in str(miniL[1])): + if "0x" in str(miniL[1]): miniL[1]=int(miniL[1],16) bigL.append([miniL[0],miniL[1]+i,miniL[2][i]]) if miniL[0]=="c": @@ -92,7 +93,8 @@ def decode(pkt): bigL.append(miniL) if bigL!=[]: - print(bigL) + print("attention") + triPacket(bigL) miniL = [0,0,0,0] diff --git a/src/triPacket.py b/src/triPacket.py new file mode 100644 index 0000000..3708cc0 --- /dev/null +++ b/src/triPacket.py @@ -0,0 +1,16 @@ +def triPacket(lPkt): + if type(lPkt)!=list: + print('pas liste') + return + if len(lPkt)==0: + print('vide') + return + if len(lPkt)==1: + triPacket(lPkt[0]) + return + if len(lPkt)==3 and type(lPkt[0])==str: + print(lPkt[0],int(lPkt[1]),int(lPkt[2])) + return + else: + for i in lPkt: + triPacket(i) From 3e95fd366fc65e1c7a24302c9d4fc5d5913cda3b Mon Sep 17 00:00:00 2001 From: Darius Bertrand Date: Fri, 17 Mar 2023 15:20:38 +0000 Subject: [PATCH 07/17] avancement bdd --- src/Main.py | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Main.py b/src/Main.py index 35942b0..7649eb4 100644 --- a/src/Main.py +++ b/src/Main.py @@ -21,29 +21,26 @@ import getpass def insert_Status(type, adresse, valeur): -if __name__ == '__main__': - db_host = input('Nom d\'hôte : ') - if not db_host: - db_host = 'londres' - db_name = input('Nom de la base de données : ') - if not db_name: - db_name = 'dblodufour1' - db_user = input('Utilisateur : ') - if not db_user: - db_user = 'ladufour1' - db_password = getpass('Mot de passe : ') - - connection = psy.connect(host=db_host, database=db_name, user=db_user, password=db_password) + if __name__ == '__main__': + db_host = input('Nom d\'hôte : ') + if not db_host: + db_host = 'londres' + db_name = input('Nom de la base de données : ') + if not db_name: + db_name = 'dblodufour1' + db_user = input('Utilisateur : ') + if not db_user: + db_user = 'ladufour1' + db_password = getpass('Mot de passe : ') + + connection = psy.connect(host=db_host, database=db_name, user=db_user, password=db_password) cur = connection.cursor() - - - - for row in df.itertuples(): - cur.execute("INSERT INTO Status VALUES (%s,%s,%s );", - (type, - adresse, - valeur) - ) + for row in df.itertuples(): + cur.execute("INSERT INTO Status VALUES (%s,%s,%s );", + (type, + adresse, + valeur) + ) connection.commit() cur.close() @@ -82,4 +79,7 @@ if __name__ == '__main__': create_tables(connection, 'Table.sql') - connection.close() + connection.close() + + +insert_Status(c,1,1) From 694a12a5e4fc4dff5ac5610009b7b2f61a967f24 Mon Sep 17 00:00:00 2001 From: Paul SQUIZZATO Date: Mon, 20 Mar 2023 14:56:13 +0100 Subject: [PATCH 08/17] =?UTF-8?q?Premier=20test=20d'impl=C3=A9mentation=20?= =?UTF-8?q?de=20la=20BDD=20via=20le=20decoder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decoderBrut.py | 18 +++++++++++++++++- src/ecritureBDD.py | 20 ++++++++++++++++++++ src/triPacket.py | 9 ++++++--- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/ecritureBDD.py diff --git a/src/decoderBrut.py b/src/decoderBrut.py index 2284178..1469a73 100644 --- a/src/decoderBrut.py +++ b/src/decoderBrut.py @@ -5,6 +5,7 @@ import scapy.all as scapy import scapy.contrib.modbus as mb from triPacket import triPacket + def decode(pkt): prt=0 addr,value = "","" @@ -94,11 +95,26 @@ def decode(pkt): if bigL!=[]: print("attention") - triPacket(bigL) + triPacket(bigL,connec) miniL = [0,0,0,0] bigL=[] miniL = [0,0,0,0] + + +print("In order for data sniffed to be stored inside the database, please register the following :") +db_host = input('host of the database server : ') +if not db_host: + db_host = 'londres' +db_name = input('name of the database : ') +if not db_name: + db_name = 'dblodufour1' +db_user = input('login of the user : ') +if not db_user: + db_user = 'lodufour1' +db_password = getpass('user password : ') +connec=[db_host,db_name,db_user,db_password] + scapy.sniff(iface="lo", prn=decode) diff --git a/src/ecritureBDD.py b/src/ecritureBDD.py new file mode 100644 index 0000000..9e7a650 --- /dev/null +++ b/src/ecritureBDD.py @@ -0,0 +1,20 @@ +import psycopg2 as psy +import pandas as pd +import getpass + +def ecritureBDD(addresse,typeMem,valeur,connec): + try: + co = psy.connect(host=connec[0], + database=connec[1], + user=connec[3], + password=connec[4]) + cur = co.cursor() + cur.execute("INSERT INTO Status VALUES (%s,%s,%s );", + (addresse,typeMem,valeur)) + co.commit() + cur.close() + except(Exception,psy.DatabaseError) as error: + print(error) + finally: + if co is not None: + co.close() \ No newline at end of file diff --git a/src/triPacket.py b/src/triPacket.py index 3708cc0..be7c22b 100644 --- a/src/triPacket.py +++ b/src/triPacket.py @@ -1,4 +1,6 @@ -def triPacket(lPkt): +from ecritureBDD import ecritureBDD + +def triPacket(lPkt,connec): if type(lPkt)!=list: print('pas liste') return @@ -8,8 +10,9 @@ def triPacket(lPkt): if len(lPkt)==1: triPacket(lPkt[0]) return - if len(lPkt)==3 and type(lPkt[0])==str: - print(lPkt[0],int(lPkt[1]),int(lPkt[2])) + if len(lPkt)==3 and type(lPkt[0])==str: + if (lPkt[0]=='c' or lPkt[0]=='r') and lPkt[1]>=0 and lPkt[2]>=0: + print(lPkt[0],int(lPkt[1]),int(lPkt[2])) return else: for i in lPkt: From 98c45d905c015324b66ddb1f1b619d4ec58b3193 Mon Sep 17 00:00:00 2001 From: Paul Squizzato Date: Mon, 20 Mar 2023 15:54:12 +0000 Subject: [PATCH 09/17] =?UTF-8?q?Base=20de=20donn=C3=A9es=20fonctionnelles?= =?UTF-8?q?!=20Tests=20r=C3=A9alis=C3=A9s=20par=20Paul,=20du=20coup=20il?= =?UTF-8?q?=20faudra=20retester=20parce=20qu'on=20connait=20tous=20comment?= =?UTF-8?q?=20=C3=A7a=20va=20avec=20Paul.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decoderBrut.py | 6 +++--- src/ecritureBDD.py | 29 +++++++++++++---------------- src/triPacket.py | 7 +++---- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/decoderBrut.py b/src/decoderBrut.py index 1469a73..e83211d 100644 --- a/src/decoderBrut.py +++ b/src/decoderBrut.py @@ -4,7 +4,7 @@ import scapy.all as scapy import scapy.contrib.modbus as mb from triPacket import triPacket - +import getpass def decode(pkt): prt=0 @@ -106,14 +106,14 @@ miniL = [0,0,0,0] print("In order for data sniffed to be stored inside the database, please register the following :") db_host = input('host of the database server : ') if not db_host: - db_host = 'londres' + db_host = '192.168.128.141' db_name = input('name of the database : ') if not db_name: db_name = 'dblodufour1' db_user = input('login of the user : ') if not db_user: db_user = 'lodufour1' -db_password = getpass('user password : ') +db_password = getpass.getpass('user password : ') connec=[db_host,db_name,db_user,db_password] scapy.sniff(iface="lo", prn=decode) diff --git a/src/ecritureBDD.py b/src/ecritureBDD.py index 9e7a650..901fbbe 100644 --- a/src/ecritureBDD.py +++ b/src/ecritureBDD.py @@ -2,19 +2,16 @@ import psycopg2 as psy import pandas as pd import getpass -def ecritureBDD(addresse,typeMem,valeur,connec): - try: - co = psy.connect(host=connec[0], - database=connec[1], - user=connec[3], - password=connec[4]) - cur = co.cursor() - cur.execute("INSERT INTO Status VALUES (%s,%s,%s );", - (addresse,typeMem,valeur)) - co.commit() - cur.close() - except(Exception,psy.DatabaseError) as error: - print(error) - finally: - if co is not None: - co.close() \ No newline at end of file +def ecritureBDD(typeMem,addresse,valeur,connec): + co = None + try: + co = psy.connect(host=connec[0],database=connec[1],user=connec[2],password=connec[3]) + cur = co.cursor() + cur.execute("INSERT INTO Status VALUES (%s,%s,%s ) ON CONFLICT (addresse,type) DO UPDATE SET valeur=%s;",(addresse,typeMem,valeur,valeur)) + co.commit() + cur.close() + except(Exception,psy.DatabaseError) as error: + print(error) + finally: + if co is not None: + co.close() diff --git a/src/triPacket.py b/src/triPacket.py index be7c22b..b5309d8 100644 --- a/src/triPacket.py +++ b/src/triPacket.py @@ -8,12 +8,11 @@ def triPacket(lPkt,connec): print('vide') return if len(lPkt)==1: - triPacket(lPkt[0]) + triPacket(lPkt[0],connec) return if len(lPkt)==3 and type(lPkt[0])==str: - if (lPkt[0]=='c' or lPkt[0]=='r') and lPkt[1]>=0 and lPkt[2]>=0: - print(lPkt[0],int(lPkt[1]),int(lPkt[2])) + ecritureBDD(lPkt[0],int(lPkt[1]),int(lPkt[2]),connec) return else: for i in lPkt: - triPacket(i) + triPacket(i,connec) From a1d72bdeb1f2dab332c38faa421fc51ba360d129 Mon Sep 17 00:00:00 2001 From: louwar Date: Tue, 21 Mar 2023 11:24:22 +0100 Subject: [PATCH 10/17] Add CR & fix bug Main.py --- Documentation/Compte rendus/CR-21-03-2023.md | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Documentation/Compte rendus/CR-21-03-2023.md diff --git a/Documentation/Compte rendus/CR-21-03-2023.md b/Documentation/Compte rendus/CR-21-03-2023.md new file mode 100644 index 0000000..6dcfd74 --- /dev/null +++ b/Documentation/Compte rendus/CR-21-03-2023.md @@ -0,0 +1,23 @@ +## Rendez-vous du 21 Mars + +- Commenter la récursivité et justifier *(dans triPacket)* +- En terme de qualité de code c'est Zéro si on rend le décoder brut +- Doxygen +- Docker wazuh + +### Ce qu'il reste à faire: +- filtrer les paquets (donc juste un dictionnaire "si j'ai coils 5 qui vaut true et le regsitre 45 j'empêche cette variable à tel valeurs" création de règle) + - Si un scénario est repérer on bloque tout la request *(S'il y a valeur interdite je bloque tout ou non ? (utile soustenance))* +- Prévoir démo soustenance +- Idée indicateur (métrique) pilnt/sonnar *(un code comme ça c'est détruit et refais)* + +### Un docker pour: +- Code python +- Docker compose entre tout les container *(postgree, client, serveur, python)* + + +#### Prochain rendez-vous: +- Mercredi 29 Mars à 8h30 : démo BDD et rapport *(envoie Lundi 27 Mars soir)* +- Jeudi 30 Mars à 9h : oral blanc soutenance + + From f163cd60c845cfbf070ab7e94aa72c2ef9904bcd Mon Sep 17 00:00:00 2001 From: louwar Date: Tue, 21 Mar 2023 11:24:51 +0100 Subject: [PATCH 11/17] Add CR & fix bug Main.py --- src/Main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.py b/src/Main.py index 7649eb4..21466ba 100644 --- a/src/Main.py +++ b/src/Main.py @@ -48,7 +48,7 @@ def insert_Status(type, adresse, valeur): def status_State(connection): cur = connection.cursor() - pd.read_sql(''' SELECT adresse, type, valeur FROM Status''' + pd.read_sql(''' SELECT adresse, type, valeur FROM Status''') res = cur.fetchone() return res cur.close() From 295ccc9ed763481f13e2f1f991d476015dfd004e Mon Sep 17 00:00:00 2001 From: louwar Date: Tue, 21 Mar 2023 11:40:53 +0100 Subject: [PATCH 12/17] Add CI --- .drone.yml | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 17 +++++++++ requirements.txt | 5 +++ src/Main.py | 1 - 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 .drone.yml create mode 100644 Dockerfile create mode 100644 requirements.txt diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..b314fa3 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,95 @@ +kind: pipeline +type: docker +name: default + +trigger: + event: + - push + +steps: + - name: build + image: python:3.9-slim-buster + volumes: + - name: docs + path: /docs + commands: + - cd src/ + - python -m venv venv + - source venv/bin/activate + - pip install -r requirements.txt + - python setup.py install + - python -m pytest + - python setup.py sdist bdist_wheel + + - name: upload + image: python:3.9-slim-buster + volumes: + - name: docs + path: /docs + commands: + - cd Sources/ + - pip install twine + - twine upload dist/* + + + # docker image build + - name: docker-build-and-push + image: plugins/docker + settings: + dockerfile: ./Dockerfile + context: src/ + registry: hub.codefirst.iut.uca.fr + repo: hub.codefirst.iut.uca.fr/louis.dufour/detection_d_intrusion + username: + from_secret: SECRET_REGISTRY_USERNAME + password: + from_secret: SECRET_REGISTRY_PASSWORD + + + - name: generate-and-deploy-docs + image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-docdeployer + failure: ignore + volumes: + - name: docs + path: /docs + commands: + #- cd Documentation/doxygen + #- doxygen Doxyfile + - /entrypoint.sh + when: + branch: + - master + depends_on: [ build ] + + # container deployment + - name: deploy-container + image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest + environment: + IMAGENAME: hub.codefirst.iut.uca.fr/louis.dufour/detection_d_intrusion:latest + CONTAINERNAME: containersae + COMMAND: create + OVERWRITE: true + depends_on: [ docker-build-and-push ] + + - name: code-analysis + image: sonarsource/sonar-scanner-cli:latest + commands: + - cd src/ + - sonar-scanner \ + -Dsonar.projectKey=MyPythonProject \ + -Dsonar.projectName=My Python Project \ + -Dsonar.sources=. \ + -Dsonar.host.url=$${PLUGIN_SONAR_HOST} \ + -Dsonar.login=$${PLUGIN_SONAR_TOKEN} + secrets: [ SONAR_TOKEN ] + settings: + # accessible en ligne de commande par $${PLUGIN_SONAR_HOST} + sonar_host: https://codefirst.iut.uca.fr/sonar/ + # accessible en ligne de commande par $${PLUGIN_SONAR_TOKEN} + sonar_token: + from_secret: SONAR_TOKEN + depends_on: [build] + +volumes: +- name: docs + temp: {} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1a43d9c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +# Définit l'image de base +FROM python:3.9 + +# Crée le répertoire de travail et le définit comme répertoire courant +WORKDIR /app + +# Copie les fichiers du projet dans le conteneur +COPY . . + +# Installe les dépendances du projet +RUN pip install --no-cache-dir -r requirements.txt + +# Expose le port utilisé par l'application +EXPOSE 8000 + +# Démarre l'application +CMD ["python", "app.py"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bd457d9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +psycopg2 +pandas +getpass +scapy.all +scapy.contrib.modbus \ No newline at end of file diff --git a/src/Main.py b/src/Main.py index 21466ba..28775cb 100644 --- a/src/Main.py +++ b/src/Main.py @@ -7,7 +7,6 @@ # # ======================================================================== -from cProfile import label import psycopg2 as psy# pip3 install types-psycopg2 import pandas as pd # pip3 install pandas import getpass From 9bb92bf5a5d3e309359a92d8d65b4e93601fc5a5 Mon Sep 17 00:00:00 2001 From: Paul Squizzato Date: Tue, 21 Mar 2023 11:52:44 +0000 Subject: [PATCH 13/17] =?UTF-8?q?Preparation=20=C3=A0=20la=20v=C3=A9rifica?= =?UTF-8?q?tion=20des=20r=C3=A8gles=20dans=20ecritureBDD.py,=20la=20foncti?= =?UTF-8?q?on=20est=20l=C3=A0=20faut=20juste=20l'impl=C3=A9menter,=20bref?= =?UTF-8?q?=20chouette=20:cool:=20:smiley:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decoderBrut.py | 6 ++++++ src/ecritureBDD.py | 13 ++++++++++--- src/triPacket.py | 18 +++++++++++------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/decoderBrut.py b/src/decoderBrut.py index e83211d..9eb65f2 100644 --- a/src/decoderBrut.py +++ b/src/decoderBrut.py @@ -116,5 +116,11 @@ if not db_user: db_password = getpass.getpass('user password : ') connec=[db_host,db_name,db_user,db_password] +# si register 5 = 55 et coil 3 = 1 et coil 12 = 0 : +# ecriture sur le registre 8 à 72 ou coil 9 à 1 impossibles +# if ['r',5,55] and ['c',3,1] and ['c',12,0] and ecriture ['r',8,72]: + #bloquer ecriture +# ecrire registre 5 72,4,4,55,4 + scapy.sniff(iface="lo", prn=decode) diff --git a/src/ecritureBDD.py b/src/ecritureBDD.py index 901fbbe..75c14fb 100644 --- a/src/ecritureBDD.py +++ b/src/ecritureBDD.py @@ -2,13 +2,20 @@ import psycopg2 as psy import pandas as pd import getpass -def ecritureBDD(typeMem,addresse,valeur,connec): +def verifRegle(a): + return True + +def ecritureBDD(lStatus,connec): co = None try: co = psy.connect(host=connec[0],database=connec[1],user=connec[2],password=connec[3]) cur = co.cursor() - cur.execute("INSERT INTO Status VALUES (%s,%s,%s ) ON CONFLICT (addresse,type) DO UPDATE SET valeur=%s;",(addresse,typeMem,valeur,valeur)) - co.commit() + for i in lStatus: + cur.execute("INSERT INTO Status VALUES (%s,%s,%s ) ON CONFLICT (addresse,type) DO UPDATE SET valeur=%s;",(i[1],i[0],i[2],i[2])) + if verifRegle(co): + co.commit() + else: + co.rollback() cur.close() except(Exception,psy.DatabaseError) as error: print(error) diff --git a/src/triPacket.py b/src/triPacket.py index b5309d8..f84d815 100644 --- a/src/triPacket.py +++ b/src/triPacket.py @@ -1,18 +1,22 @@ from ecritureBDD import ecritureBDD -def triPacket(lPkt,connec): +def decoupePacket(lPkt): if type(lPkt)!=list: print('pas liste') return if len(lPkt)==0: - print('vide') + print('liste vide') return if len(lPkt)==1: - triPacket(lPkt[0],connec) - return + return decoupePacket(lPkt[0]) if len(lPkt)==3 and type(lPkt[0])==str: - ecritureBDD(lPkt[0],int(lPkt[1]),int(lPkt[2]),connec) - return + return [[lPkt[0],int(lPkt[1]),int(lPkt[2])]] else: + l=[] for i in lPkt: - triPacket(i,connec) + l+=decoupePacket(i) + return l + +def triPacket(lPkt,connec): + lNettoyee=decoupePacket(lPkt) + ecritureBDD(lNettoyee,connec) From 76944e6695c3bf9f8dff0d01a29e360318055193 Mon Sep 17 00:00:00 2001 From: louwar Date: Tue, 21 Mar 2023 11:53:02 +0100 Subject: [PATCH 14/17] Update CI --- Dockerfile | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 1a43d9c..0000000 --- a/Dockerfile +++ /dev/null @@ -1,17 +0,0 @@ -# Définit l'image de base -FROM python:3.9 - -# Crée le répertoire de travail et le définit comme répertoire courant -WORKDIR /app - -# Copie les fichiers du projet dans le conteneur -COPY . . - -# Installe les dépendances du projet -RUN pip install --no-cache-dir -r requirements.txt - -# Expose le port utilisé par l'application -EXPOSE 8000 - -# Démarre l'application -CMD ["python", "app.py"] From cb8709fb1832fe5416f4719d9a4a3c825e282cce Mon Sep 17 00:00:00 2001 From: louwar Date: Tue, 21 Mar 2023 11:56:10 +0100 Subject: [PATCH 15/17] Update CI --- Dockerfile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f551779 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.9-slim-buster + +WORKDIR /app + +COPY src/ /app/src/ +COPY requirements.txt /app/ +COPY setup.py /app/ + +RUN python -m venv venv +RUN . venv/bin/activate && pip install -r requirements.txt && python setup.py install + +RUN python -m pytest +RUN python setup.py sdist bdist_wheel + +CMD ["python", "app.py"] From 87abffe3647790ff80b37e0e6973ff364f5d0760 Mon Sep 17 00:00:00 2001 From: louwar Date: Tue, 21 Mar 2023 11:56:51 +0100 Subject: [PATCH 16/17] Update CI --- .drone.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/.drone.yml b/.drone.yml index b314fa3..c89715e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -21,16 +21,6 @@ steps: - python -m pytest - python setup.py sdist bdist_wheel - - name: upload - image: python:3.9-slim-buster - volumes: - - name: docs - path: /docs - commands: - - cd Sources/ - - pip install twine - - twine upload dist/* - # docker image build - name: docker-build-and-push From c09fc522df9c2f2ddbf7d983042cc83b78e259f4 Mon Sep 17 00:00:00 2001 From: louwar Date: Tue, 21 Mar 2023 11:59:12 +0100 Subject: [PATCH 17/17] Update CI --- .drone.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index c89715e..50b9d12 100644 --- a/.drone.yml +++ b/.drone.yml @@ -66,10 +66,10 @@ steps: commands: - cd src/ - sonar-scanner \ - -Dsonar.projectKey=MyPythonProject \ - -Dsonar.projectName=My Python Project \ + -Dsonar.projectKey=detection_d_intrusion \ + -Dsonar.projectName=detection_d_intrusion \ -Dsonar.sources=. \ - -Dsonar.host.url=$${PLUGIN_SONAR_HOST} \ + -Dsonar.host.url=https://codefirst.iut.uca.fr/sonar \ -Dsonar.login=$${PLUGIN_SONAR_TOKEN} secrets: [ SONAR_TOKEN ] settings: