protected void SBParseMSG(PacketStream stream, TransactionNode payloadNode, int payloadLength) { List<PacketSlice> slices = new List<PacketSlice>(2); string content = stream.PeekStringUTF8(payloadLength); TransactionNode headersNode = new TransactionNode(payloadNode, "Headers"); int pos = content.IndexOf("\r\n\r\n"); string headers = content.Substring(0, pos); string[] lines = headers.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { stream.ReadBytes(StaticUtils.GetUTF8ByteCount(line), slices); string[] tokens = line.Split(new char[] { ':' }, 2); tokens[1] = tokens[1].TrimStart(new char[] { ' ' }); headersNode.AddField(tokens[0], tokens[1], "Message header field.", slices); // Skip CRLF stream.ReadBytes(2); } // Skip extra CRLF stream.ReadBytes(2); int bodyLength = payloadLength - StaticUtils.GetUTF8ByteCount(headers) - 4; if (bodyLength > 0) { TransactionNode bodyNode = new TransactionNode(payloadNode, "Body"); string contentType = (string)headersNode.Fields["Content-Type"]; contentType = contentType.Split(new char[] { ';' }, 2)[0]; if (contentType == "application/x-msnmsgrp2p") { ReadNextP2PMessageChunk(stream, bodyNode); UInt32 appID = stream.ReadU32BE(slices); bodyNode.AddField("AppID", appID, "Application ID.", slices); } else if (contentType == "text/x-msmsgsinvite") { string bodyStr = stream.ReadStringUTF8(bodyLength, slices); bodyNode.AddTextField("Body", bodyStr, "Invite body.", slices); } else { string bodyStr = stream.ReadStringUTF8(bodyLength, slices); bodyNode.AddField("Body", bodyStr, "Body.", slices); } } }
protected void ReadNextP2PMessageChunk(PacketStream stream, TransactionNode parentNode) { TransactionNode p2pNode = new TransactionNode(parentNode, "MSNP2PMessageChunk"); // Headers TransactionNode p2pHeaders = new TransactionNode(p2pNode, "Headers"); List<PacketSlice> slices = new List<PacketSlice>(); UInt32 sessionID = stream.ReadU32LE(slices); p2pHeaders.AddField("SessionID", sessionID, "Session ID.", slices); UInt32 messageID = stream.ReadU32LE(slices); p2pHeaders.AddField("MessageID", messageID, "Message ID.", slices); UInt64 dataOffset = stream.ReadU64LE(slices); p2pHeaders.AddField("DataOffset", dataOffset, "Data offset.", slices); UInt64 dataSize = stream.ReadU64LE(slices); p2pHeaders.AddField("DataSize", dataSize, "Data size.", slices); UInt32 chunkSize = stream.ReadU32LE(slices); p2pHeaders.AddField("ChunkSize", chunkSize, "Chunk size.", slices); UInt32 flags = stream.ReadU32LE(slices); p2pHeaders.AddField("Flags", flags, StaticUtils.FormatFlags(flags), "Flags.", slices); UInt32 ackedMsgID = stream.ReadU32LE(slices); p2pHeaders.AddField("AckedMsgID", ackedMsgID, "MessageID of the message to be acknowledged.", slices); UInt32 prevAckedMsgID = stream.ReadU32LE(slices); p2pHeaders.AddField("PrevAckedMsgID", prevAckedMsgID, "AckedMsgID of the last chunk to ack.", slices); UInt64 ackedDataSize = stream.ReadU64LE(slices); p2pHeaders.AddField("AckedDataSize", ackedDataSize, "Acknowledged data size.", slices); // Body TransactionNode p2pContent = null; if (chunkSize > 0) { p2pContent = new TransactionNode(p2pNode, "Content"); byte[] bytes = stream.ReadBytes((int)chunkSize, slices); if (sessionID != 0) { p2pContent.AddField("Raw", bytes, StaticUtils.FormatByteArray(bytes), "Raw content.", slices); } else { p2pContent.AddTextField("MSNSLP", bytes, StaticUtils.DecodeUTF8(bytes), "MSNSLP data.", slices); } } }
private TransactionNode ExtractHttpData(PacketStream stream, HTTPTransactionType type, string nodeName) { if (nodeName == null) { nodeName = (type == HTTPTransactionType.REQUEST) ? "Request" : "Response"; } TransactionNode node = new TransactionNode(nodeName); List<PacketSlice> slices = new List<PacketSlice>(); string line = stream.PeekLineUTF8(); int fieldCount = (type == HTTPTransactionType.REQUEST) ? 3 : 2; string[] tokens = line.Split(new char[] { ' ' }, fieldCount); if (tokens.Length < fieldCount) throw new ProtocolError(); if (type == HTTPTransactionType.REQUEST) { stream.ReadBytes(StaticUtils.GetUTF8ByteCount(tokens[0]), slices); node.AddField("Verb", tokens[0], "Request verb.", slices); stream.ReadByte(); stream.ReadBytes(StaticUtils.GetUTF8ByteCount(tokens[1]), slices); node.AddField("Argument", tokens[1], "Request argument.", slices); stream.ReadByte(); stream.ReadBytes(StaticUtils.GetUTF8ByteCount(tokens[2]), slices); node.AddField("Protocol", tokens[2], "Protocol identifier.", slices); // Set a description node.Description = String.Format("{0} {1}", tokens[0], tokens[1]); if (node.Description.Length > MAX_DESCRIPTION_LENGTH) { node.Description = node.Description.Substring(0, MAX_DESCRIPTION_LENGTH - ellipsis.Length); node.Description += ellipsis; } } else { stream.ReadBytes(StaticUtils.GetUTF8ByteCount(tokens[0]), slices); node.AddField("Protocol", tokens[0], "Protocol identifier.", slices); if (tokens.Length > 1) { stream.ReadByte(); stream.ReadBytes(StaticUtils.GetUTF8ByteCount(tokens[1]), slices); node.AddField("Result", tokens[1], "Result.", slices); } // Set a description string result = tokens[1]; if (result.Length > MAX_DESCRIPTION_LENGTH) { result = result.Substring(0, MAX_DESCRIPTION_LENGTH - ellipsis.Length) + ellipsis; } node.Description = result; } stream.ReadBytes(2); TransactionNode headersNode = new TransactionNode("Headers"); Dictionary<string, string> headerFields = new Dictionary<string, string>(); do { line = stream.PeekLineUTF8(); if (line.Length > 0) { tokens = line.Split(new char[] { ':' }, 2); if (tokens.Length < 2) throw new ProtocolError(); string key = tokens[0]; string value = tokens[1].TrimStart(); stream.ReadBytes(StaticUtils.GetUTF8ByteCount(line), slices); headersNode.AddField(key, value, "Header field.", slices); headerFields[key.ToLower()] = value; } stream.ReadBytes(2); } while (line != ""); if (headersNode.Fields.Count > 0) { node.AddChild(headersNode); } int contentLen = -1; if (headerFields.ContainsKey("content-length")) contentLen = Convert.ToInt32(headerFields["content-length"]); else if (headerFields.ContainsKey("contentlength")) contentLen = Convert.ToInt32(headerFields["contentlength"]); if (contentLen != -1) { string contentType = null, contentEncoding = null; if (headerFields.ContainsKey("content-type")) contentType = headerFields["content-type"]; else if (headerFields.ContainsKey("contenttype")) contentType = headerFields["contenttype"]; if (contentType != null) { contentType = contentType.ToLower(); tokens = contentType.Split(new char[] { ';' }); contentType = tokens[0].Trim(); if (tokens.Length > 1) { string[] encTokens = tokens[1].Split(new char[] { '=' }, 2); if (encTokens[0].Trim() == "charset" && encTokens.Length > 1) { contentEncoding = encTokens[1]; } } } string str = stream.PeekStringASCII(5); if (str == "<?xml") { contentType = "text/xml"; contentEncoding = "utf-8"; // FIXME } if (contentLen > 0) { AddBodyNode(stream, node, contentType, contentEncoding, contentLen); } } return node; }
private TransactionNode ExtractTNSData(PacketStream stream, StreamDirection direction) { TransactionNode node; OracleTransactionType type; List<PacketSlice> slices = new List<PacketSlice>(); Int16 pLen = 0; int packetType; try { pLen = stream.PeekInt16(); stream.ReadBytes(2, slices); stream.ReadBytes(2); //skip checksum packetType = stream.ReadByte(); type = (OracleTransactionType)packetType; stream.ReadByte(); //skip the reserved byte } catch (Exception e) { logger.AddMessage(e.Message); return null; } switch (type) { case OracleTransactionType.CONNECT: try { node = new TransactionNode("Connect"); node.AddField("Packet Size", pLen, "Packet Size", slices); Int16 headerChecksum = stream.ReadInt16(); Int16 version = stream.PeekInt16(); stream.ReadBytes(2, slices); node.AddField("Version", version, "Version", slices); Int16 compatVersion = stream.PeekInt16(); stream.ReadBytes(2, slices); node.AddField("Version (compatible)", compatVersion, "Compatible Version", slices); Int16 serviceOptions = stream.ReadInt16(); Int16 sessionDataUnitSize = stream.ReadInt16(); Int16 maxTxDataUnitSize = stream.ReadInt16(); Int16 NTProtCharacteristics = stream.ReadInt16(); Int16 lineTurnValue = stream.ReadInt16(); Int16 valueOfOneInHW = stream.ReadInt16(); Int16 ctDataLen = stream.PeekInt16(); stream.ReadBytes(2, slices); node.AddField("Data Length", ctDataLen, "The length of the connect datastring.", slices); Int16 ctDataOffset = stream.ReadInt16(); Int16 maxRecCtData = stream.ReadInt16(); Int16 ctFlagsA = stream.ReadInt16(); Int16 ctFlagsB = stream.ReadInt16(); Int32 traceItemA = stream.ReadInt32(); Int32 traceItemB = stream.ReadInt32(); Int64 traceID = stream.ReadInt64(); stream.ReadInt64(); byte[] ctData = stream.PeekBytes(ctDataLen); string connectData = StaticUtils.DecodeASCII(ctData); stream.ReadBytes(ctDataLen, slices); node.AddField("Connect data", connectData, "Connect data", slices); try { Match match = Regex.Match(connectData, "\\(PROTOCOL=(?<proto>\\w{2,})\\)\\(HOST=(?<host>[.\\w]{7,})\\)\\(PORT=(?<port>\\d{2,})\\)\\).*SERVICE_NAME=(?<sid>[.\\w]{1,})\\)"); string proto = match.Groups["proto"].Value; string host = match.Groups["host"].Value; string newPort = match.Groups["port"].Value; string sid = match.Groups["sid"].Value; TransactionNode cInfoNode = new TransactionNode("Connection Info"); cInfoNode.AddField("Protocol", proto, "Protocol", slices); cInfoNode.AddField("Host", host, "Server being connected to", slices); cInfoNode.AddField("Port", newPort, "Port", slices); cInfoNode.AddField("Service", sid, "Service", slices); node.AddChild(cInfoNode); } catch (ArgumentException) { } return node; } catch (Exception e) { logger.AddMessage(e.Message); return null; } case OracleTransactionType.ACCEPT: node = new TransactionNode("Accept"); node.AddField("Packet Size", pLen, "Packet Size", slices); try { Int16 headerChecksum = stream.ReadInt16(); Int16 version = stream.PeekInt16(); stream.ReadBytes(2, slices); node.AddField("Version", version, "Version", slices); Int16 serviceOptions = stream.ReadInt16(); Int16 sessionDataUnitSize = stream.PeekInt16(); stream.ReadBytes(2, slices); node.AddField("Session Data Unit Size", sessionDataUnitSize, "Session Data Unit Size", slices); Int16 maxTxDataUnitSize = stream.PeekInt16(); stream.ReadBytes(2, slices); node.AddField("Max Tx Unit Size", maxTxDataUnitSize, "Maximum Transmission Data Unit Size", slices); Int16 valueOfOneInHW = stream.ReadInt16(); Int16 acceptDataLength = stream.ReadInt16(); Int16 dataOffset = stream.ReadInt16(); int connectFlagsA = stream.ReadByte(); int connectFlagsB = stream.ReadByte(); stream.ReadBytes(pLen - 24); //read out empty bytes } catch (Exception e) { logger.AddMessage(e.Message); return null; } return node; case OracleTransactionType.REDIRECT: node = new TransactionNode("Redirect"); node.AddField("Packet Size", pLen, "Packet Size", slices); try { Int16 headerChecksum = stream.ReadInt16(); Int16 redirLen = stream.PeekInt16(); stream.ReadBytes(2, slices); node.AddField("Redirection Data Length", redirLen, "Length of the redirection data string", slices); byte[] redirData = stream.PeekBytes(redirLen); string sRedir = StaticUtils.DecodeASCII(redirData); stream.ReadBytes(redirLen, slices); node.AddField("Redirection Data", sRedir, "Redirection data", slices); //get the redirected port string newPort = null; string proto = null; string host = null; try { Match match = Regex.Match(sRedir, "\\(PROTOCOL=(?<proto>\\w{2,})\\)\\(HOST=(?<host>[.\\w]{7,})\\)\\(PORT=(?<port>\\d{2,})\\)\\)"); proto = match.Groups["proto"].Value; host = match.Groups["host"].Value; newPort = match.Groups["port"].Value; } catch (ArgumentException) { // Syntax error in the regular expression } if(!newPort.Equals("")) redirPort = Int32.Parse(newPort); TransactionNode redirInfo = new TransactionNode("Redirection Info"); redirInfo.AddField("Protocol", proto, "Protocol used", slices); redirInfo.AddField("Host", host, "Host", slices); redirInfo.AddField("Port", newPort, "Port", slices); node.AddChild(redirInfo); } catch (Exception e) { logger.AddMessage(e.Message); return null; } return node; case OracleTransactionType.DATA: string label; if (direction == StreamDirection.IN) label = "Response"; else label = "Request"; node = new TransactionNode("Data - "+label ); node.AddField("Packet Size", pLen, "Packet Size", slices); try { Int16 headerChecksum = stream.ReadInt16(); Int16 dataFlags = stream.ReadInt16(); int payLoadLength = pLen - 10; byte[] payLoad = stream.PeekBytes(payLoadLength); string sPayload = StaticUtils.DecodeASCII(payLoad); stream.ReadBytes(payLoadLength, slices); node.AddField("Data", sPayload, "Data", slices); } catch (Exception e) { logger.AddMessage(e.Message); return null; } return node; default: logger.AddMessage(String.Format("Packet dissection not implemented [TransactionType={0}]", type)); return null; } }
protected void AddBodyNode(PacketStream stream, TransactionNode transactionNode, string contentType, string contentEncoding, int bodyLen) { List<PacketSlice> slices = new List<PacketSlice>(1); TransactionNode bodyNode = new TransactionNode("Body"); byte[] body = stream.ReadBytes(bodyLen, slices); if (contentType == "text/html" || contentType == "text/xml") { int realBodyLen = body.Length; if (body[realBodyLen - 1] == '\0') realBodyLen--; Decoder dec; if (contentEncoding == "utf-8") { dec = Encoding.UTF8.GetDecoder(); } else { dec = Encoding.ASCII.GetDecoder(); } char[] bodyChars = new char[dec.GetCharCount(body, 0, realBodyLen)]; dec.GetChars(body, 0, realBodyLen, bodyChars, 0); string bodyStr = new string(bodyChars); if (contentType == "text/xml") bodyNode.AddXMLField("XML", bodyStr, "Body XML data.", slices); else bodyNode.AddTextField("HTML", bodyStr, "Body HTML data.", slices); } else if (contentType == "application/vnd.ms-sync.wbxml") { string xml = WBXML.ConvertToXML(body); bodyNode.AddXMLField("WBXML", xml, "Body WBXML data.", slices); } else { bodyNode.AddField("Raw", body, StaticUtils.FormatByteArray(body), "Raw body data.", slices); } transactionNode.AddChild(bodyNode); }