mirror of
https://github.com/rapid7/metasploit-payloads
synced 2025-01-14 17:37:27 +01:00
Update Javas stdapi_fs_delete_dir to be recursive
This commit is contained in:
parent
c3b9bbc188
commit
e0dae30a95
@ -9,16 +9,43 @@ import com.metasploit.meterpreter.TLVType;
|
|||||||
import com.metasploit.meterpreter.command.Command;
|
import com.metasploit.meterpreter.command.Command;
|
||||||
|
|
||||||
public class stdapi_fs_delete_dir implements Command {
|
public class stdapi_fs_delete_dir implements Command {
|
||||||
|
|
||||||
public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket response) throws Exception {
|
public int execute(Meterpreter meterpreter, TLVPacket request, TLVPacket response) throws Exception {
|
||||||
String path = request.getStringValue(TLVType.TLV_TYPE_DIRECTORY_PATH);
|
String path = request.getStringValue(TLVType.TLV_TYPE_DIRECTORY_PATH);
|
||||||
File file = Loader.expand(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);
|
throw new IOException("Directory not found: " + path);
|
||||||
}
|
}
|
||||||
if (!file.delete()) {
|
|
||||||
throw new IOException("Cannot delete directory " + file.getCanonicalPath());
|
|
||||||
}
|
|
||||||
return ERROR_SUCCESS;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user