mirror of
https://github.com/qi4L/JYso.git
synced 2026-09-21 22:40:43 +08:00
feat: 添加JDK信任证书
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.qi4l.JYso;
|
package com.qi4l.JYso;
|
||||||
|
|
||||||
import com.qi4l.JYso.gadgets.Config.Config;
|
import com.qi4l.JYso.gadgets.Config.Config;
|
||||||
|
|
||||||
import javax.net.ssl.*;
|
import javax.net.ssl.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -12,25 +13,32 @@ import java.nio.file.Paths;
|
|||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import static org.fusesource.jansi.Ansi.ansi;
|
import static org.fusesource.jansi.Ansi.ansi;
|
||||||
|
|
||||||
public class TLSProxy {
|
public class TLSProxy {
|
||||||
private final String localAddr;
|
private final String localAddr;
|
||||||
private final String remoteAddr;
|
private final String remoteAddr;
|
||||||
private final String certFile;
|
private final String certFile;
|
||||||
|
private final String keyPass;
|
||||||
|
|
||||||
public TLSProxy(String localAddr, String remoteAddr, String certFile) {
|
public TLSProxy(String localAddr, String remoteAddr, String certFile, String keyPass) {
|
||||||
this.localAddr = localAddr;
|
this.localAddr = localAddr;
|
||||||
this.remoteAddr = remoteAddr;
|
this.remoteAddr = remoteAddr;
|
||||||
this.certFile = certFile;
|
this.certFile = certFile;
|
||||||
|
this.keyPass = keyPass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void start() {
|
public static void start() {
|
||||||
System.out.println(ansi().render("@|green [+]|@ LDAPS Server Start Listening on >> " + Config.TLSPort + "..."));
|
System.out.println(ansi().render("@|green [+]|@ LDAPS Server Start Listening on >> " + Config.TLSPort + "..."));
|
||||||
new TLSProxy(Config.ip + ":" + Config.TLSPort, Config.ip + ":" + Config.ldapPort, Config.certFile).run();
|
new TLSProxy(Config.ip + ":" + Config.TLSPort, Config.ip + ":" + Config.ldapPort, Config.certFile,Config.keyPass).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
// 设置JDK信任证书
|
||||||
|
System.setProperty("javax.net.ssl.trustStore", certFile);
|
||||||
|
System.setProperty("javax.net.ssl.trustStorePassword", keyPass);
|
||||||
|
|
||||||
SSLServerSocketFactory sslServerSocketFactory = createSSLServerSocketFactory();
|
SSLServerSocketFactory sslServerSocketFactory = createSSLServerSocketFactory();
|
||||||
if (sslServerSocketFactory == null) {
|
if (sslServerSocketFactory == null) {
|
||||||
System.err.println("Failed to create SSLServerSocketFactory");
|
System.err.println("Failed to create SSLServerSocketFactory");
|
||||||
@@ -53,15 +61,15 @@ public class TLSProxy {
|
|||||||
|
|
||||||
private SSLServerSocketFactory createSSLServerSocketFactory() {
|
private SSLServerSocketFactory createSSLServerSocketFactory() {
|
||||||
try {
|
try {
|
||||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||||
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||||
KeyStore keyStore = KeyStore.getInstance("JKS");
|
KeyStore keyStore = KeyStore.getInstance("JKS");
|
||||||
|
|
||||||
try (InputStream keyInput = Files.newInputStream(Paths.get(certFile))) {
|
try (InputStream keyInput = Files.newInputStream(Paths.get(certFile))) {
|
||||||
keyStore.load(keyInput, Config.keyPass.toCharArray());
|
keyStore.load(keyInput, keyPass.toCharArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
keyManagerFactory.init(keyStore, Config.keyPass.toCharArray());
|
keyManagerFactory.init(keyStore, keyPass.toCharArray());
|
||||||
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
|
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
|
||||||
return sslContext.getServerSocketFactory();
|
return sslContext.getServerSocketFactory();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -72,7 +80,7 @@ public class TLSProxy {
|
|||||||
|
|
||||||
private void handleConnection(SSLSocket clientSocket) {
|
private void handleConnection(SSLSocket clientSocket) {
|
||||||
String[] remoteAddressParts = remoteAddr.split(":");
|
String[] remoteAddressParts = remoteAddr.split(":");
|
||||||
Socket remoteSocket = null;
|
Socket remoteSocket = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 修复:使用 remoteAddr 而不是 localAddr
|
// 修复:使用 remoteAddr 而不是 localAddr
|
||||||
@@ -130,7 +138,7 @@ public class TLSProxy {
|
|||||||
|
|
||||||
private void forwardData(InputStream input, OutputStream output) throws IOException {
|
private void forwardData(InputStream input, OutputStream output) throws IOException {
|
||||||
byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[8192];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while ((bytesRead = input.read(buffer)) != -1) {
|
while ((bytesRead = input.read(buffer)) != -1) {
|
||||||
|
|||||||
@@ -46,10 +46,10 @@ public class Config {
|
|||||||
public static boolean TLSProxy = false;
|
public static boolean TLSProxy = false;
|
||||||
@Parameter(names = {"-tP", " --TLSPort"}, help = true, description = "TLS port", order = 5)
|
@Parameter(names = {"-tP", " --TLSPort"}, help = true, description = "TLS port", order = 5)
|
||||||
public static String TLSPort = "1636";
|
public static String TLSPort = "1636";
|
||||||
@Parameter(names = {"-kS", " --keyPass"}, help = true, description = "TLS private key", order = 5)
|
@Parameter(names = {"-kP", " --keyPass"}, help = true, description = "TLS private key", order = 5)
|
||||||
public static String keyPass = "";
|
public static String keyPass = "326q999h80s8";
|
||||||
@Parameter(names = {"-cF", " --certFile"}, help = true, description = "Path to the TLS certificate file", order = 5)
|
@Parameter(names = {"-cF", " --certFile"}, help = true, description = "JKS certificate file Path", order = 5)
|
||||||
public static String certFile = "";
|
public static String certFile = "C:\\Users\\22393\\Downloads\\qi4l.top_jks\\qi4l.top.jks";
|
||||||
|
|
||||||
@Parameter(names = {"-j", "--jndi"}, help = true, description = "starter", order = 5)
|
@Parameter(names = {"-j", "--jndi"}, help = true, description = "starter", order = 5)
|
||||||
public static boolean jndi = false;
|
public static boolean jndi = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user