Commit d091da2b authored by yogesh.m's avatar yogesh.m

update

parent b5347e84
...@@ -52,8 +52,9 @@ def transmit_all_assets(): ...@@ -52,8 +52,9 @@ def transmit_all_assets():
pass pass
def periodic_transmit_all_assets(): def periodic_transmit_all_assets():
print("here")
while True: while True:
sleep(7) sleep(int(data["configuration"]["transfer frequency"]))
retry=0 retry=0
try: try:
if platform == "linux" or platform == "linux2": if platform == "linux" or platform == "linux2":
......
No preview for this file type
No preview for this file type
configuration: configuration:
platform: OT
asset_discovery: False #To enable disable the scan
threads: 20 #number of threads to scan at once threads: 20 #number of threads to scan at once
discovery frequency: 1800 minutes #how frequent the scan needs to be transfer frequency: 1 #how frequent the scan needs to be
scan_protocol: ethernetip_enum,sip_enum #types of protocol to use unifytwin_server_ip_address: https://webhook.site/244b0b77-e8f2-4a5b-9fb9-fd7f0e52e692 #Data will be sent back here
ot_ports : [80,102,443,502,530,593,789,1089,1090,1091,1911,1962,2222,2404,4000,4840,4843,4911,9600,19999,20000,20547,34962,34963,34964,34980,44818,46823,46824,55000,55001,55002,55003] #adding all OT ports to check
packet_storage: local #where to store packets local/remote(server)
unifytwin_server_ip_address: https://webhook.site/d3338504-e5f0-4c2b-ad21-ae7923119f16 #Data will be sent back here
asset_range_start: 10.10.3.214 #where to start the scan from
asset _range_end: 10.10.3.225 #where to end the scan from
edge_device_location : Dalmia Cement edge_device_location : Dalmia Cement
port_monitoring: True #On/Off
pcap_analysis: False
null_loopback: False null_loopback: False
interfaces: Wi-Fi interfaces: Wi-Fi
blacklist_ip: ['46.4.105.116','172.67.214.157','3.6.115.64','104.21.53.154'] blacklist_ip: ['46.4.105.116','172.67.214.157','3.6.115.64','104.21.53.154']
blacklist_dns: ['webhook.site.','hpd.gasmi.net.','dd01-14-98-12-178.in.ngrok.io.'] blacklist_dns: ['webhook.site.']
ui_host: 127.0.0.1
ui_port: 8081
\ No newline at end of file
import binascii import binascii
import json import json
class Packet_Analyzer(): class Packet_Analyzer():
def __init__(self): def __init__(self):
data=None data=None
...@@ -15,7 +18,7 @@ class Packet_Analyzer(): ...@@ -15,7 +18,7 @@ class Packet_Analyzer():
if ((b'8000' in hex_pkt or b'c000' in hex_pkt) and (hex_pkt[84:88] == b'8000' or hex_pkt[84:88] == b'c000')): if ((b'8000' in hex_pkt or b'c000' in hex_pkt) and (hex_pkt[84:88] == b'8000' or hex_pkt[84:88] == b'c000')):
print("omron found") print("omron found")
protocols = protocols + "omron:" protocols = protocols + "omron:"
if (b'0000' in hex_pkt and hex_pkt[84:88] == b'0000'): if (b'0000' in hex_pkt and hex_pkt[108:112] == b'0000'):
protocols = protocols + "modbus:" protocols = protocols + "modbus:"
if(hex_pkt[46:48] == b"06"): if(hex_pkt[46:48] == b"06"):
protocols=protocols+"tcp" protocols=protocols+"tcp"
......
...@@ -7,55 +7,72 @@ from protocol_enumerators import omron ...@@ -7,55 +7,72 @@ from protocol_enumerators import omron
import binascii import binascii
import os import os
from helpers.Packet_Analyzer import Packet_Analyzer from helpers.Packet_Analyzer import Packet_Analyzer
import sys
def analyse_protocol(protocols,pkt): def analyse_protocol(protocols,pkt):
dev_type="Unknown" try:
vendor="Unknown" dev_type = "Unknown"
firmware="Unknown" vendor = "Unknown"
model="Unknown" firmware = "Unknown"
model = "Unknown"
inhex = binascii.hexlify(bytes(pkt)) inhex = binascii.hexlify(bytes(pkt))
pa=Packet_Analyzer() pa = Packet_Analyzer()
if("enip" in protocols): if ("enip" in protocols):
print(protocols) print(protocols)
res=eip.get_info(pa.get_ip(inhex),int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(pa.get_udp_port(inhex))) res = eip.get_info(pa.get_ip(inhex),
if(res): int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(pa.get_udp_port(inhex)))
dev_type=res['Type'] if (res):
vendor=res['Vendor'] dev_type = res['Type']
firmware=res['ProductName'] vendor = res['Vendor']
model=res['SerialNumber'] firmware = res['ProductName']
elif("s7comm" in protocols): model = res['SerialNumber']
res=s7.get_info(eip.get_info(pa.get_ip(inhex) ,int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(pa.get_udp_port(inhex)))) elif ("s7comm" in protocols):
if(res): res = s7.get_info(eip.get_info(pa.get_ip(inhex), int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(
dev_type=res['Module Type']+" "+res['System Name'] pa.get_udp_port(inhex))))
vendor=res['Copyright'] if (res):
firmware=res['Module']+" "+res['Version'] dev_type = res['Module Type'] + " " + res['System Name']
model=res['Serial Number'] vendor = res['Copyright']
elif("bacnet" in protocols): firmware = res['Module'] + " " + res['Version']
res=bac.get_info(eip.get_info(pa.get_ip(inhex) ,int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(pa.get_udp_port(inhex)))) model = res['Serial Number']
if(res): elif ("bacnet" in protocols):
dev_type=res['desc'] res = bac.get_info(eip.get_info(pa.get_ip(inhex), int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(
vendor=res['vendorid'] pa.get_udp_port(inhex))))
firmware=res['firmware'] if (res):
model=res['model'] dev_type = res['desc']
elif("modbus" in protocols): vendor = res['vendorid']
res=modbus.get_info(eip.get_info(pa.get_ip(inhex) ,int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(pa.get_udp_port(inhex))),False) firmware = res['firmware']
if(res): model = res['model']
dev_type=res['Device identification'] elif ("modbus" in protocols):
vendor=res['Slave ID data'] res = modbus.get_info(eip.get_info(pa.get_ip(inhex), int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(
model=res['sid'] pa.get_udp_port(inhex))), False)
elif("omron" in protocols): print(inhex)
res=omron.get_info(eip.get_info(pa.get_ip(inhex) ,int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(pa.get_udp_port(inhex))),False) if (res):
if(res): dev_type = res['Device identification']
dev_type='Omron Device' vendor = res['Slave ID data']
vendor='Omron Devices' model = res['sid']
firmware=res['Controller Version'] elif ("omron" in protocols):
model=res['Controller Model'] res = omron.get_info(eip.get_info(pa.get_ip(inhex), int(pa.get_tcp_port(inhex)) if "tcp" in protocols else int(
pa.get_udp_port(inhex))), False)
if (res):
dev_type = 'Omron Device'
vendor = 'Omron Devices'
firmware = res['Controller Version']
model = res['Controller Model']
else: else:
dev_type=psdata[str(pa.get_tcp_port(inhex))] if "tcp" in protocols else psdata[str(pa.get_udp_port(inhex))] if "udp" in protocols else "Unknown" port_no = str(pa.get_tcp_port(inhex)) if "tcp" in protocols else str(
vendor=pa.get_vendor(pa.get_mac(inhex)) pa.get_udp_port(inhex)) if "udp" in protocols else "Unknown"
return dev_type,vendor,firmware,model dev_type = psdata[port_no] if port_no in psdata else "Unknown"
vendor = pa.get_vendor(pa.get_mac(inhex))
return dev_type, vendor, firmware, model
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno, e)
def update_protocol(protocols,pkt): def update_protocol(protocols,pkt):
try:
dev_type="Unknown" dev_type="Unknown"
vendor="Unknown" vendor="Unknown"
firmware="Unknown" firmware="Unknown"
...@@ -97,3 +114,7 @@ def update_protocol(protocols,pkt): ...@@ -97,3 +114,7 @@ def update_protocol(protocols,pkt):
firmware=res['Controller Version'] firmware=res['Controller Version']
model=res['Controller Model'] model=res['Controller Model']
return dev_type,vendor,firmware,model return dev_type,vendor,firmware,model
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno, e)
\ No newline at end of file
from flask import Flask, request, render_template from flask import Flask, request, render_template
import asset_actions
import json import json
import os
import sniff import sniff
import multiprocessing import multiprocessing
from utilities.list_interfaces import get_interfaces from utilities.list_interfaces import get_interfaces
import asset_actions
import yaml
app = Flask(__name__, template_folder='template') app = Flask(__name__, template_folder='template')
status="Offline" status="Offline"
transfer_status="not transmitting"
process=None
periodic_transfer=None
try:
yamlfile=open("config.yaml")
data = yaml.load(yamlfile, Loader=yaml.FullLoader)
ui_host=data["configuration"]["ui_host"]
ui_port=int(data["configuration"]["ui_port"])
except Exception as e:
ui_host="127.0.0.1"
ui_port=8080
@app.route('/') @app.route('/')
# ‘/’ URL is bound with hello_world() function.
def hello_world(): def hello_world():
global status global status
global transfer_status
headings = ["name", "last_activity", "type", "protocols", "mac_addr", "vendor", "firmware_version", "model", "os", headings = ["name", "last_activity", "type", "protocols", "mac_addr", "vendor", "firmware_version", "model", "os",
"plant"] "plant"]
try: try:
data = json.loads(asset_actions.transmit_all_assets()) data = json.loads(asset_actions.transmit_all_assets())
except: except:
data = {} data = {}
return render_template("index.html", headings=headings, data=data, interfaces=get_interfaces(),status=status) return render_template("index.html", headings=headings, data=data, interfaces=get_interfaces(),status=status,transfer_status=transfer_status)
process=None
@app.route('/start_sniff',methods = ['POST', 'GET']) @app.route('/start_sniff',methods = ['POST', 'GET'])
def start_sniff(): def start_sniff():
global process global process
...@@ -51,13 +63,6 @@ def stop_sniff(): ...@@ -51,13 +63,6 @@ def stop_sniff():
@app.route('/refresh',methods = ['POST', 'GET']) @app.route('/refresh',methods = ['POST', 'GET'])
def refresh(): def refresh():
# headings = ["name", "last_activity", "type", "protocols", "mac_addr", "vendor", "firmware_version", "model", "os",
# "plant"]
# try:
# data = json.loads(asset_actions.transmit_all_assets())
# except:
# data = {}
# return render_template("index.html", headings=headings, data=data, interfaces=get_interfaces())
return hello_world() return hello_world()
@app.route('/delete_devices',methods = ['POST', 'GET']) @app.route('/delete_devices',methods = ['POST', 'GET'])
...@@ -65,6 +70,32 @@ def delete(): ...@@ -65,6 +70,32 @@ def delete():
asset_actions.delete_all_assets() asset_actions.delete_all_assets()
return hello_world() return hello_world()
# main driver function @app.route('/start_transfer',methods = ['POST', 'GET'])
def transfer_data():
global transfer_status
global periodic_transfer
transfer_status="transmitting"
periodic_transfer = multiprocessing.Process(target=asset_actions.periodic_transmit_all_assets, args=())
if(periodic_transfer.is_alive()):
periodic_transfer.kill()
periodic_transfer.start()
else:
periodic_transfer.start()
return hello_world()
@app.route('/stop_transfer',methods = ['POST', 'GET'])
def stop_transfer_data():
global transfer_status
global periodic_transfer
if (periodic_transfer):
if (periodic_transfer.is_alive()):
periodic_transfer.kill()
transfer_status = "not transmitting"
else:
transfer_status = "not transmitting"
else:
transfer_status = "not transmitting"
return hello_world()
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=8080) app.run(debug=True,host=ui_host, port=ui_port)
...@@ -18,7 +18,6 @@ try: ...@@ -18,7 +18,6 @@ try:
yamlfile=open("config.yaml") yamlfile=open("config.yaml")
data = yaml.load(yamlfile, Loader=yaml.FullLoader) data = yaml.load(yamlfile, Loader=yaml.FullLoader)
server_ip=data["configuration"]["unifytwin_server_ip_address"] server_ip=data["configuration"]["unifytwin_server_ip_address"]
packet_storage=data["configuration"]["packet_storage"]
plant=data["configuration"]["edge_device_location"] plant=data["configuration"]["edge_device_location"]
balacklist_ips=data["configuration"]["blacklist_ip"] balacklist_ips=data["configuration"]["blacklist_ip"]
blacklist_dns=data["configuration"]["blacklist_dns"] blacklist_dns=data["configuration"]["blacklist_dns"]
...@@ -27,7 +26,6 @@ try: ...@@ -27,7 +26,6 @@ try:
null_loopback=data["configuration"]["null_loopback"] null_loopback=data["configuration"]["null_loopback"]
except Exception as e: except Exception as e:
server_ip="" server_ip=""
packet_storage="local"
plant="Unknown Location" plant="Unknown Location"
balacklist_ips=['46.4.105.116','172.67.214.157','3.6.115.64','104.21.53.154'] balacklist_ips=['46.4.105.116','172.67.214.157','3.6.115.64','104.21.53.154']
blacklist_dns=['webhook.site.','hpd.gasmi.net.','dd01-14-98-12-178.in.ngrok.io.'] blacklist_dns=['webhook.site.','hpd.gasmi.net.','dd01-14-98-12-178.in.ngrok.io.']
...@@ -55,9 +53,7 @@ def convert_text(pkt): ...@@ -55,9 +53,7 @@ def convert_text(pkt):
try: try:
pa = Packet_Analyzer() pa = Packet_Analyzer()
protocols=pa.identify_protocol(inhex) protocols=pa.identify_protocol(inhex)
print(protocols)
ip=pa.get_ip(inhex) ip=pa.get_ip(inhex)
print(ip)
if(ip): if(ip):
mac=pa.get_mac(inhex) mac=pa.get_mac(inhex)
check_exist=query.execute("SELECT ip_address FROM inventory WHERE ip_address='"+ip+"'").fetchone() check_exist=query.execute("SELECT ip_address FROM inventory WHERE ip_address='"+ip+"'").fetchone()
...@@ -92,12 +88,18 @@ def convert_text(pkt): ...@@ -92,12 +88,18 @@ def convert_text(pkt):
query.close() query.close()
mydb.commit() mydb.commit()
except sqlite3.OperationalError as e: except sqlite3.OperationalError as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno,e)
query.close() query.close()
if(not mydb.in_transaction): if(not mydb.in_transaction):
mydb.rollback() mydb.rollback()
mydb.commit() mydb.commit()
pass pass
except Exception as e: except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno,e)
pass pass
def gasmi_api(pkt): def gasmi_api(pkt):
...@@ -123,18 +125,8 @@ def network_sniffing_local_storage(pkt): ...@@ -123,18 +125,8 @@ def network_sniffing_local_storage(pkt):
ti=threading.Thread(target=gasmi_api, args=pkt,) ti=threading.Thread(target=gasmi_api, args=pkt,)
ti.start() ti.start()
def network_sniffing_remote_storage(pkt):
if(threading.active_count()<configured_threads):
ti=threading.Thread(target=packet_transmit, args=pkt,)
ti.start()
def start_sniff(iface=interfaces): def start_sniff(iface=interfaces):
if(packet_storage=="local"):
# ts=threading.Thread(target=asset_actions.periodic_transmit_all_assets)
# ts.start()
sniff(iface=iface,prn=network_sniffing_local_storage) sniff(iface=iface,prn=network_sniffing_local_storage)
else:
sniff(iface=iface,prn=network_sniffing_remote_storage)
if __name__=="__main__": if __name__=="__main__":
......
...@@ -17,10 +17,13 @@ ...@@ -17,10 +17,13 @@
<br><br> <br><br>
<p><input type = "submit" value = "Start" /></p> <p><input type = "submit" value = "Start" /></p>
</form> </form>
<h5>Status :{{status}} </h5> <h5>Status :{{status}} </h5>
<a href="/stop_sniff" >Stop</a> <a href="/stop_sniff" >Stop</a>
<a href="/refresh">Refresh</a> <a href="/refresh">Refresh</a>
<a href="/delete_devices">Delete</a> <a href="/delete_devices">Delete</a>
<h5>Transfer : {{transfer_status}} </h5>
<a href="/start_transfer" >Start Transfer</a><a href="/stop_transfer" >Stop Transfer</a></p>
<div> <div>
<table> <table>
<tr> <tr>
......
import json from scapy.all import *
with open('test.json') as f: def packet_has_modbus_layer(packet):
s = json.dumps(f.read()) # Check if the packet has the TCP layer and destination port is 502
print(s.__len__()) if TCP in packet and packet[TCP].dport == 502:
\ No newline at end of file # Check if the packet has the Modbus layer
if packet.haslayer(TCP):
return True
return False
def network_sniffing_local_storage(pkt):
if(packet_has_modbus_layer(pkt)):
print("yes")
sniff(prn=network_sniffing_local_storage)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment