如何使用 Sharppcap 获取 http 内容
how to get http content with Sharppcap
对于我的学校项目,我们必须在中间做一个人。但是我们没有成功获取 HTTP 内容。我写了一些东西来重新组装 tcp 有效负载数据,但我只得到 HTTP Header 而不是源代码。我在 wireshark 上看到数据是未压缩的,但我不知道这是什么意思。
这是我的代码
TcpSession class
public class TcpSession : IDisposable
{
private MemoryStream _clientBuffer;
private MemoryStream _serverBuffer;
public TcpSession()
{
_clientBuffer = new MemoryStream();
_serverBuffer = new MemoryStream();
}
public void WritePacket(TcpPacket packet, bool toServer)
{
if (toServer)
{
_clientBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
}
else
{
_serverBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
}
}
public byte[] GetBuffer(bool toServer)
{
if (toServer)
{
return _clientBuffer.ToArray();
}
return _serverBuffer.ToArray();
}
public void Dispose()
{
_clientBuffer?.Dispose();
_serverBuffer?.Dispose();
}
}
PacketAssembler
public class PacketAssembler
{
public delegate void OnPacketReassembledDelegate(TcpSession session);
public OnPacketReassembledDelegate OnPacketReassembled;
private Dictionary<int, TcpSession> _tcpSessions;
public PacketAssembler()
{
_tcpSessions = new Dictionary<int, TcpSession>();
}
public void AddPacket(TcpPacket packet, bool toServer)
{
// Client port (unique) is used for the dictionary.
int port = toServer ? packet.SourcePort : packet.DestinationPort;
if (packet.Syn) // Connection established
{
if (!_tcpSessions.ContainsKey(port))
{
_tcpSessions.Add(port, new TcpSession());
}
}
else if (packet.Psh) // Push data
{
_tcpSessions[port].WritePacket(packet, toServer);
}
else if (packet.Rst) // reset the session
{
_tcpSessions.Remove(port);
}
else if (packet.Fin) // End the session
{
OnPacketReassembled(_tcpSessions[port]);
_tcpSessions.Remove(port);
}
}
}
你检查过packetnet-connections了吗? https://github.com/chmorgan/packetnet-connections
对于我的学校项目,我们必须在中间做一个人。但是我们没有成功获取 HTTP 内容。我写了一些东西来重新组装 tcp 有效负载数据,但我只得到 HTTP Header 而不是源代码。我在 wireshark 上看到数据是未压缩的,但我不知道这是什么意思。
这是我的代码 TcpSession class
public class TcpSession : IDisposable
{
private MemoryStream _clientBuffer;
private MemoryStream _serverBuffer;
public TcpSession()
{
_clientBuffer = new MemoryStream();
_serverBuffer = new MemoryStream();
}
public void WritePacket(TcpPacket packet, bool toServer)
{
if (toServer)
{
_clientBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
}
else
{
_serverBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
}
}
public byte[] GetBuffer(bool toServer)
{
if (toServer)
{
return _clientBuffer.ToArray();
}
return _serverBuffer.ToArray();
}
public void Dispose()
{
_clientBuffer?.Dispose();
_serverBuffer?.Dispose();
}
}
PacketAssembler
public class PacketAssembler
{
public delegate void OnPacketReassembledDelegate(TcpSession session);
public OnPacketReassembledDelegate OnPacketReassembled;
private Dictionary<int, TcpSession> _tcpSessions;
public PacketAssembler()
{
_tcpSessions = new Dictionary<int, TcpSession>();
}
public void AddPacket(TcpPacket packet, bool toServer)
{
// Client port (unique) is used for the dictionary.
int port = toServer ? packet.SourcePort : packet.DestinationPort;
if (packet.Syn) // Connection established
{
if (!_tcpSessions.ContainsKey(port))
{
_tcpSessions.Add(port, new TcpSession());
}
}
else if (packet.Psh) // Push data
{
_tcpSessions[port].WritePacket(packet, toServer);
}
else if (packet.Rst) // reset the session
{
_tcpSessions.Remove(port);
}
else if (packet.Fin) // End the session
{
OnPacketReassembled(_tcpSessions[port]);
_tcpSessions.Remove(port);
}
}
}
你检查过packetnet-connections了吗? https://github.com/chmorgan/packetnet-connections