议题解析与复现--Java内存攻击技术漫谈

allowAttachSelf绕过

在Java9及以后的版本不允许SelfAttach(即无法attach自身的进程),如图

image-20211029091820625

调试一下,发现这里ALLOW_ATTACH_SELF字段设置为false

image-20211029093656259

步入getSavedProperty,最终到ImmitableCollections中的table中去查找allowAttachSelf,找不到,返回空

image-20211029094925925

之后,这里进行了ALLOW_ATTACH_SELF字段的检测,若不为true则抛出异常

image-20211029093254605

这样看来有两种方法对这个检验进行绕过一种是使用反射直接更改HotSpotVirtualMachine中的ALLOW_ATTACH_SELF字段,另一种是想办法在ImmitableCollections中的table中添加jdk.attach.allowAttachSelf。

rebeyond师傅使用的是第一种方法。

 
Field field=cls.getDeclaredField("ALLOW_ATTACH_SELF");
field.setAccessible(true);
Field modifiersField=Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field,field.getModifiers()&~Modifier.FINAL);
field.setBoolean(null,true);

这样便完成了allowAttachSelf机制的绕过。

内存马防检测

instrument机制实现类agent内存马的注入,但是也可以实现对内存马进行检测。

这里给出的方法就是注入内存马后将instrument机制破坏的,使其无法检测进程的类字节码等。

以下为instrument的工作流程

image-20211029105648919

1.检测工具作为Client,根据指定的PID,向目标JVM发起attach请求;
2.JVM收到请求后,做一些校验(比如上文提到的jdk.attach.allowAttachSelf的校验),校验通过后,会打开一个IPC通道。
3.接下来Client会封装一个名为AttachOperation的C++对象,发送给Server端;
4.Server端会把Client发过来的AttachOperation对象放入一个队列;
5.Server端另外一个线程会从队列中取出AttachOperation对象并解析,然后执行对应的操作,并把执行结果通过IPC通道返回Client。

windows端

现在loadAgent处下断点,步入调试。

image-20211029111517246

步入,执行execute方法

image-20211029112529339

看一下execute方法

 
InputStream execute(String cmd, Object ... args)
throws AgentLoadException, IOException
{
assert args.length <= 3; // includes null
// create a pipe using a random name
Random rnd = new Random();
int r = rnd.nextInt();
String pipeprefix = "\\\\.\\pipe\\javatool";
String pipename = pipeprefix + r;
long hPipe;
try {
hPipe = createPipe(pipename);//创建pipe管道
} catch (IOException ce) {
// Retry with another random pipe name.
r = rnd.nextInt();
pipename = pipeprefix + r;
hPipe = createPipe(pipename);
}
// check if we are detached - in theory it's possible that detach is invoked
// after this check but before we enqueue the command.
if (hProcess == -1) {
closePipe(hPipe);
throw new IOException("Detached from target VM");
}
try {
// enqueue the command to the process
enqueue(hProcess, stub, cmd, pipename, args);//调用enqueue方法
....

这个enqueue是native方法。

image-20211029153241852

看一下这个方法的源码

 
/*
* Class: sun_tools_attach_WindowsVirtualMachine
* Method: enqueue
* Signature: (JZLjava/lang/String;[Ljava/lang/Object;)V
*/
JNIEXPORT void JNICALL Java_sun_tools_attach_WindowsVirtualMachine_enqueue
(JNIEnv *env, jclass cls, jlong handle, jbyteArray stub, jstring cmd,
jstring pipename, jobjectArray args)
{
DataBlock data;
DataBlock* pData;
DWORD* pCode;
DWORD stubLen;
HANDLE hProcess, hThread;
jint argsLen, i;
jbyte* stubCode;
jboolean isCopy;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值