Lua rules provide fast, efficient detection logic with minimal overhead. Dr.Semu includes a comprehensive utility library to simplify working with analysis data.
Retrieves the dynamic behavior JSON for the initial process.
local first_dynamic = utils.get_first_process_json(report_directory)
Returns: Table containing API call logs, or nil if not availableSource:/run_detections/dr_rules_files/utils.lua:27-46Implementation:
function utils.get_first_process_json(report_directory) local starter_json = report_directory .. "\\" .. "starter.json" local decoded_starer_json = utils.get_json_from_path(starter_json) if decoded_starer_json.empty then return nil end local starter_path = decoded_starer_json.image_path local starter_pid = decoded_starer_json.starter_pid if starter_path == nil or starter_pid == nil then return nil end -- starter process local first_decoded_dynamic = utils.get_json_pid(report_directory, starter_pid) if first_decoded_dynamic.empty then return nil end return first_decoded_dynamicend
Retrieves the static analysis JSON for the initial sample.
local first_static = utils.get_first_static(report_directory)
Returns: Table containing static analysis data (PE info, imports, sections), or nil if not availableSource:/run_detections/dr_rules_files/utils.lua:48-57Implementation:
function utils.get_first_static(report_directory) local starter_json = report_directory .. "\\" .. "starter.json" local decoded_starer_json = utils.get_json_from_path(starter_json) if decoded_starer_json.empty then return nil end local sha_256 = decoded_starer_json.sha_256 local static_decoded = utils.get_json_from_path(report_directory .. "\\" .. sha_256 .. ".json") return static_decodedend
local first_static = utils.get_first_static(report_directory)if first_static ~= nil then local is_x86 = first_static.generic.is_x86 local is_dll = first_static.generic.is_dll -- Access other static propertiesend
Dynamic behavior is stored as an array of API calls:
local first_dynamic = utils.get_first_process_json(report_directory)if first_dynamic ~= nil then for index, win_func in pairs(first_dynamic) do -- Check for specific API calls if win_func.NtCreateKey then -- Access call details end endend
if win_func.NtCreateUserProcess then -- Before state: input parameters local image_path = win_func.NtCreateUserProcess.before.image_path -- After state: output values if win_func.NtCreateUserProcess.success == true then local target_PID = win_func.NtCreateUserProcess.after.proc_id endend
Detect process creation and analyze child process behavior:
if win_func.NtCreateUserProcess then if win_func.NtCreateUserProcess.success == true then local target_PID = win_func.NtCreateUserProcess.after.proc_id local decoded_json = utils.get_json_pid(report_directory, target_PID) if not decoded_json.empty then -- enumerate a json of the child process for index, child_func in pairs(decoded_json) do -- Analyze child process API calls end end endend
Lua’s string:find() is useful for pattern detection:
if win_func.NtCreateUserProcess.before.image_path ~= nil then if win_func.NtCreateUserProcess.before.image_path:find("whoami") then return "WHOAMI!EXE" endend
function check(report_directory) local status = "CLEAN" -- Detection logic if suspicious_behavior then return "Win32.Malware.DR" -- Detection verdict end return status -- No detectionend
Verdict Format:
Return "CLEAN" for benign samples
Return a detection string for malicious samples (e.g., "Win32.WannaCry.DR")
Here’s the WannaCry detection rule from the source:
utils = require "utils"function check(report_directory) local status = "CLEAN" local first_dynamic = utils.get_first_process_json(report_directory) local first_static = utils.get_first_static(report_directory) -- static information local is_x86 = false if first_static ~= nil then is_x86 = first_static.generic.is_x86 end -- dynamic information if first_dynamic ~= nil then for index, win_func in pairs(first_dynamic) do if win_func.InternetOpenUrlA and win_func.InternetOpenUrlA.before.url then local url = win_func.InternetOpenUrlA.before.url:lower() if url == "http://www.iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com" then return "Win32.WannaCry.DR" end end end end return statusend
Source:/run_detections/dr_rules_files/wannacry_url.luaThis rule detects WannaCry by identifying its kill switch URL.