diff --git a/c/meterpreter/source/extensions/stdapi/server/fs/dir.c b/c/meterpreter/source/extensions/stdapi/server/fs/dir.c index 522dbba5..c671d289 100644 --- a/c/meterpreter/source/extensions/stdapi/server/fs/dir.c +++ b/c/meterpreter/source/extensions/stdapi/server/fs/dir.c @@ -58,12 +58,14 @@ DWORD request_fs_ls(Remote * remote, Packet * packet) DWORD request_fs_getwd(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); - char directory[FS_MAX_PATH]; + char *directory = NULL; DWORD result; - result = fs_getwd(directory, sizeof(directory)); - - packet_add_tlv_string(response, TLV_TYPE_DIRECTORY_PATH, directory); + result = fs_getwd(&directory); + if (directory != NULL) { + packet_add_tlv_string(response, TLV_TYPE_DIRECTORY_PATH, directory); + free(directory); + } packet_add_tlv_uint(response, TLV_TYPE_RESULT, result); return packet_transmit(remote, response, NULL); @@ -78,7 +80,7 @@ DWORD request_fs_getwd(Remote * remote, Packet * packet) DWORD request_fs_chdir(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); - LPCSTR directory; + char *directory; DWORD result; directory = packet_get_tlv_value_string(packet, TLV_TYPE_DIRECTORY_PATH); @@ -100,7 +102,7 @@ DWORD request_fs_chdir(Remote * remote, Packet * packet) DWORD request_fs_mkdir(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); - LPCSTR directory; + char *directory; DWORD result; directory = packet_get_tlv_value_string(packet, TLV_TYPE_DIRECTORY_PATH); @@ -122,7 +124,7 @@ DWORD request_fs_mkdir(Remote * remote, Packet * packet) DWORD request_fs_delete_dir(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); - LPCSTR directory; + char *directory; DWORD result; directory = packet_get_tlv_value_string(packet, TLV_TYPE_DIRECTORY_PATH); diff --git a/c/meterpreter/source/extensions/stdapi/server/fs/fs_local.h b/c/meterpreter/source/extensions/stdapi/server/fs/fs_local.h index 467da074..6e3369eb 100644 --- a/c/meterpreter/source/extensions/stdapi/server/fs/fs_local.h +++ b/c/meterpreter/source/extensions/stdapi/server/fs/fs_local.h @@ -55,7 +55,7 @@ int fs_fopen(const char *path, const char *mode, FILE **f); int fs_ls(const char *directory, fs_ls_cb_t cb, void *arg); -int fs_getwd(char *directory, size_t len); +int fs_getwd(char **directory); int fs_mkdir(const char *directory); diff --git a/c/meterpreter/source/extensions/stdapi/server/fs/fs_posix.c b/c/meterpreter/source/extensions/stdapi/server/fs/fs_posix.c index ee638386..ccf3e3e1 100644 --- a/c/meterpreter/source/extensions/stdapi/server/fs/fs_posix.c +++ b/c/meterpreter/source/extensions/stdapi/server/fs/fs_posix.c @@ -56,12 +56,14 @@ char * fs_expand_path(const char *regular) return strdup(regular); } -int fs_getwd(char *directory, size_t len) +int fs_getwd(char **directory) { - if (getcwd(directory, len) == NULL) { + char dir[FS_MAX_PATH]; + if (getcwd(dir, sizeof(dir)) == NULL) { return errno; } - return ERROR_SUCCESS; + *directory = strdup(dir); + return *directory == NULL ? ERROR_NOT_ENOUGH_MEMORY : ERROR_SUCCESS; } int fs_mkdir(const char *directory) diff --git a/c/meterpreter/source/extensions/stdapi/server/fs/fs_win.c b/c/meterpreter/source/extensions/stdapi/server/fs/fs_win.c index 45b40705..18a8ba75 100644 --- a/c/meterpreter/source/extensions/stdapi/server/fs/fs_win.c +++ b/c/meterpreter/source/extensions/stdapi/server/fs/fs_win.c @@ -201,33 +201,22 @@ out: return rc; } -int fs_getwd(char *directory, size_t len) +int fs_getwd(char **dir) { int rc = ERROR_SUCCESS; - wchar_t *dir_w; - char *dir; + wchar_t dir_w[FS_MAX_PATH]; - dir_w = calloc(len, sizeof(wchar_t)); - if (dir_w == NULL) { + if (GetCurrentDirectoryW(FS_MAX_PATH, dir_w) == 0) { rc = GetLastError(); goto out; } - if (GetCurrentDirectoryW(len, dir_w) == 0) { + *dir = wchar_to_utf8(dir_w); + if (*dir == NULL) { rc = GetLastError(); - goto out; } - dir = wchar_to_utf8(dir_w); - if (dir == NULL) { - rc = GetLastError(); - goto out; - } - strncpy(directory, dir, len); - out: - free(dir); - free(dir_w); return rc; }