-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Land #731, Fix Java Meterpreter Symlink Handling on Windows
Land #731, Fix Java Meterpreter Symlink Handling on Windows
- Loading branch information
Showing
11 changed files
with
111 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
java/meterpreter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/FsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.metasploit.meterpreter.stdapi; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
|
||
public class FsUtils { | ||
public static boolean isSymlink(File file) throws IOException { | ||
String osName = System.getProperty("os.name"); | ||
if (osName != null && osName.toLowerCase().contains("windows") && isWindowsSymlink(file)) { | ||
return true; | ||
} | ||
|
||
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 static boolean isWindowsSymlink(File file) { | ||
// this uses reflection to access the java.nio.file classes necessary that are available on Java 7+ | ||
try { | ||
// first check using isSymbolicLink | ||
Class<?> filesClass = Class.forName("java.nio.file.Files"); | ||
Class<?> pathClass = Class.forName("java.nio.file.Path"); | ||
|
||
Method isSymbolicLinkMethod = filesClass.getMethod("isSymbolicLink", pathClass); | ||
Method toPathMethod = File.class.getMethod("toPath"); | ||
|
||
Object path = toPathMethod.invoke(file); | ||
if ((Boolean)isSymbolicLinkMethod.invoke(null, path)) { | ||
return true; | ||
} | ||
|
||
// next check if the target is a junction because isSymbolicLink doesn't handle that | ||
Class<?> linkOptionClass = Class.forName("java.nio.file.LinkOption"); | ||
Object linkOptionArray = java.lang.reflect.Array.newInstance(linkOptionClass, 0); | ||
Method toRealPath = pathClass.getMethod("toRealPath", linkOptionArray.getClass()); | ||
Object realPath = toRealPath.invoke(path, linkOptionArray); | ||
|
||
// toRealPath resolves junctions so the result will be different | ||
Method equalsMethod = pathClass.getMethod("equals", Object.class); | ||
if (!(Boolean)equalsMethod.invoke(path, realPath)) { | ||
return true; | ||
} | ||
} catch (ReflectiveOperationException e) { | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ter/stdapi/src/main/java/com/metasploit/meterpreter/stdapi/stdapi_fs_delete_dir_V1_7.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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 { | ||
String osName = System.getProperty("os.name"); | ||
if (osName != null && osName.toLowerCase().contains("windows")) { | ||
Files.delete(file.toPath()); | ||
return true; | ||
} | ||
return file.delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters