1
mirror of https://github.com/rapid7/metasploit-payloads synced 2025-03-24 18:16:24 +01:00

correct various issues listing files

* bury common _snprintf that snuck in while testing the Windows version back
   into the platform-specific code.
 * remove now-unneeded separator defines
 * don't free a stack variable on windows
This commit is contained in:
Brent Cook 2015-03-18 02:11:13 -05:00
parent 8944ca5156
commit e8318f8c5b
5 changed files with 30 additions and 23 deletions
c/meterpreter/source/extensions/stdapi/server/fs

@ -2,21 +2,11 @@
#include "fs_local.h"
void request_fs_ls_cb(void *arg, char *name, const char *dir)
void request_fs_ls_cb(void *arg, char *name, char *path)
{
Packet *response = arg;
char path[FS_MAX_PATH];
struct meterp_stat s;
/*
* Build the full path if we have a base directory
*/
if (dir) {
_snprintf(path, sizeof(path), "%s%c%s", dir, FS_SEPARATOR, name);
} else {
_snprintf(path, sizeof(path), "%s", name);
}
/*
* Add the file name, full path and stat information
*/

@ -207,7 +207,7 @@ DWORD request_fs_separator(Remote *remote, Packet *packet)
{
Packet *response = packet_create_response(packet);
packet_add_tlv_string(response, TLV_TYPE_STRING, FS_SEPARATOR_S);
packet_add_tlv_string(response, TLV_TYPE_STRING, FS_SEPARATOR);
packet_add_tlv_uint(response, TLV_TYPE_RESULT, ERROR_SUCCESS);

@ -5,13 +5,11 @@
#include <stdio.h>
#ifdef _WIN32
#define FS_SEPARATOR '\\'
#define FS_SEPARATOR_S "\\"
#define FS_MAX_PATH 32768
#define FS_SEPARATOR "\\"
#define FS_MAX_PATH 32768
#else
#define FS_SEPARATOR '/'
#define FS_SEPARATOR_S "/"
#define FS_MAX_PATH PATH_MAX
#define FS_SEPARATOR "/"
#define FS_MAX_PATH PATH_MAX
#endif
/*
@ -38,7 +36,7 @@ struct meterp_stat {
uint64_t st_ctime;
};
typedef void (* fs_ls_cb_t)(void *arg, char *name, const char *path);
typedef void (*fs_ls_cb_t)(void *arg, char *name, char *path);
int fs_chdir(const char *directory);

@ -13,6 +13,7 @@ int fs_ls(const char *directory, fs_ls_cb_t cb, void *arg)
{
struct meterp_stat s;
struct dirent *data;
char path[FS_MAX_PATH];
DIR *ctx = opendir(directory);
if (ctx == NULL) {
@ -20,7 +21,10 @@ int fs_ls(const char *directory, fs_ls_cb_t cb, void *arg)
}
while ((data = readdir(ctx))) {
cb(arg, data->d_name, directory);
snprintf(path, sizeof(path), "%s/%s", directory, data->d_name);
cb(arg, data->d_name, path);
}
closedir(ctx);

@ -115,19 +115,35 @@ int fs_ls(const char *directory, fs_ls_cb_t cb, void *arg)
WIN32_FIND_DATAW data;
wchar_t *path_w = utf8_to_wchar(expanded);
if (path_w == NULL) {
result = ERROR_NOT_ENOUGH_MEMORY;
result = GetLastError();
goto out;
}
HANDLE ctx = FindFirstFileW(path_w, &data);
if (ctx == NULL) {
result = GetLastError();
goto out;
}
do {
if (ctx == INVALID_HANDLE_VALUE) {
result = GetLastError();
break;
}
/*
* Build the full path if we have a base directory
*/
char *filename = wchar_to_utf8(data.cFileName);
cb(arg, filename, baseDirectory);
char path[FS_MAX_PATH];
if (baseDirectory) {
_snprintf(path, sizeof(path), "%s\\%s", filename, baseDirectory);
} else {
_snprintf(path, sizeof(path), "%s", data.cFileName);
}
cb(arg, filename, path);
free(filename);
} while (FindNextFileW(ctx, &data));
@ -138,7 +154,6 @@ int fs_ls(const char *directory, fs_ls_cb_t cb, void *arg)
FindClose(ctx);
free(expanded);
out:
free(tempDirectory);
free(baseDirectory);
free(path_w);
return result;