示例#1
0
        private void parseData(IPHeader ipHeader, TCPHeader tcpHeader, string content, bool isRequest)
        {
            string destionationIP  = ipHeader.DestinationAddress.ToString();
            int    destinationPort = Int32.Parse(tcpHeader.DestinationPort);
            string sourceIP        = ipHeader.SourceAddress.ToString();
            int    sourcePort      = Int32.Parse(tcpHeader.SourcePort);

            if (!isRequest)
            {
                int index = content.IndexOf("\r\n\r\n");
                if (index != -1)
                {
                    content = content.Substring(0, index).Trim();
                }
            }

            string[] lines = content.Split(new char[] { '\r', '\n' });
            if (isRequest && lines != null && lines.Length > 0)
            {
                Hashtable httpRequest = new Hashtable();
                targetIP   = destionationIP;
                targetPort = destinationPort;
                httpRequest.Add("REQUEST_IP_SOURCE", sourceIP);
                httpRequest.Add("REQUEST_PORT_SOURCE", sourcePort);
                httpRequest.Add("REQUEST_IP_DESTINATION", destionationIP);
                httpRequest.Add("REQUEST_PORT_DESTINATION", destinationPort);

                for (int i = 0; i < lines.Length; i++)
                {
                    if (lines[i] != null && lines[i].Length > 0)
                    {
                        if (lines[i].StartsWith("GET "))
                        {
                            string value = lines[i].Substring("GET ".Length);
                            int    index = value.IndexOf(" ");
                            if (index != -1)
                            {
                                value = value.Substring(0, index);
                            }
                            httpRequest.Add("REQUEST_URL", value);
                        }
                        else
                        {
                            int index = lines[i].IndexOf(":");
                            if (index > 0)
                            {
                                string key   = lines[i].Substring(0, index).Trim();
                                string value = lines[i].Substring(index + 1).Trim();
                                if (!httpRequest.ContainsKey(key))
                                {
                                    httpRequest.Add(key, value);
                                }
                            }
                        }
                    }
                }
                receiveRequest(httpRequest);
            }
            else if (!isRequest && lines != null && lines.Length > 0)
            {
                if (targetIP != null && targetIP.Equals(sourceIP) && targetPort == sourcePort)
                {
                    Hashtable httpResponse = new Hashtable();
                    httpResponse.Add("RESPONSE_IP_SOURCE", sourceIP);
                    httpResponse.Add("RESPONSE_PORT_SOURCE", sourcePort);
                    httpResponse.Add("RESPONSE_IP_DESTINATION", destionationIP);
                    httpResponse.Add("RESPONSE_PORT_DESTINATION", destinationPort);

                    for (int i = 0; i < lines.Length; i++)
                    {
                        if (lines[i] != null && lines[i].Length > 0)
                        {
                            if (lines[i].StartsWith("HTTP/1.1 "))
                            {
                                string value = lines[i].Substring("HTTP/1.1 ".Length);
                                httpResponse.Add("REQUEST_CODE", value);
                            }
                            else
                            {
                                int index = lines[i].IndexOf(":");
                                if (index > 0)
                                {
                                    string key   = lines[i].Substring(0, index).Trim();
                                    string value = lines[i].Substring(index + 1).Trim();
                                    if (!httpResponse.ContainsKey(key))
                                    {
                                        httpResponse.Add(key, value);
                                    }
                                }
                            }
                        }
                    }
                    receiveResponse(httpResponse);
                }
            }
        }
示例#2
0
        private void parseData(IPHeader ipHeader, TCPHeader tcpHeader, string content, bool isRequest)
        {
            string destionationIP = ipHeader.DestinationAddress.ToString();
            int destinationPort = Int32.Parse(tcpHeader.DestinationPort);
            string sourceIP = ipHeader.SourceAddress.ToString();
            int sourcePort = Int32.Parse(tcpHeader.SourcePort);

            if (!isRequest) {
                int index = content.IndexOf("\r\n\r\n");
                if (index != -1) {
                    content = content.Substring(0, index).Trim();
                }
            }

            string[] lines = content.Split(new char[] { '\r', '\n' });
            if (isRequest && lines != null && lines.Length>0) {
                Hashtable httpRequest = new Hashtable();
                targetIP = destionationIP;
                targetPort = destinationPort;
                httpRequest.Add("REQUEST_IP_SOURCE", sourceIP);
                httpRequest.Add("REQUEST_PORT_SOURCE", sourcePort);
                httpRequest.Add("REQUEST_IP_DESTINATION", destionationIP);
                httpRequest.Add("REQUEST_PORT_DESTINATION", destinationPort);

                for (int i = 0; i < lines.Length; i++) {
                    if (lines[i] != null && lines[i].Length > 0) {
                        if (lines[i].StartsWith("GET ")) {
                            string value = lines[i].Substring("GET ".Length);
                            int index = value.IndexOf(" ");
                            if (index != -1) value = value.Substring(0, index);
                            httpRequest.Add("REQUEST_URL", value);
                        }
                        else {
                            int index = lines[i].IndexOf(":");
                            if (index > 0) {
                                string key = lines[i].Substring(0, index).Trim();
                                string value = lines[i].Substring(index + 1).Trim();
                                if (!httpRequest.ContainsKey(key)) httpRequest.Add(key, value);
                            }
                        }
                    }
                }
                receiveRequest(httpRequest);
            }
            else if (!isRequest && lines != null && lines.Length > 0) {
                if (targetIP != null && targetIP.Equals(sourceIP) && targetPort == sourcePort) {
                    Hashtable httpResponse = new Hashtable();
                    httpResponse.Add("RESPONSE_IP_SOURCE", sourceIP);
                    httpResponse.Add("RESPONSE_PORT_SOURCE", sourcePort);
                    httpResponse.Add("RESPONSE_IP_DESTINATION", destionationIP);
                    httpResponse.Add("RESPONSE_PORT_DESTINATION", destinationPort);

                    for (int i = 0; i < lines.Length; i++) {
                        if (lines[i] != null && lines[i].Length > 0) {
                            if (lines[i].StartsWith("HTTP/1.1 ")) {
                                string value = lines[i].Substring("HTTP/1.1 ".Length);
                                httpResponse.Add("REQUEST_CODE", value);
                            }
                            else {
                                int index = lines[i].IndexOf(":");
                                if (index > 0) {
                                    string key = lines[i].Substring(0, index).Trim();
                                    string value = lines[i].Substring(index + 1).Trim();
                                    if (!httpResponse.ContainsKey(key)) httpResponse.Add(key, value);
                                }
                            }
                        }
                    }
                    receiveResponse(httpResponse);
                }
            }
        }