NTLM认证的proxy客户端的Java实现-NTLM_Proxy.java
来源:互联网 发布:区域增长算法代码 编辑:程序博客网 时间:2024/04/29 10:03
程序在Windows和MacOS上编译运行成功。
下面是实现流程的主程序。
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ProxySelector;
import java.net.Socket;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.security.Key;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class NTLM_Proxy {
private static byte[] str2bytes(String str) {
char[] chars = str.toCharArray();
byte[] bytes = new byte[chars.length];
int i;
for (i = 0; i < chars.length; i++) {
bytes[i] = (byte) (chars[i] & 0xFF);
}
return bytes;
}
static void detect_proxy_auth() {
String auth_method = "<none>";
try {
Socket sock = new Socket("192.168.12.240", 8080);
InputStream sp_in = sock.getInputStream();
OutputStream sp_out = sock.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(sp_in));
BufferedOutputStream buff_out = new BufferedOutputStream(sp_out,1460);
/* authenticate against a proxy host */
String connect = "CONNECT " + "10.4.59.20" + ":" + 443
+ " HTTP/1.0/r/n/r/n";
/* send the CONNECT call */
/* since we use 1-byte chars */
buff_out.write(str2bytes(connect), 0, connect.length());
buff_out.flush(); /* no more to write */
/* read the response */
String header;
while ((header = reader.readLine()) != null) {
if (header.length() == 0) {
break; /* end of headers */
}
header.trim();
System.out.println("Recv:" + header);
}
/* close the connection now since we don't need it */
sock.close();
sock = null;
/* if authentication is required, do it now */
System.out.println("ntlm");
NTLM ntlm = new NTLM();
String s = ntlm.getResponseFor("", "user", "passwd", "", "");
System.out.println("ret:" + s);
sock = new Socket("192.168.12.240", 8080);
sp_in = sock.getInputStream();
sp_out = sock.getOutputStream();
reader = new BufferedReader(new InputStreamReader(sp_in));
buff_out = new BufferedOutputStream(sp_out, 1460);
/* authenticate against a proxy host */
connect = "CONNECT 10.4.59.20:443 HTTP/1.0/r/nProxy-Authorization: NTLM " + s + "/r/n/r/n";
/* send the CONNECT call */
/* since we use 1-byte chars */
buff_out.write(str2bytes(connect), 0, connect.length());
buff_out.flush(); /* no more to write */
/* read the response */
// String header;
header = "";
while ((header = reader.readLine()) != null) {
if (header.length() == 0) {
break; /* end of headers */
}
header.trim();
System.out.println("Recv:" + header);
/* authenticate against a proxy host */
if (header.toLowerCase().startsWith("proxy-authenticate")) {
auth_method = header.substring(header.indexOf(":") + 6)
.trim();
System.out.println("auth_method: " + auth_method);
}
}
/* close the connection now since we don't need it */
// sock.close();
// sock = null;
String s1 = ntlm.getResponseFor(auth_method, "user", "passwd","", "");
//sock = new Socket("192.168.12.240", 8080);
sp_in = sock.getInputStream();
sp_out = sock.getOutputStream();
reader = new BufferedReader(new InputStreamReader(sp_in));
buff_out = new BufferedOutputStream(sp_out, 1460);
/* authenticate against a proxy host */
connect = "CONNECT 10.4.59.20:443 HTTP/1.0/r/n";
String keepalive = "Proxy-Connection: Keep-Alive/r/n/r/n";
String AuthorizationStr = "Proxy-Authorization: NTLM " + s1 + "/r/n/r/n";
/* send the CONNECT call */
/* since we use 1-byte chars */
buff_out.write(str2bytes(connect), 0, connect.length());
buff_out.write(str2bytes(AuthorizationStr), 0, AuthorizationStr.length());
/* since we use 1-byte chars */
buff_out.write(str2bytes(keepalive), 0, keepalive.length());
buff_out.flush(); /* no more to write */
/* read the response */
// String header;
header = "";
while ((header = reader.readLine()) != null) {
if (header.length() == 0) {
break; /* end of headers */
}
header.trim();
System.out.println("OK! Recv:" + header);
}
/* close the connection now since we don't need it */
sock.close();
sock = null;
// return;
} catch (Exception e) {
/* detect no proxy, continue the next step, don't fail */
}
}
public static void main(String arg[]) {
detect_proxy_auth();
}
}
- NTLM认证的proxy客户端的Java实现-NTLM_Proxy.java
- NTLM认证的proxy客户端的Java实现-NTLM.java
- NTLM认证的proxy客户端的Java实现-Base64.java
- 基于NTLM的Proxy认证
- 基于NTLM的Proxy认证
- 基于NTLM的Proxy认证
- NTLM认证proxy的认证流程
- NTLM的PROXY穿越
- 关于NTLM认证的问题
- SMB的NTLM认证过程与NTLM挑战的编程实现
- SMB的NTLM认证过程与NTLM挑战的编程实现
- SMB的NTLM认证过程与NTLM挑战的编程实现
- 老紫竹提高教程 - Java代理服务器和NTLM认证的使用
- 嵌入式系统NTLM/NTLMV2身份认证的C语言实现
- Java 实现 SSH 协议的客户端登录认证方式
- Java 实现 SSH 协议的客户端登录认证方式
- 【JAVA】 代理Proxy的实现
- NTLM SSO的实现
- isdn是什么
- 什么叫路由
- 这道数据类型题 错啦???
- 关于最后一公里
- 什么是VPN代理,VPN代理有哪些优点
- NTLM认证的proxy客户端的Java实现-NTLM_Proxy.java
- FPGA 简介
- Net有四个判等函数(Equal ,==,refernceEqual, 以及静态Equal的区别)
- msdn是什么
- 前辈程序员光荣的一生
- 嵌入式产品开发竞赛 高职组
- 如何使电脑进入DOS
- 主动FTP与被动FTP
- 在HTML中用Javascript接收参数