diff --git a/src/alert_methods/SMB/alert b/src/alert_methods/SMB/alert index 886a594d3..197b16a27 100755 --- a/src/alert_methods/SMB/alert +++ b/src/alert_methods/SMB/alert @@ -32,8 +32,9 @@ def smb_error_print(message, stdout, stderr): print(stdout, file=sys.stderr) -def smb_call(auth_path, share, command): - args = ["smbclient", "-A", auth_path, share, "-c", command] +def smb_call(auth_path, share, command, extra_args): + args = ["smbclient"] + extra_args + ["-A", auth_path, share, "-c", command] + retries = 10 stdout = '' stderr = '' @@ -74,9 +75,9 @@ def smb_call(auth_path, share, command): sys.exit(1) -def smb_dir_exists(auth_path, share, check_dir): +def smb_dir_exists(auth_path, share, check_dir, extra_args): command = "cd \"%s\"" % (check_dir) - rc, stdout, stderr = smb_call(auth_path, share, command) + rc, stdout, stderr = smb_call(auth_path, share, command, extra_args) if rc == 0: return True @@ -89,12 +90,12 @@ def smb_dir_exists(auth_path, share, check_dir): sys.exit(1) -def smb_mkdir(auth_path, share, check_dir): +def smb_mkdir(auth_path, share, check_dir, extra_args): command = "mkdir \"%s\"" % (check_dir) - rc, stdout, stderr = smb_call(auth_path, share, command) + rc, stdout, stderr = smb_call(auth_path, share, command, extra_args) if rc == 0: - if not smb_dir_exists(auth_path, share, check_dir): + if not smb_dir_exists(auth_path, share, check_dir, extra_args): print("Could not create directory %s" % check_dir, file=sys.stderr) sys.exit(1) @@ -106,9 +107,9 @@ def smb_mkdir(auth_path, share, check_dir): sys.exit(1) -def smb_put(auth_path, share, report_path, dest_path): +def smb_put(auth_path, share, report_path, dest_path, extra_args): command = "put \"%s\" \"%s\"" % (report_path, dest_path) - rc, stdout, stderr = smb_call(auth_path, share, command) + rc, stdout, stderr = smb_call(auth_path, share, command, extra_args) if rc == 0: print("Report copied to directory %s" % dest_path) @@ -119,15 +120,21 @@ def smb_put(auth_path, share, report_path, dest_path): def main(): - if len(sys.argv) != 5: - print("usage: %s " + if len(sys.argv) != 6: + print("usage: %s " % sys.argv[0], file=sys.stderr) sys.exit(1) share = sys.argv[1] dest_path = sys.argv[2] - auth_path = sys.argv[3] - report_path = sys.argv[4] + + extra_args = [] + if sys.argv[3]: + extra_args.append("-m") + extra_args.append(sys.argv[3]) + + auth_path = sys.argv[4] + report_path = sys.argv[5] create_dirs = True @@ -158,16 +165,16 @@ def main(): # Find first existing path first_existing_path_index = -1 for i in range(len(dest_subpaths)-1, -1, -1): - if smb_dir_exists(auth_path, share, dest_subpaths[i]): + if smb_dir_exists(auth_path, share, dest_subpaths[i], extra_args): first_existing_path_index = i break # Create missing directories if create_dirs: for i in range(first_existing_path_index + 1, len(dest_subpaths)): - smb_mkdir(auth_path, share, dest_subpaths[i]) + smb_mkdir(auth_path, share, dest_subpaths[i], extra_args) - smb_put(auth_path, share, report_path, dest_path) + smb_put(auth_path, share, report_path, dest_path, extra_args) if __name__ == '__main__': diff --git a/src/manage_sql.c b/src/manage_sql.c index fe7f998da..5add27246 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -9987,15 +9987,17 @@ scp_to_host (const char *username, const char *password, static int smb_send_to_host (const char *password, const char *username, const char *share_path, const char *file_path, + const char *max_protocol, const char *report, gsize report_size, gchar **script_message) { - gchar *clean_share_path, *clean_file_path; + gchar *clean_share_path, *clean_file_path, *clean_max_protocol; gchar *authfile_content; gchar *command_args; int ret; - g_debug ("smb as %s to share: %s, path: %s", username, share_path, file_path); + g_debug ("smb as %s to share: %s, path: %s, max_protocol: %s", + username, share_path, file_path, max_protocol); if (password == NULL || username == NULL || share_path == NULL || file_path == NULL) @@ -10003,13 +10005,17 @@ smb_send_to_host (const char *password, const char *username, clean_share_path = g_shell_quote (share_path); clean_file_path = g_shell_quote (file_path); + clean_max_protocol = g_shell_quote (max_protocol ? max_protocol : ""); authfile_content = g_strdup_printf ("username = %s\n" "password = %s\n", username, password); - command_args = g_strdup_printf ("%s %s", - clean_share_path, clean_file_path); + command_args = g_strdup_printf ("%s %s %s", + clean_share_path, + clean_file_path, + clean_max_protocol); g_free (clean_share_path); g_free (clean_file_path); + g_free (clean_max_protocol); ret = run_alert_script ("c427a688-b653-40ab-a9d0-d6ba842a9d63", command_args, "report", report, report_size, @@ -13122,7 +13128,7 @@ escalate_2 (alert_t alert, task_t task, report_t report, event_t event, case ALERT_METHOD_SMB: { char *credential_id, *username, *password; - char *share_path, *file_path_format; + char *share_path, *file_path_format, *max_protocol; gboolean file_path_is_dir; report_format_t report_format; gchar *file_path, *report_content, *extension; @@ -13167,6 +13173,7 @@ escalate_2 (alert_t alert, task_t task, report_t report, event_t event, credential_id = alert_data (alert, "method", "smb_credential"); share_path = alert_data (alert, "method", "smb_share_path"); + max_protocol = alert_data (alert, "method", "smb_max_protocol"); file_path_format = sql_string ("SELECT value FROM tags" @@ -13207,6 +13214,7 @@ escalate_2 (alert_t alert, task_t task, report_t report, event_t event, free (credential_id); free (share_path); free (file_path_format); + free (max_protocol); g_free (report_content); g_free (extension); return ret ? ret : -1; @@ -13245,6 +13253,7 @@ escalate_2 (alert_t alert, task_t task, report_t report, event_t event, free (credential_id); free (share_path); free (file_path); + free (max_protocol); g_free (report_content); g_free (extension); return ret ? -1 : -4; @@ -13254,7 +13263,7 @@ escalate_2 (alert_t alert, task_t task, report_t report, event_t event, password = credential_encrypted_value (credential, "password"); ret = smb_send_to_host (password, username, share_path, file_path, - report_content, content_length, + max_protocol, report_content, content_length, script_message); g_free (username); @@ -13262,6 +13271,7 @@ escalate_2 (alert_t alert, task_t task, report_t report, event_t event, free (credential_id); free (share_path); free (file_path); + free (max_protocol); g_free (report_content); g_free (extension); return ret;