This commit is contained in:
Jokob-sk
2022-12-28 22:02:42 +11:00
parent 29131e39d2
commit 89ffaff64d
5 changed files with 314 additions and 91 deletions

View File

@@ -510,11 +510,12 @@ def main ():
sql_connection = None
sql = None
# create log files
write_file(logPath + 'IP_changes.log', '')
write_file(logPath + 'stdout.log', '')
write_file(logPath + 'stderr.log', '')
write_file(logPath + 'pialert.log', '')
# # create log files
# write_file(logPath + 'IP_changes.log', '')
# write_file(logPath + 'stdout.log', '')
# write_file(logPath + 'stderr.log', '')
# write_file(logPath + 'pialert.log', '')
# write_file(logPath + 'pialert_pholus.log', '')
# Upgrade DB if needed
upgradeDB()
@@ -1102,9 +1103,6 @@ def execute_arpscan_on_interface (SCAN_SUBNETS):
file_print(e.output)
result = ""
if PHOLUS_ACTIVE:
performPholusScan(interface, mask)
return result
#-------------------------------------------------------------------------------
@@ -1577,16 +1575,53 @@ def update_devices_names ():
# Devices without name
file_print(' Trying to resolve devices without name')
# BUGFIX #97 - Updating name of Devices w/o IP
for device in sql.execute ("SELECT * FROM Devices WHERE dev_Name IN ('(unknown)','') AND dev_LastIP <> '-'") :
# Resolve device name
newName = resolve_device_name (device['dev_MAC'], device['dev_LastIP']) #<<<< continue here TODO DEV > get >latest< Pholus scan results and match
sql.execute ("SELECT * FROM Devices WHERE dev_Name IN ('(unknown)','') AND dev_LastIP <> '-'")
unknownDevices = sql.fetchall()
# perform Pholus scan if (unknown) devices found
if len(unknownDevices) > 0 and PHOLUS_ACTIVE:
subnetList = []
# handle old strin setting
if type(SCAN_SUBNETS) is not list:
subnetList.append(SCAN_SUBNETS)
else:
subnetList = SCAN_SUBNETS
# scan every interface
for subnet in subnetList:
temp = subnet.strip().split()
mask = temp[0]
interface = temp[1].split('=')[1]
file_print(">>> Pholus scan on: ", interface,mask)
performPholusScan(interface, mask)
# get names from Pholus scan
sql.execute ('SELECT * FROM Pholus_Scan where "MAC" in (select "dev_MAC" from Devices where "dev_Name" IN ("(unknown)","")) and "Record_Type"="Answer"')
pholusResults = sql.fetchall()
file_print("pholusResults: ", len(pholusResults))
for device in unknownDevices:
# Resolve device name OLD
newName = resolve_device_name (device['dev_MAC'], device['dev_LastIP'])
# Resolve with Pholus scan results
if newName == -1:
newName = resolve_device_name_pholus (device['dev_MAC'], device['dev_LastIP'], pholusResults)
if newName == -1 :
notFound += 1
elif newName == -2 :
ignored += 1
else :
recordsToUpdate.append ([newName, device['dev_MAC']])
# else :
# recordsToUpdate.append ([newName, device['dev_MAC']])
recordsToUpdate.append (["(name not found)", device['dev_MAC']])
# Print log
file_print(" Names updated: ", len(recordsToUpdate) )
@@ -1618,14 +1653,18 @@ def performPholusScan (interface, mask):
if output != "":
file_print('[', timeNow(), '] Scan: Pholus SUCCESS')
write_file (logPath + '/pialert_pholus.log', output)
write_file (logPath + '/pialert_pholus_old.log', output)
for line in output.split("\n"):
append_line_to_file (logPath + '/pialert_pholus.log', line +'\n')
params = []
for line in output.split("\n"):
columns = line.split("|")
if len(columns) == 4:
params.append(( interface + " " + mask, timeNow() , columns[0], columns[1], columns[2], columns[3], ''))
params.append(( interface + " " + mask, timeNow() , columns[0].replace(" ", ""), columns[1].replace(" ", ""), columns[2].replace(" ", ""), columns[3], ''))
if len(params) > 0:
openDB ()
@@ -1636,6 +1675,17 @@ def performPholusScan (interface, mask):
#-------------------------------------------------------------------------------
def resolve_device_name_pholus (pMAC, pIP, pholusResults):
newName = -1
for result in pholusResults:
if pholusResults["MAC"] == pMAC:
return pholusResults["Value"]
return newName
#-------------------------------------------------------------------------------
def resolve_device_name (pMAC, pIP):
try :
pMACstr = str(pMAC)
@@ -2663,11 +2713,11 @@ def upgradeDB ():
AND name='Pholus_Scan';
""").fetchone() == None
# Re-creating Pholus_Scan table
file_print("[upgradeDB] Re-creating Pholus_Scan table")
# if pholusScanMissing == False:
# # Re-creating Pholus_Scan table
# file_print("[upgradeDB] Re-creating Pholus_Scan table")
# sql.execute("DROP TABLE Pholus_Scan;")
# pholusScanMissing = True
if pholusScanMissing:
sql.execute("""
@@ -2792,7 +2842,7 @@ def get_device_stats():
# columns = ["online","down","all","archived","new","unknown"]
sql.execute("""
SELECT Online_Devices as online, Down_Devices as down, All_Devices as 'all', Archived_Devices as archived, (select count(*) from Devices a where dev_NewDevice = 1 ) as new, (select count(*) from Devices a where dev_Name = '(unknown)' ) as unknown from Online_History order by Scan_Date desc limit 1
SELECT Online_Devices as online, Down_Devices as down, All_Devices as 'all', Archived_Devices as archived, (select count(*) from Devices a where dev_NewDevice = 1 ) as new, (select count(*) from Devices a where dev_Name = '(unknown)' or dev_Name = '(unresolved)' ) as unknown from Online_History order by Scan_Date desc limit 1
""")
row = sql.fetchone()

