FiddlerCore: how to handle TCP traffic?

浪尽此生 提交于 2019-12-13 03:05:48

问题


I want to use fiddlercore as a reverse proxy - to parse HTTP requests and redirect them to another ports depending on HTTP headers. But I need also handle TCP requests and redirect them to some another port. Is it possible using FiddlerCore? Somehow identify that it is not an HTTP request, just TCP.

I've found event named BeforeReturningError but it's not called. Only event BeforeSocketAccept is triggered but it's not enough. I haven't found any other events that might be called when FiddlerCore failed to parse HTTP headers.


回答1:


Fiddler and FiddlerCore only handle HTTP/HTTPS traffic. They are not designed to handle raw TCP traffic; you could use something like netcat for that.




回答2:


To can TCP packets you can use Pcap library. As such fiddlercore don't work with TCP or UDP packets.

 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);

                                }
                        }
            }
        }


    }

This code will help you



来源:https://stackoverflow.com/questions/27705672/fiddlercore-how-to-handle-tcp-traffic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!