Skip to content

Commit

Permalink
Add: Add smb_max_protocol option for SMB alert
Browse files Browse the repository at this point in the history
  • Loading branch information
timopollmeier authored Mar 6, 2023
2 parents a47802c + 24c2374 commit 6712c83
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
39 changes: 23 additions & 16 deletions src/alert_methods/SMB/alert
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -119,15 +120,21 @@ def smb_put(auth_path, share, report_path, dest_path):


def main():
if len(sys.argv) != 5:
print("usage: %s <share> <dest_path> <auth_path> <report_path>"
if len(sys.argv) != 6:
print("usage: %s <share> <dest_path> <max_protocol> <auth_path> <report_path>"
% 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

Expand Down Expand Up @@ -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__':
Expand Down
22 changes: 16 additions & 6 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -9987,29 +9987,35 @@ 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)
return -1;

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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -13254,14 +13263,15 @@ 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);
g_free (password);
free (credential_id);
free (share_path);
free (file_path);
free (max_protocol);
g_free (report_content);
g_free (extension);
return ret;
Expand Down

0 comments on commit 6712c83

Please sign in to comment.