View File

@@ -115,6 +115,7 @@ if ($_REQUEST['mac'] == 'Internet') { $DevDetail_Tap_temp = "Tools"; } else { $D
<li> <a id="tabSessions" href="#panSessions" data-toggle="tab"> <?php echo lang('DevDetail_Tab_Sessions');?> </a></li>
<li> <a id="tabPresence" href="#panPresence" data-toggle="tab"> <?php echo lang('DevDetail_Tab_Presence');?> </a></li>
<li> <a id="tabEvents" href="#panEvents" data-toggle="tab"> <?php echo lang('DevDetail_Tab_Events');?> </a></li>
<li> <a id="tabPholus" href="#panPholus" data-toggle="tab"> <?php echo lang('DevDetail_Tab_Pholus');?> </a></li>
<div class="btn-group pull-right">
<button type="button" class="btn btn-default" style="padding: 10px; min-width: 30px;"
@@ -586,6 +587,26 @@ if ($_REQUEST['mac'] == 'Internet') {
</thead>
</table>
</div>
<!-- tab page 6 ------------------------------------------------------------ -->
<div class="tab-pane fade table-responsive" id="panPholus">
<!-- Datatable Events -->
<table id="tablePholus" class="table table-bordered table-hover table-striped ">
<thead>
<tr>
<th>Index</th>
<th>Info</th>
<th>Time</th>
<th>IP</th>
<th>Entry Type</th>
<th>Value</th>
<th>Extra</th>
</tr>
</thead>
<tbody id="tablePholusBody">
<tr id="tablePholusPlc" class="text-center"><td colspan='7'><span>Nothing sniffed out with Polus for this device.</span></td></tr>
</tbody>
</table>
</div>
</div>
<!-- /.tab-content -->
@@ -637,7 +658,15 @@ if ($ENABLED_DARKMODE === True) {
<!-- page script ----------------------------------------------------------- -->
<script>
var mac = '';
function getMac(){
params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});
return params.mac
}
mac = getMac()
var devicesList = [];
var pos = -1;
var parPeriod = 'Front_Details_Period';
@@ -651,6 +680,8 @@ if ($ENABLED_DARKMODE === True) {
var eventsRows = 10;
var eventsHide = true;
var skipRepeatedItems = ['0 h (notify all events)', '1 h', '8 h', '24 h', '168 h (one week)'];
var selectedTab = 'tabDetails';
var emptyArr = ['undefined', "", undefined, null];
// Read parameters & Initialize components
main();
@@ -666,6 +697,16 @@ function main () {
$('#pageTitle').html ('Device not found');
}
key ="activeDevicesTab"
// Activate panel
if(!emptyArr.includes(getCache(key)))
{
selectedTab = getCache(key);
}
tab = selectedTab;
// get parameter value
$.get('php/server/parameters.php?action=get&parameter='+ parPeriod, function(data) {
var result = JSON.parse(data);
@@ -674,13 +715,6 @@ function main () {
$('#period').val(period);
}
// get parameter value
$.get('php/server/parameters.php?action=get&parameter='+ parTab, function(data) {
var result = JSON.parse(data);
if (result) {
tab = result;
}
// get parameter value
$.get('php/server/parameters.php?action=get&parameter='+ parSessionsRows, function(data) {
var result = JSON.parse(data);
@@ -740,7 +774,7 @@ function main () {
});
});
});
});
});
}
@@ -752,14 +786,6 @@ function initializeTabs () {
// Activate panel
$('.nav-tabs a[id='+ tab +']').tab('show');
// Not necessary if first panel is not active
// // Force show first panel
// var panel = $('.nav-tabs a[id='+ tab +']').attr('href');
// panel = panel.substring(1);
// var element = $('#'+panel)[0];
// element.classList.add('in');
// element.classList.add('active');
// When changed save new current tab
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
setParameter (parTab, $(e.target).attr('id'));
@@ -1360,6 +1386,11 @@ function nextRecord () {
return;
}
// get new mac
mac = params.mac;
// reload current tab
reloadTab()
// Next Record
if (pos < (devicesList.length-1) ) {
pos++;
@@ -1558,4 +1589,100 @@ function setTextValue (textElement, textValue) {
activateSaveRestoreData ();
}
// -----------------------------------------------------------------------------
function initializeTabsNew () {
key ="activeDevicesTab"
// Activate panel
if(!emptyArr.includes(getCache(key)))
{
selectedTab = getCache(key);
}
$('.nav-tabs a[id='+ selectedTab +']').tab('show');
// When changed save new current tab
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
setCache(key, $(e.target).attr('id'))
});
// events on tab change
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var target = $(e.target).attr("href") // activated tab
// load tab data only when needed (tab change)
if(target == "#panPholus")
{
loadPholus();
}
});
}
function loadPholus()
{
console.log(mac)
console.log('php/server/devices.php?action=getPholus&mac='+ mac)
$.get('php/server/devices.php?action=getPholus&mac='+ mac, function(data) {
data = sanitize(data);
if(data != "false" && $.trim(data) != [])
{
var listData = JSON.parse(data);
var order = 1;
// console.log(listData)
// console.log(listData[0].MAC)
tableRows = "";
// for each item
listData.forEach(function (item, index) {
tableRows += '<tr class="deviceSpecific"><td>'+item.Index+'</td><td>'+item.Info+'</td><td>'+item.Time+'</td><td>'+item.IP_v4_or_v6+'</td><td>'+item.Record_Type+'</td><td>'+item.Value+'</td><td>'+item.Extra+'</td></tr>';
});
$("#tablePholusBody").html($("#tablePholusBody").html()+tableRows);
// $("#tablePholusPlc").attr("style", "display:none");
$("#tablePholusPlc").hide();
}
else
{
console.log("else")
$("#tablePholusPlc").show();
$(".deviceSpecific").remove();
}
});
}
window.onload = function async()
{
initializeTabsNew();
reloadTab();
}
function reloadTab()
{
// Get the value of "some_key" in eg "https://example.com/?some_key=some_value"
mac = getMac(); // "some_value"
// console.log("aaAAAAAAAAAaa:"+my_mac)
// load tab data only when needed (tab change)
if(getCache("activeDevicesTab") == "tabPholus")
{
console.log("herea")
loadPholus();
}
}
function sanitize(data)
{
return data.replace(/(\r\n|\n|\r)/gm,"").replace(/[^\x00-\x7F]/g, "")
}
</script>

View File

@@ -58,6 +58,7 @@
case 'getDeviceTypes': getDeviceTypes(); break;
case 'getGroups': getGroups(); break;
case 'getLocations': getLocations(); break;
case 'getPholus': getPholus(); break;
default: logServerConsole ('Action: '. $action); break;
}
@@ -900,6 +901,49 @@ function getLocations() {
echo (json_encode ($tableData));
}
//------------------------------------------------------------------------------
// Query the List of locations
//------------------------------------------------------------------------------
function getPholus() {
global $db;
// SQL
$mac = $_REQUEST['mac'];
if (false === filter_var($mac , FILTER_VALIDATE_MAC)) {
throw new Exception('Invalid mac address');
}
else{
$sql = 'SELECT * from Pholus_Scan where MAC ="'.$mac.'"';
// array
$tableData = array();
// execute query
$result = $db->query($sql);
while ($row = $result -> fetchArray (SQLITE3_ASSOC)){
// Push row data
$tableData[] = array( 'Index' => $row['Index'],
'Info' => $row['Info'],
'Time' => $row['Time'],
'MAC' => $row['MAC'],
'IP_v4_or_v6' => $row['IP_v4_or_v6'],
'Record_Type' => $row['Record_Type'],
'Value' => $row['Value'],
'Extra' => $row['Extra']);
}
$db->close();
if(count($tableData) == 0)
{
echo "false";
} else{
// Return json
echo (json_encode ($tableData));
}
}
}
//------------------------------------------------------------------------------
// Status Where conditions

