//////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// private Int32 _ReadRequest2(Int64 offset, Int32 length) { SMB2Header header = new SMB2Header(); header.SetCommand(new Byte[] { 0x08, 0x00 }); header.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header.SetMessageID(++messageId); header.SetProcessID(processId); header.SetTreeId(treeId); header.SetSessionID(sessionId); header.SetChainOffset(new Byte[] { 0x00, 0x00, 0x00, 0x00 }); SMB2ReadRequest readRequest = new SMB2ReadRequest(); readRequest.SetGuidHandleFile(guidFileHandle); readRequest.SetLength(BitConverter.GetBytes(length)); readRequest.SetOffset(BitConverter.GetBytes(offset)); Byte[] bData = readRequest.GetRequest(); if (signing) { header.SetFlags(new Byte[] { 0x0c, 0x00, 0x00, 0x00 }); header.SetSignature(sessionKey, ref bData); } Byte[] bHeader = header.GetHeader(); NetBIOSSessionService sessionService = new NetBIOSSessionService(); sessionService.SetHeaderLength(bHeader.Length); sessionService.SetDataLength(bData.Length); Byte[] bSessionService = sessionService.GetNetBIOSSessionService(); Byte[] bSend = Combine.combine(Combine.combine(bSessionService, bHeader), bData); streamSocket.Write(bSend, 0, bSend.Length); streamSocket.Flush(); Combine combine = new Combine(); Int32 streamLength = 0; Byte[] dataRecieved = new Byte[0]; do { Byte[] part = new Byte[65619]; Int32 partLength = streamSocket.Read(part, 0, part.Length); streamLength += partLength; combine.Extend(part.Take(partLength).ToArray()); }while (streamLength < length); Int32 padding = length < 65535 ? 0 : 54; recieve = combine.Retrieve().Skip(84).Take(streamLength - 84 - padding).ToArray(); binaryWriter.Write(recieve); binaryWriter.Flush(); return(recieve.Length); }
//////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// internal Boolean SetInfoRequest(String sourceFilePath, String destination) { SMB2Header header = new SMB2Header(); header.SetCommand(new Byte[] { 0x11, 0x00 }); header.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header.SetMessageID(++messageId); header.SetProcessID(processId); header.SetTreeId(treeId); header.SetSessionID(sessionId); SMB2SetInfo setInfo = new SMB2SetInfo(); setInfo.SetClass(new Byte[] { 0x01 }); setInfo.SetInfoLevel(new Byte[] { 0x14 }); setInfo.SetGUIDHandleFile(guidFileHandle); //This may need to be coverted to int32 using (FileStream fileStream = new FileStream(Path.GetFullPath(sourceFilePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader binaryReader = new BinaryReader(fileStream)) { setInfo.SetBuffer(BitConverter.GetBytes(binaryReader.BaseStream.Length)); } } setInfo.SetGUIDHandleFile(guidFileHandle); Byte[] bData = setInfo.GetRequest(); header.SetChainOffset(bData.Length); if (signing) { header.SetFlags(new Byte[] { 0x08, 0x00, 0x00, 0x00 }); header.SetSignature(sessionKey, ref bData); } Byte[] bHeader = header.GetHeader(); NetBIOSSessionService sessionService = new NetBIOSSessionService(); sessionService.SetHeaderLength(bHeader.Length); sessionService.SetDataLength(bData.Length); Byte[] bSessionService = sessionService.GetNetBIOSSessionService(); Byte[] bSend = Combine.combine(bSessionService, Combine.combine(bHeader, bData)); streamSocket.Write(bSend, 0, bSend.Length); streamSocket.Flush(); streamSocket.Read(recieve, 0, recieve.Length); if (GetStatus(recieve.Skip(12).Take(4).ToArray())) { treeId = recieve.Skip(40).Take(4).ToArray(); return(true); } return(false); }
//////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// internal override Boolean ReadRequest() { SMB2Header header = new SMB2Header(); header.SetCommand(new Byte[] { 0x08, 0x00 }); header.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header.SetMessageID(++messageId); header.SetProcessID(processId); header.SetTreeId(treeId); header.SetSessionID(sessionId); header.SetChainOffset(new Byte[] { 0x00, 0x00, 0x00, 0x00 }); SMB2ReadRequest readRequest = new SMB2ReadRequest(); readRequest.SetGuidHandleFile(guidFileHandle); readRequest.SetLength(BitConverter.GetBytes(4096)); readRequest.SetOffset(BitConverter.GetBytes((Int64)0)); Byte[] bData = readRequest.GetRequest(); if (signing) { header.SetFlags(new Byte[] { 0x0c, 0x00, 0x00, 0x00 }); header.SetSignature(sessionKey, ref bData); } Byte[] bHeader = header.GetHeader(); NetBIOSSessionService sessionService = new NetBIOSSessionService(); sessionService.SetHeaderLength(bHeader.Length); sessionService.SetDataLength(bData.Length); Byte[] bSessionService = sessionService.GetNetBIOSSessionService(); Byte[] bSend = Combine.combine(Combine.combine(bSessionService, bHeader), bData); streamSocket.Write(bSend, 0, bSend.Length); streamSocket.Flush(); Int16 i = 0; while (smbClient.Available < 8192 && i < 10) { System.Threading.Thread.Sleep(100); i++; } Int32 streamLength = streamSocket.Read(recieve, 0, recieve.Length); Byte[] data = recieve.Skip(84).Take((Int32)streamLength).ToArray(); GetStatus(recieve.Skip(12).Take(4).ToArray()); return(true); }
//////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// private void _WriteRequest(Byte[] buffer, Int64 offset) { SMB2Header header = new SMB2Header(); header.SetCommand(new Byte[] { 0x09, 0x00 }); header.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header.SetMessageID(++messageId); header.SetProcessID(processId); header.SetTreeId(treeId); header.SetSessionID(sessionId); SMB2WriteRequest writeRequest = new SMB2WriteRequest(); writeRequest.SetGuidHandleFile(guidFileHandle); writeRequest.SetBuffer(buffer); writeRequest.SetOffset(offset); Byte[] bData = writeRequest.GetRequest(); header.SetChainOffset(bData.Length); if (signing) { header.SetFlags(new Byte[] { 0x08, 0x00, 0x00, 0x00 }); header.SetSignature(sessionKey, ref bData); } Byte[] bHeader = header.GetHeader(); NetBIOSSessionService sessionService = new NetBIOSSessionService(); sessionService.SetHeaderLength(bHeader.Length); sessionService.SetDataLength(bData.Length); Byte[] bSessionService = sessionService.GetNetBIOSSessionService(); Byte[] bSend = Combine.combine(bSessionService, Combine.combine(bHeader, bData)); streamSocket.Write(bSend, 0, bSend.Length); streamSocket.Flush(); streamSocket.Read(recieve, 0, recieve.Length); }
//////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// internal Boolean GetInfoRequest() { SMB2Header header = new SMB2Header(); header.SetCommand(new Byte[] { 0x10, 0x00 }); header.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header.SetMessageID(++messageId); header.SetProcessID(processId); header.SetTreeId(treeId); header.SetSessionID(sessionId); SMB2GetInfo getInfo = new SMB2GetInfo(); getInfo.SetClass(new Byte[] { 0x02 }); getInfo.SetInfoLevel(new Byte[] { 0x01 }); getInfo.SetMaxResponseSize(new Byte[] { 0x58, 0x00, 0x00, 0x00 }); getInfo.SetGetInfoInputOffset(new Byte[] { 0x00, 0x00 }); getInfo.SetGUIDHandleFile(guidFileHandle); Byte[] bData = getInfo.GetRequest(); header.SetChainOffset(bData.Length); if (signing) { header.SetFlags(new Byte[] { 0x08, 0x00, 0x00, 0x00 }); header.SetSignature(sessionKey, ref bData); } Byte[] bHeader = header.GetHeader(); SMB2Header header2 = new SMB2Header(); header2.SetCommand(new Byte[] { 0x10, 0x00 }); header2.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header2.SetMessageID(++messageId); header2.SetProcessID(processId); header2.SetTreeId(treeId); header2.SetSessionID(sessionId); header2.SetFlags(new Byte[] { 0x00, 0x00, 0x00, 0x04 }); SMB2GetInfo getInfo2 = new SMB2GetInfo(); getInfo2.SetClass(new Byte[] { 0x02 }); getInfo2.SetInfoLevel(new Byte[] { 0x05 }); getInfo2.SetMaxResponseSize(new Byte[] { 0x50, 0x00, 0x00, 0x00 }); getInfo2.SetGetInfoInputOffset(new Byte[] { 0x00, 0x00 }); getInfo2.SetGUIDHandleFile(guidFileHandle); Byte[] bData2 = getInfo2.GetRequest(); if (signing) { header2.SetFlags(new Byte[] { 0x08, 0x00, 0x00, 0x00 }); header2.SetSignature(sessionKey, ref bData2); } Byte[] bHeader2 = header2.GetHeader(); NetBIOSSessionService sessionService = new NetBIOSSessionService(); sessionService.SetHeaderLength(bHeader.Length + bHeader2.Length); sessionService.SetDataLength(bData.Length + bData2.Length); Byte[] bSessionService = sessionService.GetNetBIOSSessionService(); Combine combine = new Combine(); combine.Extend(bHeader); combine.Extend(bData); combine.Extend(bHeader2); combine.Extend(bData2); Byte[] bSend = Combine.combine(bSessionService, combine.Retrieve()); streamSocket.Write(bSend, 0, bSend.Length); streamSocket.Flush(); streamSocket.Read(recieve, 0, recieve.Length); if (GetStatus(recieve.Skip(12).Take(4).ToArray())) { return(true); } return(false); }
//////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// internal virtual Boolean ReadRequest() { treeId = recieve.Skip(40).Take(4).ToArray(); //////////////////////////////////////////////////////////////////////////////// SMB2Header header = new SMB2Header(); header.SetCommand(new Byte[] { 0x05, 0x00 }); header.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header.SetMessageID(++messageId); header.SetProcessID(processId); header.SetTreeId(treeId); header.SetSessionID(sessionId); SMB2ReadRequest readRequest = new SMB2ReadRequest(); readRequest.SetGuidHandleFile(guidFileHandle); Byte[] bData = readRequest.GetRequest(); header.SetChainOffset(bData.Length); if (signing) { header.SetFlags(new Byte[] { 0x0c, 0x00, 0x00, 0x00 }); header.SetSignature(sessionKey, ref bData); } else { header.SetFlags(new Byte[] { 0x00, 0x00, 0x00, 0x00 }); } Byte[] bHeader = header.GetHeader(); //////////////////////////////////////////////////////////////////////////////// SMB2Header header2 = new SMB2Header(); header2.SetCommand(new Byte[] { 0x0e, 0x00 }); header2.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header2.SetMessageID(++messageId); header2.SetProcessID(processId); header2.SetTreeId(treeId); header2.SetSessionID(sessionId); header2.SetChainOffset(new Byte[] { 0x68, 0x00, 0x00, 0x00 }); SMB2FindFileRequestFile requestFile = new SMB2FindFileRequestFile(); requestFile.SetPadding(new Byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); Byte[] bData2 = requestFile.GetRequest(); if (signing) { header2.SetFlags(new Byte[] { 0x0c, 0x00, 0x00, 0x00 }); header2.SetSignature(sessionKey, ref bData2); } else { header2.SetFlags(new Byte[] { 0x04, 0x00, 0x00, 0x00 }); } Byte[] bHeader2 = header2.GetHeader(); //////////////////////////////////////////////////////////////////////////////// SMB2Header header3 = new SMB2Header(); header3.SetCommand(new Byte[] { 0x0e, 0x00 }); header3.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header3.SetMessageID(++messageId); header3.SetProcessID(processId); header3.SetTreeId(treeId); header3.SetSessionID(sessionId); SMB2FindFileRequestFile requestFile2 = new SMB2FindFileRequestFile(); requestFile2.SetOutputBufferLength(new Byte[] { 0x80, 0x00, 0x00, 0x00 }); Byte[] bData3 = requestFile2.GetRequest(); if (signing) { header3.SetFlags(new Byte[] { 0x0c, 0x00, 0x00, 0x00 }); header3.SetSignature(sessionKey, ref bData3); } else { header3.SetFlags(new Byte[] { 0x04, 0x00, 0x00, 0x00 }); } Byte[] bHeader3 = header3.GetHeader(); //////////////////////////////////////////////////////////////////////////////// NetBIOSSessionService sessionService = new NetBIOSSessionService(); sessionService.SetHeaderLength(bHeader.Length + bHeader2.Length + bHeader3.Length); sessionService.SetDataLength(bData.Length + bData2.Length + bData3.Length); Byte[] bSessionService = sessionService.GetNetBIOSSessionService(); Byte[] bSend = Combine.combine(Combine.combine(bSessionService, bHeader), bData); bSend = Combine.combine(bSend, Combine.combine(bHeader2, bData2)); bSend = Combine.combine(bSend, Combine.combine(bHeader3, bData3)); streamSocket.Write(bSend, 0, bSend.Length); streamSocket.Flush(); streamSocket.Read(recieve, 0, recieve.Length); if (GetStatus(recieve.Skip(12).Take(4).ToArray())) return true; else return false; }
//////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////// internal Boolean FindRequest() { treeId = recieve.Skip(40).Take(4).ToArray(); //////////////////////////////////////////////////////////////////////////////// SMB2Header header = new SMB2Header(); header.SetCommand(new Byte[] { 0x0e, 0x00 }); header.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header.SetMessageID(++messageId); header.SetProcessID(processId); header.SetTreeId(treeId); header.SetSessionID(sessionId); header.SetChainOffset(new Byte[] { 0x68, 0x00, 0x00, 0x00 }); SMB2FindFileRequestFile findFileRequestFile = new SMB2FindFileRequestFile(); findFileRequestFile.SetInfoLevel(new Byte[] { 0x25 }); findFileRequestFile.SetFileID(guidFileHandle); findFileRequestFile.SetPadding(new Byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); Byte[] bData = findFileRequestFile.GetRequest(); if (signing) { header.SetFlags(new Byte[] { 0x0c, 0x00, 0x00, 0x00 }); header.SetSignature(sessionKey, ref bData); } Byte[] bHeader = header.GetHeader(); //////////////////////////////////////////////////////////////////////////////// SMB2Header header2 = new SMB2Header(); header2.SetCommand(new Byte[] { 0x0e, 0x00 }); header2.SetCreditsRequested(new Byte[] { 0x01, 0x00 }); header2.SetMessageID(++messageId); header2.SetProcessID(processId); header2.SetTreeId(treeId); header2.SetSessionID(sessionId); header.SetFlags(new Byte[] { 0x04, 0x00, 0x00, 0x00 }); SMB2FindFileRequestFile findFileRequestFile2 = new SMB2FindFileRequestFile(); findFileRequestFile2.SetInfoLevel(new Byte[] { 0x25 }); findFileRequestFile2.SetFileID(guidFileHandle); findFileRequestFile2.SetPadding(new Byte[] { 0x80, 0x00, 0x00, 0x00 }); Byte[] bData2 = findFileRequestFile2.GetRequest(); if (signing) { header2.SetFlags(new Byte[] { 0x0c, 0x00, 0x00, 0x00 }); header2.SetSignature(sessionKey, ref bData); } Byte[] bHeader2 = header2.GetHeader(); //////////////////////////////////////////////////////////////////////////////// NetBIOSSessionService sessionService = new NetBIOSSessionService(); sessionService.SetHeaderLength(bHeader.Length + bHeader2.Length); sessionService.SetDataLength(bData.Length + bData2.Length); Byte[] bSessionService = sessionService.GetNetBIOSSessionService(); Combine combine = new Combine(); combine.Extend(bHeader); combine.Extend(bData); combine.Extend(bHeader2); combine.Extend(bData2); Byte[] bSend = Combine.combine(bSessionService, combine.Retrieve()); streamSocket.Write(bSend, 0, bSend.Length); streamSocket.Flush(); streamSocket.Read(recieve, 0, recieve.Length); return(GetStatus(recieve.Skip(12).Take(4).ToArray())); }