1
mirror of https://github.com/rapid7/metasploit-payloads synced 2025-01-02 11:36:22 +01:00

Fix handinig symlinks and junctions on Windows

This commit is contained in:
Spencer McIntyre 2024-11-06 16:14:13 -05:00
parent 894226a0f2
commit 64631d276b
4 changed files with 26 additions and 16 deletions

View File

@ -42,7 +42,7 @@ public class Loader implements ExtensionLoader {
public void load(CommandManager mgr) throws Exception {
mgr.registerCommand(CommandId.CORE_CHANNEL_OPEN, stdapi_channel_open.class, V1_2, V1_15);
mgr.registerCommand(CommandId.STDAPI_FS_CHDIR, stdapi_fs_chdir.class);
mgr.registerCommand(CommandId.STDAPI_FS_DELETE_DIR, stdapi_fs_delete_dir.class);
mgr.registerCommand(CommandId.STDAPI_FS_DELETE_DIR, stdapi_fs_delete_dir.class, V1_2, V1_7);
mgr.registerCommand(CommandId.STDAPI_FS_DELETE_FILE, stdapi_fs_delete_file.class);
mgr.registerCommand(CommandId.STDAPI_FS_FILE_EXPAND_PATH, stdapi_fs_file_expand_path.class, V1_2, V1_5); // %COMSPEC% only
mgr.registerCommand(CommandId.STDAPI_FS_FILE_MOVE, stdapi_fs_file_move.class);

View File

@ -12,8 +12,8 @@ public class stdapi_fs_delete_dir implements Command {
public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket response) throws Exception {
String path = request.getStringValue(TLVType.TLV_TYPE_DIRECTORY_PATH);
File file = Loader.expand(path);
if (isSymlink(file)) {
if (!file.delete()) {
if (FsUtils.isSymlink(file)) {
if (!deleteSymlink(file)) {
throw new IOException("Cannot delete symbolic link " + file.getCanonicalPath());
}
} else if (file.isDirectory()) {
@ -26,22 +26,15 @@ public class stdapi_fs_delete_dir implements Command {
return ERROR_SUCCESS;
}
private static boolean isSymlink(File file) throws IOException {
File canon;
if (file.getParent() == null) {
canon = file;
} else {
File canonDir = file.getParentFile().getCanonicalFile();
canon = new File(canonDir, file.getName());
}
return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
protected boolean deleteSymlink(File file) throws IOException {
return file.delete();
}
private boolean rmtree(File file) throws IOException {
boolean ret = true;
for (File subFile : file.listFiles()) {
if (isSymlink(subFile)) {
ret = ret && subFile.delete();
if (FsUtils.isSymlink(subFile)) {
ret = ret && deleteSymlink(subFile);
} else if (subFile.isDirectory()) {
ret = ret && rmtree(subFile);
} else {

View File

@ -0,0 +1,13 @@
package com.metasploit.meterpreter.stdapi;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class stdapi_fs_delete_dir_V1_7 extends stdapi_fs_delete_dir {
@Override
protected boolean deleteSymlink(File file) throws IOException {
Files.delete(file.toPath());
return true;
}
}

View File

@ -24,10 +24,10 @@ public class stdapi_fs_stat implements Command {
}
}
File file = new File(path);
if (!file.exists()) {
if (!exists(file)) {
file = Loader.expand(path);
}
if (!file.exists()) {
if (!exists(file)) {
throw new IOException("File/directory does not exist: " + path);
}
response.add(TLVType.TLV_TYPE_STAT_BUF, stat(file));
@ -76,6 +76,10 @@ public class stdapi_fs_stat implements Command {
return false;
}
private boolean exists(File file) throws IOException {
return file.exists() || FsUtils.isSymlink(file);
}
/**
* Convert an integer to little endian.
*/