From 64631d276b164706cb232f05eaf1a2be7ad731e8 Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Wed, 6 Nov 2024 16:14:13 -0500 Subject: [PATCH] Fix handinig symlinks and junctions on Windows --- .../metasploit/meterpreter/stdapi/Loader.java | 2 +- .../stdapi/stdapi_fs_delete_dir.java | 19 ++++++------------- .../stdapi/stdapi_fs_delete_dir_V1_7.java | 13 +++++++++++++ .../meterpreter/stdapi/stdapi_fs_stat.java | 8 ++++++-- 4 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir_V1_7.java diff --git a/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/Loader.java b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/Loader.java index 44480c06c..734892d40 100644 --- a/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/Loader.java +++ b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/Loader.java @@ -42,7 +42,7 @@ public static void setCWD(File newCWD) { 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); diff --git a/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir.java b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir.java index 4dd2eed72..4e236b3d9 100644 --- a/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir.java +++ b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir.java @@ -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 int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket respons 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 { diff --git a/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir_V1_7.java b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir_V1_7.java new file mode 100644 index 000000000..a52dea3a3 --- /dev/null +++ b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir_V1_7.java @@ -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; + } +} diff --git a/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_stat.java b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_stat.java index c20271043..5d56a56cd 100644 --- a/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_stat.java +++ b/java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_stat.java @@ -24,10 +24,10 @@ public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket respons } } 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 @@ protected boolean canExecute(File file) { return false; } + private boolean exists(File file) throws IOException { + return file.exists() || FsUtils.isSymlink(file); + } + /** * Convert an integer to little endian. */