forked from Luohuayu/CatServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
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
73 changes: 73 additions & 0 deletions
73
src/fmllauncher/java/catserver/server/utils/Log4j2_3201_Fixer.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,73 @@ | ||
package catserver.server.utils; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.regex.Pattern; | ||
import org.apache.logging.log4j.core.appender.AbstractManager; | ||
import org.apache.logging.log4j.core.net.JndiManager; | ||
|
||
public class Log4j2_3201_Fixer { | ||
private final static Pattern REGEX = Pattern.compile("(?i)\\$\\{(jndi|ctx|date|env|event|java|jvmrunargs|log4j|lower|main|map|marker|bundle|sd|sys|upper|):[\\s\\S]*}"); | ||
|
||
public static boolean match(String message) { | ||
if (message != null) { | ||
return REGEX.matcher(message.replaceAll("\u00a7[a-zA-Z0-9]", "").replaceAll("(\\s|\\n\\r)", "")).find(); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public static void matchThrowException(String message) throws RuntimeException { | ||
if (match(message)) { | ||
throw new RuntimeException("Detected log4j2 3201 bug! Message: " + message.replace("$", "\\u0024")); | ||
} | ||
} | ||
|
||
public static boolean matchPrintException(String message) { | ||
if (match(message)) { | ||
new RuntimeException("Detected log4j2 3201 bug! Message: " + message.replace("$", "\\u0024")).printStackTrace(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean matchPrintMessage(String message) { | ||
if (match(message)) { | ||
System.out.println("Detected log4j2 3201 bug! Message: " + message.replace("$", "\\u0024")); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static void disableJndiLookup() { | ||
try { | ||
Field fieldLock = AbstractManager.class.getDeclaredField("LOCK"); | ||
Field fieldNap = AbstractManager.class.getDeclaredField("MAP"); | ||
fieldLock.setAccessible(true); | ||
fieldNap.setAccessible(true); | ||
|
||
Lock lock = (Lock) fieldLock.get(null); | ||
Map<String, AbstractManager> map = (Map<String, AbstractManager>) fieldNap.get(null); | ||
|
||
lock.lock(); | ||
map.put(JndiManager.class.getName(), new AbstractManager(null, JndiManager.class.getName()) { | ||
private final RuntimeException disallowException = new RuntimeException() { | ||
public synchronized Throwable fillInStackTrace() | ||
{ | ||
this.setStackTrace(new StackTraceElement[0]); | ||
return this; | ||
} | ||
}; | ||
|
||
@Override | ||
public void updateData(Object data) { | ||
throw disallowException; | ||
} | ||
}); | ||
lock.unlock(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
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