|
| 1 | +package com.contrastsecurity; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URISyntaxException; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import com.sun.tools.attach.AgentInitializationException; |
| 8 | +import com.sun.tools.attach.AgentLoadException; |
| 9 | +import com.sun.tools.attach.AttachNotSupportedException; |
| 10 | +import com.sun.tools.attach.VirtualMachine; |
| 11 | +import com.sun.tools.attach.VirtualMachineDescriptor; |
| 12 | + |
| 13 | +public class App { |
| 14 | + |
| 15 | + private static final String SELF; |
| 16 | + |
| 17 | + private static boolean CAN_ATTACH; |
| 18 | + |
| 19 | + static{ |
| 20 | + String self; |
| 21 | + try { |
| 22 | + self = App.class.getProtectionDomain() |
| 23 | + .getCodeSource() |
| 24 | + .getLocation() |
| 25 | + .toURI() |
| 26 | + .getPath(); |
| 27 | + } catch (URISyntaxException e) { |
| 28 | + self=null; |
| 29 | + } |
| 30 | + SELF=self; |
| 31 | + //Need the agent's location or Attach won't work. |
| 32 | + try { |
| 33 | + Class.forName("com.sun.tools.attach.VirtualMachine"); |
| 34 | + CAN_ATTACH=SELF!=null; |
| 35 | + } catch (ClassNotFoundException e) { |
| 36 | + CAN_ATTACH=false; |
| 37 | + } |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + public static void main(String[] args){ |
| 42 | + System.out.println("SafeLog4j by Contrast Security"); |
| 43 | + |
| 44 | + if(args.length>0 && CAN_ATTACH){ |
| 45 | + String options = args.length>=2 ? args[1] : null; |
| 46 | + patch(args[0].trim(), options); |
| 47 | + }else{ |
| 48 | + showHelp(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static void listProcesses(){ |
| 53 | + List<VirtualMachineDescriptor> vms = VirtualMachine.list(); |
| 54 | + vms.stream() |
| 55 | + .filter(vm -> !vm.displayName().contains("safelog")) //No need to patch ourselves |
| 56 | + .forEach(vm -> { |
| 57 | + System.out.println(vm.id() + " \t" + vm.displayName()); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + private static void showHelp(){ |
| 62 | + System.out.println("This tool can be used in two modes for custom and third party applications:"); |
| 63 | + System.out.println("1. At application startup, through the -javaagent flag."); |
| 64 | + System.out.println("2. Without application restart, if supported."); |
| 65 | + |
| 66 | + if(!CAN_ATTACH){ |
| 67 | + System.out.println(" -- Option 2 is not supported on this system."); |
| 68 | + }else{ |
| 69 | + System.out.println(); |
| 70 | + System.out.println("SafeLog4j can connect to and patch the following Java processes:"); |
| 71 | + try{ |
| 72 | + listProcesses(); |
| 73 | + |
| 74 | + System.out.println(); |
| 75 | + System.out.println("To patch a process, add an argument of the ID or use the word all."); |
| 76 | + }catch(NoClassDefFoundError err){ |
| 77 | + System.err.println("Please use jcmd to list Java processes."); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static void patch(String process, String options){ |
| 83 | + try{ |
| 84 | + int pid = Integer.parseInt(process); |
| 85 | + patch(pid, options); |
| 86 | + }catch(NumberFormatException ex){ |
| 87 | + if("all".equalsIgnoreCase(process)){ |
| 88 | + System.out.println("Patching all..."); |
| 89 | + List<VirtualMachineDescriptor> vms = VirtualMachine.list(); |
| 90 | + vms.stream() |
| 91 | + .filter(vm -> !vm.displayName().contains("safelog")) //No need to patch ourselves |
| 92 | + .map(vm -> vm.id()) |
| 93 | + .forEach(id -> patch(Integer.parseInt(id), options)); |
| 94 | + }else{ |
| 95 | + System.err.println("Uknown process: " + process); |
| 96 | + System.err.println(" Use a number or all."); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void patch(int process, String options){ |
| 102 | + try { |
| 103 | + VirtualMachine vm = VirtualMachine.attach(String.valueOf(process)); |
| 104 | + vm.loadAgent(SELF, options); |
| 105 | + System.out.println("Successfully patched process " |
| 106 | + + process |
| 107 | + + ". You should see safelog4j messages in its log."); |
| 108 | + } catch (AttachNotSupportedException | IOException e) { |
| 109 | + if("Non-numeric value found - int expected".equals(e.getMessage())){ |
| 110 | + //Odd interplay between majors. https://bugs.eclipse.org/bugs/show_bug.cgi?id=534489 |
| 111 | + String javaHome = System.getProperty("java.home"); |
| 112 | + System.err.println("Unable to bridge Java version to process " |
| 113 | + + process |
| 114 | + + ". Please re-run this tool from its Java installation instead of " + javaHome); |
| 115 | + }else{ |
| 116 | + System.err.println("Unable to connect to " + process + ". " + e.getMessage()); |
| 117 | + } |
| 118 | + } catch (AgentLoadException | AgentInitializationException e) { |
| 119 | + System.err.println("Attempt to patch was unsuccessful. Process " |
| 120 | + + process |
| 121 | + + " must be restarted to be patched. " |
| 122 | + + e.getMessage()); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments