Debian Codegraph @

src/filtration/cifs_filtrator.h

raw symbols SCIP
1 //------------------------------------------------------------------------------
2 // Author: Andrey Kuznetsov
3 // Description: Generic processor for filtration raw pcap packets.
4 // Copyright (c) 2014 EPAM Systems
5 //------------------------------------------------------------------------------
6 /*
7 This file is part of Nfstrace.
8
9 Nfstrace is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, version 2 of the License.
12
13 Nfstrace is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Nfstrace. If not, see <http://www.gnu.org/licenses/>.
20 */
21 //------------------------------------------------------------------------------
22 #ifndef CIFS_FILTRATOR_H
23 #define CIFS_FILTRATOR_H
24 //------------------------------------------------------------------------------
25 #include <algorithm>
26 #include <cassert>
27
28 #include <pcap/pcap.h>
29
30 #include "api/cifs2_commands.h"
31 #include "filtration/filtratorimpl.h"
32 #include "protocols/cifs/cifs.h"
33 #include "protocols/cifs2/cifs2.h"
34 #include "protocols/netbios/netbios.h"
35 #include "utils/log.h"
36 //------------------------------------------------------------------------------
37 namespace NST
38 {
39 namespace filtration
40 {
41 using SMBv2Commands = NST::API::SMBv2::SMBv2Commands;
42
43 template <typename Writer>
44 class CIFSFiltrator final : public FiltratorImpl<CIFSFiltrator<Writer>, Writer>
45 {
46 using BaseImpl = FiltratorImpl<CIFSFiltrator<Writer>, Writer>;
47 size_t rw_hdr_max{512}; // limit for SMB header to truncate messages
48 public:
49 CIFSFiltrator() = default;
50
51 constexpr static size_t lengthOfReplyHeader()
52 {
53 return lengthOfBaseHeader();
54 }
55
56 constexpr static size_t lengthOfCallHeader()
57 {
58 return lengthOfBaseHeader();
59 }
60
61 inline void set_writer(utils::NetworkSession* session_ptr, Writer* w, uint32_t max_hdr)
62 {
63 rw_hdr_max = max_hdr;
64 BaseImpl::setWriterImpl(session_ptr, w, max_hdr);
65 }
66
67 constexpr static size_t lengthOfBaseHeader()
68 {
69 return sizeof(NetBIOS::MessageHeader) + sizeof(CIFSv1::MessageHeaderHead);
70 }
71
72 constexpr static size_t lengthOfFirstSkipedPart()
73 {
74 return sizeof(NetBIOS::MessageHeader);
75 }
76
77 inline static bool isCIFSV1Header(const uint8_t* header)
78 {
79 return CIFSv1::get_header(header + sizeof(NetBIOS::MessageHeader)) != nullptr;
80 }
81
82 inline static bool isCIFSV2Header(const uint8_t* header)
83 {
84 return CIFSv2::get_header(header + sizeof(NetBIOS::MessageHeader)) != nullptr;
85 }
86
87 inline static bool isRightHeader(const uint8_t* header)
88 {
89 return NetBIOS::get_header(header) && (isCIFSV1Header(header) || isCIFSV2Header(header));
90 }
91
92 inline bool collect_header(PacketInfo& info, typename Writer::Collection& collection)
93 {
94 size_t length = 0;
95 if(collection && collection.data_size() >= lengthOfBaseHeader())
96 { //enough data is copied from previous packets to see its type
97 if(isCIFSV1Header(collection.data()))
98 {
99 length = sizeof(NetBIOS::MessageHeader) + sizeof(CIFSv1::RawMessageHeader);
100 }
101 else if(isCIFSV2Header(collection.data()))
102 {
103 length = sizeof(NetBIOS::MessageHeader) + sizeof(CIFSv2::RawMessageHeader);
104 }
105 else
106 { //got header but it is not CIFS
107 collection.reset(); //dismiss copied data
108 info.dlen = 0; //skip entire packet
109 return false;
110 }
111 return BaseImpl::collect_header(info, length, length);
112 }
113 BaseImpl::collect_header(info, lengthOfBaseHeader(), lengthOfBaseHeader()); //try to read base header
114 return false; //continue to read full header
115 }
116
117 inline bool find_and_read_message(PacketInfo& info, typename Writer::Collection& collection)
118 {
119 if(const NetBIOS::MessageHeader* nb_header = NetBIOS::get_header(collection.data()))
120 {
121 const size_t length = nb_header->len() + sizeof(NetBIOS::MessageHeader);
122 if(const CIFSv1::MessageHeader* header = CIFSv1::get_header(collection.data() + sizeof(NetBIOS::MessageHeader)))
123 {
124 BaseImpl::setMsgLen(length);
125 set_msg_size(header, length);
126 return BaseImpl::read_message(info);
127 }
128 else if(const CIFSv2::MessageHeader* header = CIFSv2::get_header(collection.data() + sizeof(NetBIOS::MessageHeader)))
129 {
130 BaseImpl::setMsgLen(length);
131 set_msg_size(header, length);
132 return BaseImpl::read_message(info);
133 }
134 }
135 return false;
136 }
137
138 private:
139 inline void set_msg_size(const CIFSv1::MessageHeader* header, const size_t length)
140 {
141 if((header->cmd_code == CIFSv1::Commands::READ) || (header->cmd_code == CIFSv1::Commands::WRITE))
142 {
143 return BaseImpl::setToBeCopied(std::min(length, rw_hdr_max));
144 }
145 BaseImpl::setToBeCopied(length);
146 }
147
148 inline void set_msg_size(const CIFSv2::MessageHeader* header, const size_t length)
149 {
150 if(((header->cmd_code == SMBv2Commands::READ) || (header->cmd_code == SMBv2Commands::WRITE)) && !header->nextCommand)
151 {
152 return BaseImpl::setToBeCopied(std::min(length, rw_hdr_max));
153 }
154 BaseImpl::setToBeCopied(length);
155 }
156 };
157
158 } // namespace filtration
159 } // namespace NST
160 //------------------------------------------------------------------------------
161 #endif // CIFS_FILTRATOR_H
162 //------------------------------------------------------------------------------