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

Update Javas stdapi_fs_delete_dir to be recursive

This commit is contained in:
Spencer McIntyre 2022-01-05 17:12:41 -05:00
parent c3b9bbc188
commit e0dae30a95

View File

@ -9,16 +9,43 @@ import com.metasploit.meterpreter.TLVType;
import com.metasploit.meterpreter.command.Command;
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 (!file.exists() || !file.isDirectory()) {
if (isSymlink(file)) {
if (!file.delete()) {
throw new IOException("Cannot delete symbolic link " + file.getCanonicalPath());
}
} else if (file.isDirectory()) {
rmtree(file);
} else {
throw new IOException("Directory not found: " + path);
}
if (!file.delete()) {
throw new IOException("Cannot delete directory " + file.getCanonicalPath());
}
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());
}
private void rmtree(File file) throws IOException {
for (File subFile : file.listFiles()) {
if (isSymlink(subFile)) {
subFile.delete();
} else if (subFile.isDirectory()) {
rmtree(subFile);
} else {
subFile.delete();
}
}
file.delete();
return;
}
}