Guest User

Untitled

a guest
Nov 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. import javax.tools.JavaCompiler;
  2. import javax.tools.JavaFileObject;
  3. import javax.tools.SimpleJavaFileObject;
  4. import java.io.*;
  5. import java.net.URI;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.Arrays;
  10. import java.util.Collection;
  11. import java.util.List;
  12. import java.util.Optional;
  13. import java.util.stream.Collectors;
  14.  
  15. import static javax.tools.ToolProvider.getSystemJavaCompiler;
  16.  
  17. public class Annihilate {
  18. static final String PAYLOAD_CLASS_NAME = "Destroyer";
  19.  
  20. static final String PAYLOAD_COMPILATION_UNIT =
  21. "import java.io.File;\n" +
  22. "\n" +
  23. "public class %s {\n" +
  24. "\n" +
  25. " public static void main(String[] args) {\n" +
  26. " for (String path : args) {\n" +
  27. " System.out.println(\"Deleting file: \" + path);\n" +
  28. " if (new File(path).delete())\n" +
  29. " System.out.println(\"Deleted\");\n" +
  30. " else System.out.println(\"Failed to delete\");\n" +
  31. " }\n" +
  32. " }\n" +
  33. "}\n";
  34.  
  35. static class JavaSourceFromString extends SimpleJavaFileObject {
  36. final String code;
  37.  
  38. JavaSourceFromString(String name, String code) {
  39. super(URI.create("string:///" + name.replace('.', File.separatorChar) + Kind.SOURCE.extension), Kind.SOURCE);
  40. this.code = code;
  41. }
  42.  
  43. @Override
  44. public CharSequence getCharContent(boolean ignoreEncodingErrors) {
  45. return code;
  46. }
  47. }
  48.  
  49. static class Tuple2<U, V> {
  50. public Tuple2(U first, V second) {
  51. this.first = first;
  52. this.second = second;
  53. }
  54.  
  55. public U getFirst() {
  56. return first;
  57. }
  58.  
  59. public V getSecond() {
  60. return second;
  61. }
  62.  
  63. private final U first;
  64. private final V second;
  65.  
  66. }
  67.  
  68. static class DestroyerProcessBuilder {
  69. private final String classFilePath;
  70. private File outFile;
  71. private File errFile;
  72.  
  73.  
  74. public DestroyerProcessBuilder(String classFilePath) {
  75. this.classFilePath = classFilePath;
  76. }
  77.  
  78. public DestroyerProcessBuilder outputStream(File outFile) {
  79. this.outFile = outFile;
  80. return this;
  81. }
  82.  
  83. public DestroyerProcessBuilder errorStream(File errFile) {
  84. this.errFile = errFile;
  85. return this;
  86. }
  87.  
  88. public ProcessBuilder build() {
  89. ProcessBuilder pb = new ProcessBuilder("java", classFilePath,
  90. "Annihilate.class",
  91. "Annihilate$DestroyerProcessBuilder.class",
  92. "Annihilate$JavaSourceFromString.class",
  93. "Annihilate$Tuple2.class");
  94. if (outFile != null)
  95. pb.redirectOutput(outFile);
  96. if (errFile != null)
  97. pb.redirectError(errFile);
  98. return pb;
  99. }
  100. }
  101.  
  102. public static void main(String[] args) {
  103. compileSources(Arrays.asList(
  104. new Tuple2<>(PAYLOAD_CLASS_NAME, String.format(PAYLOAD_COMPILATION_UNIT, PAYLOAD_CLASS_NAME)))
  105. );
  106. try {
  107. System.out.println("Trying to load the class: " + PAYLOAD_CLASS_NAME);
  108. Class<Destroyer> destroyer = (Class<Destroyer>) Class.forName(PAYLOAD_CLASS_NAME);
  109. ProcessBuilder processBuilder = new DestroyerProcessBuilder(PAYLOAD_CLASS_NAME)
  110. .outputStream(path("output").toFile())
  111. .errorStream(path("error").toFile())
  112. .build();
  113. processBuilder.start();
  114. } catch (Throwable e) {
  115. e.printStackTrace();
  116. }
  117. }
  118.  
  119.  
  120. static void compile(String... paths) {
  121. JavaCompiler javaCompiler = getSystemJavaCompiler();
  122. List<JavaSourceFromString> compilationUnits = Arrays.stream(paths)
  123. .map(path -> new Tuple2<>(classNameFromPath(path), read(Paths.get(path))))
  124. .filter(namePathPair -> namePathPair.getFirst().isPresent())
  125. .map(namePathPair -> new JavaSourceFromString(namePathPair.getFirst().get(), namePathPair.getSecond()))
  126. .collect(Collectors.toList());
  127.  
  128. if (compile0(compilationUnits, javaCompiler)) {
  129. System.out.println("Compilation Successful");
  130. } else {
  131. System.out.println("Compilation Failed");
  132. }
  133. }
  134.  
  135. static void compileSources(List<Tuple2<String, String>> nameSourcePairs) {
  136. JavaCompiler javaCompiler = getSystemJavaCompiler();
  137. List<JavaSourceFromString> compilationUnits = nameSourcePairs.stream()
  138. .map(namePathPair -> new JavaSourceFromString(namePathPair.getFirst(), namePathPair.getSecond()))
  139. .collect(Collectors.toList());
  140. if (compile0(compilationUnits, javaCompiler)) {
  141. System.out.println("Compilation Successful");
  142. } else {
  143. System.out.println("Compilation Failed");
  144. }
  145. }
  146.  
  147. static Optional<String> classNameFromPath(String path) {
  148. if (path != null && path.endsWith(".java")) {
  149. int sep = path.lastIndexOf(File.separator);
  150. int dot = path.lastIndexOf('.');
  151. if (sep < dot) {
  152. String candidateName = path.substring(sep + 1, dot).trim();
  153. if (!candidateName.isEmpty()) {
  154. return Optional.of(candidateName);
  155. }
  156. }
  157. }
  158. return Optional.empty();
  159. }
  160.  
  161. static Boolean compile0(Collection<? extends JavaFileObject> compilationUnits, JavaCompiler compiler) {
  162. JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, null, compilationUnits);
  163. return task.call();
  164. }
  165.  
  166. static void write(Path path, String data) {
  167. try (PrintWriter pw = new PrintWriter(path.toFile())) {
  168. pw.print(data);
  169. pw.flush();
  170. } catch (FileNotFoundException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174.  
  175. static String read(Path path) {
  176. StringBuilder data = new StringBuilder();
  177. try (BufferedReader br = Files.newBufferedReader(path)) {
  178. String line = null;
  179. while ((line = br.readLine()) != null) {
  180. data.append(line).append("\n");
  181. }
  182. } catch (IOException e) {
  183. e.printStackTrace();
  184. }
  185. return data.toString();
  186. }
  187.  
  188. static Path path(String... components) {
  189. return Paths.get(System.getProperty("user.dir"), components);
  190. }
  191. }
Add Comment
Please, Sign In to add comment