Nmap fix - Results not written into DB

This commit is contained in:
Jokob-sk
2023-02-26 15:40:38 +11:00
parent e0d639cba0
commit d87d933058
4 changed files with 31 additions and 36 deletions

View File

@@ -1765,6 +1765,8 @@ def performNmapScan(devicesToScan):
# previous Nmap Entries # previous Nmap Entries
oldEntries = [] oldEntries = []
mylog('verbose', ['[', timeNow(), '] Scan: Ports found by NMAP: ', len(newEntries)])
if len(newEntries) > 0: if len(newEntries) > 0:
# get all current NMAP ports from the DB # get all current NMAP ports from the DB
@@ -1779,16 +1781,18 @@ def performNmapScan(devicesToScan):
# Remove all entries already available in the database # Remove all entries already available in the database
for newEntry in newEntries: for newEntry in newEntries:
# Check if available in oldEntries # Check if available in oldEntries and remove if yes
if any(x.hash == newEntry.hash for x in oldEntries): if any(x.hash == newEntry.hash for x in oldEntries):
newEntries.pop(index) newEntries.pop(index)
mylog('verbose', ['[', timeNow(), '] Scan: Nmap found ', len(newEntries), ' new or changed ports']) mylog('verbose', ['[', timeNow(), '] Scan: Nmap new or changed ports: ', len(newEntries)])
mylog('verbose', ['[', timeNow(), '] Scan: Nmap old entries: ', len(oldEntries)])
# collect new ports, find the corresponding old entry and return for notification purposes # collect new ports, find the corresponding old entry and return for notification purposes
# also update the DB with the new values after deleting the old ones # also update the DB with the new values after deleting the old ones
if len(newEntries) > 0: if len(newEntries) > 0:
# params to build the SQL query
params = [] params = []
indexesToDelete = "" indexesToDelete = ""
@@ -1799,15 +1803,16 @@ def performNmapScan(devicesToScan):
for oldEntry in oldEntries: for oldEntry in oldEntries:
if oldEntry.hash == newEntry.hash: if oldEntry.hash == newEntry.hash:
params.append(newEntry.mac, newEntry.time, newEntry.port, newEntry.state, newEntry.service, oldEntry.extra)
indexesToDelete = indexesToDelete + str(oldEntry.index) + ',' indexesToDelete = indexesToDelete + str(oldEntry.index) + ','
foundEntry = oldEntry foundEntry = oldEntry
columnNames = ["Name", "MAC", "Port", "State", "Service", "Extra", "NewOrOld" ] columnNames = ["Name", "MAC", "Port", "State", "Service", "Extra", "NewOrOld" ]
# Old entry found
if foundEntry is not None: if foundEntry is not None:
# Build params for sql query
params.append((newEntry.mac, newEntry.time, newEntry.port, newEntry.state, newEntry.service, oldEntry.extra))
# Build JSON for API and notifications
changedPortsTmp.append({ changedPortsTmp.append({
"Name" : foundEntry.name, "Name" : foundEntry.name,
"MAC" : newEntry.mac, "MAC" : newEntry.mac,
@@ -1826,7 +1831,11 @@ def performNmapScan(devicesToScan):
"Extra" : foundEntry.extra, "Extra" : foundEntry.extra,
"NewOrOld" : "Old values" "NewOrOld" : "Old values"
}) })
# New entry - no matching Old entry found
else: else:
# Build params for sql query
params.append((newEntry.mac, newEntry.time, newEntry.port, newEntry.state, newEntry.service, ''))
# Build JSON for API and notifications
changedPortsTmp.append({ changedPortsTmp.append({
"Name" : "New device", "Name" : "New device",
"MAC" : newEntry.mac, "MAC" : newEntry.mac,
@@ -3806,11 +3815,7 @@ def execute_plugin(plugin):
# cleanup - select only lines containing a separator to filter out unnecessary data # cleanup - select only lines containing a separator to filter out unnecessary data
newLines = list(filter(lambda x: '|' in x, newLines)) newLines = list(filter(lambda x: '|' in x, newLines))
if len(newLines) == 0: # check if the subprocess failed / there was no valid output pluginEventCount = len(newLines)
mylog('none', [' [Plugins] No output received from the plugin - enable LOG_LEVEL=debug and check logs'])
return
else:
mylog('verbose', ['[', timeNow(), '] [Plugins]: SUCCESS, received ', len(newLines), ' entries'])
# # regular logging # # regular logging
# for line in newLines: # for line in newLines:
@@ -3833,7 +3838,7 @@ def execute_plugin(plugin):
sqlParams = [] sqlParams = []
# set_CMD should contain a SQL query # set_CMD should contain a SQL query
arr = get_sql_array (set_CMD) arr = get_sql_array (set_CMD.replace("{s-quote}", '\''))
for row in arr: for row in arr:
# There has to be always 8 columns # There has to be always 8 columns
@@ -3842,6 +3847,15 @@ def execute_plugin(plugin):
else: else:
mylog('none', [' [Plugins]: Skipped invalid sql result']) mylog('none', [' [Plugins]: Skipped invalid sql result'])
# check if the subprocess / SQL query failed / there was no valid output
if len(sqlParams) == 0:
mylog('none', [' [Plugins] No output received from the plugin ', plugin["unique_prefix"], ' - enable LOG_LEVEL=debug and check logs'])
return
else:
mylog('verbose', ['[', timeNow(), '] [Plugins]: SUCCESS, received ', pluginEventCount, ' entries'])
# process results if any
if len(sqlParams) > 0: if len(sqlParams) > 0:
sql.executemany ("""INSERT INTO Plugins_Events ("Plugin", "Object_PrimaryID", "Object_SecondaryID", "DateTimeCreated", "DateTimeChanged", "Watched_Value1", "Watched_Value2", "Watched_Value3", "Watched_Value4", "Status" ,"Extra", "UserData") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", sqlParams) sql.executemany ("""INSERT INTO Plugins_Events ("Plugin", "Object_PrimaryID", "Object_SecondaryID", "DateTimeCreated", "DateTimeChanged", "Watched_Value1", "Watched_Value2", "Watched_Value3", "Watched_Value4", "Status" ,"Extra", "UserData") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", sqlParams)
commitDB () commitDB ()

View File

@@ -1775,7 +1775,7 @@ function initializeTabsNew () {
function loadNmap() function loadNmap()
{ {
$(".deviceSpecific").remove(); // remove any previous data listed in teh table $(".deviceSpecific").remove(); // remove any previous data listed in the table
$.get('php/server/devices.php?action=getNmap&mac='+ mac, function(data) { $.get('php/server/devices.php?action=getNmap&mac='+ mac, function(data) {

View File

@@ -182,7 +182,7 @@ function generateTabs()
$.each(pluginDefinitions, function(index, obj) { $.each(pluginDefinitions, function(index, obj) {
$('#tabs-location').append( $('#tabs-location').append(
`<li class="${activetab}"> `<li class=" ${activetab}">
<a href="#${obj.unique_prefix}" data-plugin-prefix="${obj.unique_prefix}" id="${obj.unique_prefix}_id" data-toggle="tab" > <a href="#${obj.unique_prefix}" data-plugin-prefix="${obj.unique_prefix}" id="${obj.unique_prefix}_id" data-toggle="tab" >
${localize(obj, 'icon')} ${localize(obj, 'display_name')} ${localize(obj, 'icon')} ${localize(obj, 'display_name')}
</a> </a>

View File

@@ -221,25 +221,6 @@
"string" : "Only enabled if you select <code>schedule</code> in the <a href=\"#NMAPSRV_RUN\"><code>NMAPSRV_RUN</code> setting</a>. Make sure you enter the schedule in the correct cron-like format (e.g. validate at <a href=\"https://crontab.guru/\" target=\"_blank\">crontab.guru</a>). For example entering <code>0 4 * * *</code> will run the scan after 4 am in the <a onclick=\"toggleAllSettings()\" href=\"#TIMEZONE\"><code>TIMEZONE</code> you set above</a>. Will be run NEXT time the time passes." "string" : "Only enabled if you select <code>schedule</code> in the <a href=\"#NMAPSRV_RUN\"><code>NMAPSRV_RUN</code> setting</a>. Make sure you enter the schedule in the correct cron-like format (e.g. validate at <a href=\"https://crontab.guru/\" target=\"_blank\">crontab.guru</a>). For example entering <code>0 4 * * *</code> will run the scan after 4 am in the <a onclick=\"toggleAllSettings()\" href=\"#TIMEZONE\"><code>TIMEZONE</code> you set above</a>. Will be run NEXT time the time passes."
}] }]
}, },
{
"function": "RUN_TIMEOUT",
"type": "integer",
"default_value":5,
"options": [],
"localized": ["name", "description"],
"name" : [{
"language_code":"en_us",
"string" : "Run timeout"
},
{
"language_code":"de_de",
"string" : "Wartezeit"
}],
"description": [{
"language_code":"en_us",
"string" : "Maximum time in seconds to wait for the call to finish. If this time is exceeded the script is aborted."
}]
},
{ {
"function": "WATCH", "function": "WATCH",
"type": "multiselect", "type": "multiselect",