Skip to content

Commit feac5ee

Browse files
authored
Merge pull request #792 from msutovsky-r7/feat/stdapi/fs/mkdir/nested_dirs
Adds support for nested directories
2 parents a5d4c0c + 4584a75 commit feac5ee

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

c/meterpreter/source/extensions/stdapi/server/fs/fs_local.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int fs_ls(const char *directory, fs_ls_cb_t cb, void *arg);
4949

5050
int fs_getwd(char **directory);
5151

52-
int fs_mkdir(const char *directory);
52+
int fs_mkdir(char *directory);
5353

5454
int fs_move(const char *oldpath, const char *newpath);
5555

c/meterpreter/source/extensions/stdapi/server/fs/fs_win.c

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,69 @@ int fs_copy(const char *oldpath, const char *newpath)
345345
return rc;
346346
}
347347

348-
int fs_mkdir(const char *directory)
348+
int fs_mkdir(char *directory)
349349
{
350350
int rc = ERROR_SUCCESS;
351-
wchar_t *dir_w = met_api->string.utf8_to_wchar(directory);
351+
struct meterp_stat s = { 0 };
352+
wchar_t* dir_w;
353+
size_t directory_length;
354+
char* base_dir;
355+
char* dir;
356+
HANDLE process_heap = NULL;
357+
358+
if(directory == NULL){
359+
return ERROR_INVALID_PARAMETER;
360+
}
352361

353-
if (dir_w == NULL) {
362+
process_heap = GetProcessHeap();
363+
364+
if(process_heap == NULL){
354365
rc = GetLastError();
355-
goto out;
366+
return rc;
356367
}
357368

358-
if (CreateDirectoryW(dir_w, NULL) == 0) {
369+
// Add 2 because of NULL character and additional backslash
370+
directory_length = lstrlenA(directory)+2;
371+
base_dir = (char *)HeapAlloc(process_heap, 0, directory_length);
372+
373+
if(base_dir == NULL){
359374
rc = GetLastError();
375+
return rc;
360376
}
361377

378+
dir = strtok(directory, "\\");
379+
380+
sprintf_s(base_dir, directory_length, "%s\\", dir);
381+
382+
while (dir != NULL)
383+
{
384+
if (fs_stat(base_dir, &s) != ERROR_SUCCESS) {
385+
dir_w = met_api->string.utf8_to_wchar(base_dir);
386+
if (dir_w == NULL) {
387+
rc = GetLastError();
388+
goto out;
389+
}
390+
391+
if (CreateDirectoryW(dir_w, NULL) == 0) {
392+
rc = GetLastError();
393+
free(dir_w);
394+
goto out;
395+
}
396+
free(dir_w);
397+
}
398+
399+
dir = strtok(NULL, "\\");
400+
401+
if (dir != NULL){
402+
sprintf_s((char*)(base_dir+lstrlenA(base_dir)), directory_length, "%s\\",dir);
403+
}
404+
405+
memset(&s, 0, sizeof(struct meterp_stat));
406+
}
407+
408+
362409
out:
363-
free(dir_w);
410+
HeapFree(process_heap,0, base_dir);
364411
return rc;
365412
}
366413

0 commit comments

Comments
 (0)