Skip to content

Commit

Permalink
Fix handinig symlinks and junctions on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroSteiner committed Nov 6, 2024
1 parent 894226a commit 64631d2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 64631d2

Please sign in to comment.