View File

@@ -98,11 +98,12 @@ if ($ENABLED_DARKMODE === True) {
setTimeout("show_pia_servertime()", 1000);
}
document.addEventListener("visibilitychange",()=>{
if(document.visibilityState==="visible"){
window.location.href = window.location.href.split('#')[0];
}
})
// refresh page on focus - adds a lot of SQL queries overhead onto the DB - disabling for now
// document.addEventListener("visibilitychange",()=>{
// if(document.visibilityState==="visible"){
// window.location.href = window.location.href.split('#')[0];
// }
// })
</script>

View File

@@ -152,6 +152,7 @@ $lang['en_us'] = array(
'DevDetail_Tab_Sessions' => 'Sessions',
'DevDetail_Tab_Presence' => 'Presence',
'DevDetail_Tab_Events' => 'Events',
'DevDetail_Tab_Pholus' => 'Pholus',
'DevDetail_MainInfo_Title' => 'Main Info',
'DevDetail_MainInfo_mac' => 'MAC',
'DevDetail_MainInfo_Name' => 'Name',
@@ -551,7 +552,7 @@ the scan will take hours to complete instead of seconds.
'PHOLUS_ACTIVE_name' => 'Enable Pholus scan',
'PHOLUS_ACTIVE_description' => '<a href="https://github.com/jokob-sk/Pi.Alert/tree/main/pholus" target="_blank" >Pholus</a> is a sniffing tool to discover additional information about the devices on the network, including the device name. Please be aware it can spam the network with unnecessary traffic.',
'PHOLUS_TIMEOUT_name' => 'Pholus timeout',
'PHOLUS_TIMEOUT_description' => 'How long (s) should Pholus be sniffing the network. Tested with <code>60</code>s on a network with 50 devices.',
'PHOLUS_TIMEOUT_description' => 'How long (s) should Pholus be sniffing the network. Only used if an <code>(unknown)</code> device is found. The longer you leave it on, the more likely devices would broadcast more info.',
);