我正在尝试通过中继所有流量并挑选出要修改的某些流量来对HTTPS
连接执行man-in-the-middle
攻击。我研究了LittleProxy-mitm
和LittleProxy
,这些项目看起来非常有前途。我按照自述文件编写了我的代码,但是连接在此代理运行时不起作用。我的完整代码如下:
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.util.AttributeKey;
import org.littleshoot.proxy.HttpFilters;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.littleshoot.proxy.mitm.CertificateSniffingMitmManager;
import org.littleshoot.proxy.mitm.RootCertificateException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
public class Main
{
private static final AttributeKey<String> CONNECTED_URL = AttributeKey.valueOf("connected_url");
public static void main(String[] arguments) throws RootCertificateException
{
System.out.println("Starting HTTP proxy server...");
DefaultHttpProxyServer.bootstrap()
.withPort(9090)
.withManInTheMiddle(new CertificateSniffingMitmManager())
.withFiltersSource(new HttpFiltersSourceAdapter()
{
@Override
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx)
{
String uri = originalRequest.getUri();
if (originalRequest.getMethod() == HttpMethod.CONNECT)
{
if (ctx != null)
{
String prefix = "https://" + uri.replaceFirst(":443$", "");
ctx.channel().attr(CONNECTED_URL).set(prefix);
}
return new HttpFiltersAdapter(originalRequest, ctx);
}
String connectedUrl = ctx.channel().attr(CONNECTED_URL).get();
if (connectedUrl == null)
{
return new MyHttpFilters(uri);
}
return new MyHttpFilters(connectedUrl + uri);
}
})
.start();
System.out.println("HTTP proxy server started...");
}
private static void printHeaders(DefaultHttpRequest httpObject)
{
final HttpHeaders headers = httpObject.headers();
final List<Map.Entry<String, String>> entries = headers.entries();
for (Map.Entry<String, String> entry : entries)
{
final String value = entry.getValue();
System.out.println(entry.getKey() + " -> " + value);
}
System.out.println();
}
public static class MyHttpFilters implements HttpFilters
{
private final String uri;
MyHttpFilters(String uri)
{
this.uri = uri;
}
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject)
{
((DefaultHttpRequest) httpObject).setUri(uri);
printHeaders((DefaultHttpRequest) httpObject);
return (HttpResponse) httpObject;
}
@Override
public HttpResponse proxyToServerRequest(HttpObject httpObject)
{
((DefaultHttpRequest) httpObject).setUri(uri);
printHeaders((DefaultHttpRequest) httpObject);
return (HttpResponse) httpObject;
}
@Override
public void proxyToServerRequestSending()
{
}
@Override
public void proxyToServerRequestSent()
{
}
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject)
{
return null;
}
@Override
public void serverToProxyResponseTimedOut()
{
}
@Override
public void serverToProxyResponseReceiving()
{
}
@Override
public void serverToProxyResponseReceived()
{
}
@Override
public HttpObject proxyToClientResponse(HttpObject httpObject)
{
return null;
}
@Override
public void proxyToServerConnectionQueued()
{
}
@Override
public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort)
{
return null;
}
@Override
public void proxyToServerResolutionFailed(String hostAndPort)
{
}
@Override
public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress)
{
}
@Override
public void proxyToServerConnectionStarted()
{
}
@Override
public void proxyToServerConnectionSSLHandshakeStarted()
{
}
@Override
public void proxyToServerConnectionFailed()
{
}
@Override
public void proxyToServerConnectionSucceeded(ChannelHandlerContext serverCtx)
{
}
}
}
打印请求的标头,但通信仍然中断。我做错了什么?如果我按照此处所述使用过滤器运行HTTP
版本,则连接工作正常。不幸的是,对于HTTP
版本,所有HTTPS
流量都被忽略。
**我已经更改了您代码中的几行,只需运行它.它应该有效。确保更改浏览器网络设置以使用代理**
package com.example.ProxyTest.mitm;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.util.AttributeKey;
import org.littleshoot.proxy.HttpFilters;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.littleshoot.proxy.mitm.CertificateSniffingMitmManager;
import org.littleshoot.proxy.mitm.RootCertificateException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
public class mitmExample1
{
private static final AttributeKey<String> CONNECTED_URL = AttributeKey.valueOf("connected_url");
public static void main(String[] arguments) throws RootCertificateException
{
System.out.println("Starting HTTP proxy server...");
DefaultHttpProxyServer.bootstrap()
.withPort(9090)
.withManInTheMiddle(new CertificateSniffingMitmManager())
.withFiltersSource(new HttpFiltersSourceAdapter()
{
@Override
public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx)
{
String uri = originalRequest.getUri();
if (originalRequest.getMethod() == HttpMethod.CONNECT)
{
if (ctx != null)
{
String prefix = "https://" + uri.replaceFirst(":443$", "");
ctx.channel().attr(CONNECTED_URL).set(prefix);
}
return new HttpFiltersAdapter(originalRequest, ctx);
}
String connectedUrl = ctx.channel().attr(CONNECTED_URL).get();
if (connectedUrl == null)
{
return new MyHttpFilters(uri);
}
return new MyHttpFilters(connectedUrl + uri);
}
})
.start();
System.out.println("HTTP proxy server started...");
}
private static void printHeaders(DefaultHttpRequest httpObject)
{
final HttpHeaders headers = httpObject.headers();
final List<Map.Entry<String, String>> entries = headers.entries();
for (Map.Entry<String, String> entry : entries)
{
final String value = entry.getValue();
System.out.println(entry.getKey() + " -> " + value);
}
System.out.println();
}
public static class MyHttpFilters implements HttpFilters
{
private final String uri;
MyHttpFilters(String uri)
{
this.uri = uri;
}
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject)
{
return null;
}
//
@Override
public HttpResponse proxyToServerRequest(HttpObject httpObject)
{
return null;
}
@Override
public void proxyToServerRequestSending()
{
}
@Override
public void proxyToServerRequestSent()
{
}
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject)
{
return httpObject;
}
@Override
public void serverToProxyResponseTimedOut()
{
}
@Override
public void serverToProxyResponseReceiving()
{
}
@Override
public void serverToProxyResponseReceived()
{
}
@Override
public HttpObject proxyToClientResponse(HttpObject httpObject)
{
return httpObject;
}
@Override
public void proxyToServerConnectionQueued()
{
}
@Override
public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort)
{
return null;
}
@Override
public void proxyToServerResolutionFailed(String hostAndPort)
{
}
@Override
public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress)
{
}
@Override
public void proxyToServerConnectionStarted()
{
}
@Override
public void proxyToServerConnectionSSLHandshakeStarted()
{
}
@Override
public void proxyToServerConnectionFailed()
{
}
@Override
public void proxyToServerConnectionSucceeded(ChannelHandlerContext serverCtx)
{
}
}
}