adding export

main
root 2023-11-09 11:11:19 +00:00
parent 73597bcd6a
commit 8d0dce1267
3 changed files with 42 additions and 21 deletions

33
app.py
View File

@ -4,7 +4,6 @@ import sys
import time import time
import socket import socket
import iperf3 import iperf3
import logging
import requests import requests
import subprocess import subprocess
import scapy.all as scapy import scapy.all as scapy
@ -14,12 +13,6 @@ from flask_navigation import Navigation
from flask_socketio import SocketIO, emit from flask_socketio import SocketIO, emit
from flask import Flask, render_template, Response, request, redirect, url_for, session, escape from flask import Flask, render_template, Response, request, redirect, url_for, session, escape
#Global config
__version__ = "1.0.0"
path = os.path.dirname(__file__)
logging.basicConfig(filename=path+'/log/Pyng.log', level=logging.DEBUG, format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
logging.info('PROGRAM START')
#Init #Init
app = Flask(__name__) app = Flask(__name__)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
@ -30,6 +23,7 @@ nav = Navigation(app)
#Variable #Variable
uphost=0 uphost=0
export_list=[]
#Menu #Menu
nav.Bar('top', [ nav.Bar('top', [
nav.Item('Network', 'index'), nav.Item('Network', 'index'),
@ -100,6 +94,7 @@ def bg_scan_port(target):
#Background network scan #Background network scan
def bg_scan_network(subnet): def bg_scan_network(subnet):
global export_list
global uphost global uphost
uphost=0 uphost=0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -109,18 +104,18 @@ def bg_scan_network(subnet):
broadcast.dst = 'ff:ff:ff:ff:ff:ff' broadcast.dst = 'ff:ff:ff:ff:ff:ff'
request_broadcast = broadcast / request request_broadcast = broadcast / request
clients = scapy.srp(request_broadcast, timeout = 1)[0] clients = scapy.srp(request_broadcast, timeout = 1)[0]
export_list=['IP,MAC,VENDOR,DNS']
for element in clients: for element in clients:
uphost=uphost+1 uphost=uphost+1
macdet=get_mac_details(element[1].hwsrc) macdet=get_mac_details(element[1].hwsrc)
dns=dns_resolv(element[1].psrc) dns=dns_resolv(element[1].psrc)
export_list.append(element[1].psrc+','+element[1].hwsrc+','+macdet.replace(",", " ")+','+dns)
socketio.emit('net_response',{'ip': element[1].psrc,'mac': element[1].hwsrc,'vendor':macdet,'dns':dns}) socketio.emit('net_response',{'ip': element[1].psrc,'mac': element[1].hwsrc,'vendor':macdet,'dns':dns})
sock.close() sock.close()
#Main route to index. #Main route to index.
@app.route("/") @app.route("/")
def index(): def index():
app.logger.info('Info level log')
app.logger.warning('Warning level log')
if 'username' in session: if 'username' in session:
return render_template('index.html') return render_template('index.html')
return render_template('login.html',title="Welcome") return render_template('login.html',title="Welcome")
@ -156,11 +151,24 @@ def connect(data):
@socketio.on('net_port') @socketio.on('net_port')
def connect(data): def connect(data):
global uphost global uphost
global export_list
emit('net_response', {'ip':'IP','mac':'Mac address','vendor':'Vendor resolution','dns':'Local DNS'}) emit('net_response', {'ip':'IP','mac':'Mac address','vendor':'Vendor resolution','dns':'Local DNS'})
socketio.start_background_task(bg_scan_network(data)) socketio.start_background_task(bg_scan_network(data))
downhost=256-uphost downhost=256-uphost
emit('net_response', {'ip':' ','mac':'Up:'+str(uphost),'vendor': 'Down:'+str(downhost),'dns':' '}) print(export_list)
emit('net_response', {'ip':' ','mac':'Up:'+str(uphost),'vendor': 'Down:'+str(downhost),'dns':''})
#Export
@app.route("/export", methods=['GET', 'POST'])
def export_file():
global export_list
csv=""
for line in export_list:
csv = csv+'\n'+line
return Response(
csv,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=CMDB.csv"})
##Login ##Login
@app.route("/login", methods=['GET', 'POST']) @app.route("/login", methods=['GET', 'POST'])
def login(): def login():
@ -169,7 +177,6 @@ def login():
password = request.form['password'] password = request.form['password']
if authenticate(str(username), str(password)): if authenticate(str(username), str(password)):
session['username'] = request.form['username'] session['username'] = request.form['username']
logging.info('AUTH LOG - New auth from '+username+' !')
return redirect(url_for('index')) return redirect(url_for('index'))
else: else:
resultats='<p id="alert" style="color:red">Invalid Username/Password ! </p>' resultats='<p id="alert" style="color:red">Invalid Username/Password ! </p>'
@ -182,4 +189,4 @@ def logout():
##Start ##Start
if __name__ == '__main__': if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port='5005', allow_unsafe_werkzeug=True) socketio.run(app, host='0.0.0.0', port='5005')

View File

@ -4,8 +4,8 @@ body {
font-family: helvetica, arial, verdana, sans-serif; font-family: helvetica, arial, verdana, sans-serif;
text-align:center; text-align:center;
min-height: 100% !important; min-height: 100% !important;
background: #333; background: white;
color: white; color: black;
} }
::placeholder { ::placeholder {
@ -87,7 +87,7 @@ span {
background-color: #BB8FCE; background-color: #BB8FCE;
} }
#output { #output {
border-radius: 15px; // border-radius: 15px;
box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.1); box-shadow: 0px 0px 9px 0px rgba(0,0,0,0.1);
width: fit-content; width: fit-content;
background-color:white; background-color:white;
@ -110,13 +110,13 @@ tr:not(:first-child):hover {
background-color: rgba(114, 114, 113, 0.2); background-color: rgba(114, 114, 113, 0.2);
} }
input { input {
padding: 6px; padding: 15px;
font-size: 14px; font-size: 14px;
border-width: 0px; border-width: 0px;
border-color: #CCCCCC; border-color: #CCCCCC;
background-color: #FFFFFF; background-color: #FFFFFF;
color: #000000; color: #333;
border-style: solid; border-style: solid;
border-radius: 9px; border-radius: 5px;
box-shadow: 0px 0px 5px rgba(66,66,66,.75); box-shadow: 0px 0px 2px rgba(66,66,66,.75);
} }

View File

@ -32,6 +32,19 @@
return false; return false;
}); });
const outputDiv = document.querySelector('#output');
const autreDiv = document.querySelector('#export_list');
if (outputDiv.innerHTML.trim() === '') {
autreDiv.style.display = 'none';
}
outputDiv.addEventListener('DOMSubtreeModified', function () {
if (outputDiv.innerHTML.trim() === '') {
autreDiv.style.display = 'none';
} else {
autreDiv.style.display = 'block';
}
});
}); });
</script> </script>
</head> </head>
@ -58,7 +71,8 @@
</form> </form>
<center> <center>
<br><br><div id="output"></div> <br><br><div id="export_list"><a href="./export">Export List</a></div>
<br><div id="output"></div>
</center> </center>
</body> </body>
</html> </html>