FiddlerCore:如何处理TCP流量?

时间:2014-12-30 12:39:47

标签: http tcp fiddler fiddlercore

我想使用fiddlercore作为反向代理 - 解析HTTP请求并根据HTTP标头将它们重定向到另一个端口。但我还需要处理TCP请求并将它们重定向到另一个端口。是否可以使用FiddlerCore?以某种方式确定它不是HTTP请求,只是TCP。

  

我发现事件名为BeforeReturningError,但未调用。   只触发事件BeforeSocketAccept,但这还不够。当FiddlerCore无法解析HTTP头时,我还没有找到任何可能被调用的事件。

2 个答案:

答案 0 :(得分:1)

Fiddler和FiddlerCore仅处理HTTP / HTTPS流量。它们不是为处理原始TCP流量而设计的;你可以使用netcat这样的东西。

答案 1 :(得分:0)

要使用TCP数据包,您可以使用Pcap库。因此,fiddlercore不使用TCP或UDP数据包。

 private void PacketHandler(Packet packet)
    {
        this.count = ""; this.time = ""; this.source = ""; this.destination = ""; this.protocol = ""; this.length = "";

        this.tcpack = ""; this.tcpsec = ""; this.tcpnsec = ""; this.tcpsrc = ""; this.tcpdes = ""; this.udpscr = "";

        this.udpdes = ""; this.httpheader = ""; this.httpver = ""; this.httplen = ""; this.reqres = ""; this.httpbody = "";

        IpV4Datagram ip = packet.Ethernet.IpV4;
        TcpDatagram tcp = ip.Tcp;
        UdpDatagram udp = ip.Udp;
        HttpDatagram httpPacket=null;


        if (ip.Protocol.ToString().Equals("Tcp"))
        {
            httpPacket = tcp.Http;//Initialize http variable only if the packet was tcp

            if ((httpPacket.Header != null) && (!_tcp.Checked))
            {
                protocol = "Http";
                httpheader = httpPacket.Header.ToString();
                count = packet.Count.ToString();
                time = packet.Timestamp.ToString();
                this.source = ip.Source.ToString();
                this.destination = ip.Destination.ToString();
                length = ip.Length.ToString();
                httpver = httpPacket.Version.ToString();
                httplen = httpPacket.Length.ToString();
                httpbody = httpPacket.Body.ToString();

                if (httpPacket.IsRequest)
                {
                    reqres = "Request";
                }
                else
                {
                    reqres = "Response";
                }
            }

            else
            {

                count = packet.Count.ToString();
                time = packet.Timestamp.ToString();
                this.source = ip.Source.ToString();
                this.destination = ip.Destination.ToString();
                length = ip.Length.ToString();
                protocol = ip.Protocol.ToString();

                tcpsrc = tcp.SourcePort.ToString();
                tcpdes = tcp.DestinationPort.ToString();
                tcpack = tcp.AcknowledgmentNumber.ToString();
                tcpsec = tcp.SequenceNumber.ToString();
                tcpnsec = tcp.NextSequenceNumber.ToString();
            }


        }
        else
        {
            if ((ip.Protocol.ToString().Equals("Udp")))
            {
                count = packet.Count.ToString();
                time = packet.Timestamp.ToString();
                this.source = ip.Source.ToString();
                this.destination = ip.Destination.ToString();
                length = ip.Length.ToString();
                protocol = ip.Protocol.ToString();
                udpscr = udp.SourcePort.ToString();
                udpdes = udp.DestinationPort.ToString();
            }
            else
            {

                count = packet.Count.ToString();
                time = packet.Timestamp.ToString();
                this.source = ip.Source.ToString();
                this.destination = ip.Destination.ToString();
                length = ip.Length.ToString();
                protocol = ip.Protocol.ToString();

            }
        }


        if (ip.Protocol.ToString().Equals("Tcp")&&(save.Checked))
        {
            int _source = tcp.SourcePort;
            int _destination = tcp.DestinationPort;

            if (tcp.PayloadLength != 0) //not syn or ack
            {
                payload = new byte[tcp.PayloadLength];
                tcp.Payload.ToMemoryStream().Read(payload, 0, tcp.PayloadLength);// read payload from 0 to length
                if (_destination == 80)// request from server
                {
                    Packet1 packet1 = new Packet1();
                    int i = Array.IndexOf(payload, (byte)32, 6);
                    byte[] t = new byte[i - 5];
                    Array.Copy(payload, 5, t, 0, i - 5);
                    packet1.Name = System.Text.ASCIIEncoding.ASCII.GetString(t);

                    if (!packets.ContainsKey(_source))
                        packets.Add(_source, packet1);
                }
                else
                    if (_source == 80)
                        if (packets.ContainsKey(_destination))
                        {
                            Packet1 packet1 = packets[_destination];
                            if (packet1.Data == null)
                            {
                                if ((httpPacket.Header != null) && (httpPacket.Header.ContentLength != null))
                                {
                                    packet1.Data = new byte[(uint)httpPacket.Header.ContentLength.ContentLength];
                                    Array.Copy(httpPacket.Body.ToMemoryStream().ToArray(), packet1.Data, httpPacket.Body.Length);
                                    packet1.Order = (uint)(tcp.SequenceNumber + payload.Length - httpPacket.Body.Length);
                                    packet1.Data_Length = httpPacket.Body.Length;
                                    for (int i = 0; i < packet1.TempPackets.Count; i++)
                                    {
                                        Temp tempPacket = packet1.TempPackets[i];
                                        Array.Copy(tempPacket.data, 0, packet1.Data, tempPacket.tempSeqNo - packet1.Order, tempPacket.data.Length);
                                        packet1.Data_Length += tempPacket.data.Length;
                                    }
                                }
                                else
                                {
                                    Temp tempPacket = new Temp();
                                    tempPacket.tempSeqNo = (uint)tcp.SequenceNumber;
                                    tempPacket.data = new byte[payload.Length];
                                    Array.Copy(payload, tempPacket.data, payload.Length);
                                    packet1.TempPackets.Add(tempPacket);
                                }
                            }
                            else if (packet1.Data_Length != packet1.Data.Length)
                            {
                                Array.Copy(payload, 0, packet1.Data, tcp.SequenceNumber - packet1.Order, payload.Length);

                                packet1.Data_Length += payload.Length;
                            }

                            if (packet1.Data != null)
                                if (packet1.Data_Length == packet1.Data.Length)
                                {

                                    using (BinaryWriter writer = new BinaryWriter(File.Open(@"D:\captured\" + Directory.CreateDirectory(Path.GetFileName(packet1.Name)), FileMode.Create)))
                                    {
                                        writer.Write(packet1.Data);

                                    }

                                    packets.Remove(_destination);

                                }
                        }
            }
        }


    }

此代码可以帮助您