我正在尝试通过中继所有流量并挑选某些流量进行修改来对man-in-the-middle
连接执行 HTTPS
攻击。我研究了 LittleProxy-mitm
和 LittleProxy
,这些项目看起来非常有前途。 I followed the readme编写我的代码,但通过此代理运行连接时不起作用。我完整的代码如下:
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)
{
}
}
}
请求的标题已打印,但通信仍然中断。我究竟做错了什么?如果我按照here所述使用过滤器运行
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)
{
}
}
}