-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServerOps.cc
344 lines (278 loc) · 12 KB
/
ServerOps.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/***************************************************************************
* *
* _ _____ ____ *
* /\ | | | __ \ /\ | _ \ /\ *
* / \ | | | | | | / \ | |_) | / \ *
* / /\ \ | | | | | | / /\ \ | _ < / /\ \ *
* / ____ \ | |___ | |__| / / ____ \ | |_) / / ____ \ *
* /_/ \_\ | ____| |_____/ /_/ \_\ |____/ /_/ \_\ *
* *
* == {Port Knocking/Single Packet Authorization} Security Suite == *
* *
***************************************************************************
* *
* This file is part of Aldaba Knocking Suite. *
* *
* Copyright (c) 2010, Luis MartinGarcia. (aldabaknocking@gmail.com) *
* *
* Aldaba is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free *
* Software Foundation; Version 2 of the License, with the exceptions, *
* conditions and clarifications described in the file named LICENSE.txt, *
* distributed with Aldaba or available from: *
* <http://www.aldabaknocking.com/LICENSE.txt> *
* *
* Aldaba is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* v2.0 for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with Aldaba; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
* Please check file LICENSE.txt for the complete version of the license, *
* as this disclaimer does not contain the full information. Also, note *
* that although Aldaba is licensed under the GNU GPL v2.0 license, it may *
* be possible to obtain copies of it under different, less restrictive, *
* alternative licenses. Requests will be studied on a case by case basis. *
* If you wish to obtain Aldaba under a different license, please use the *
* email address shown above. *
* *
***************************************************************************/
/** \file filename.ext
* \brief Short description. */
#include "GeneralOps.h"
#include "ServerOps.h"
#include "output.h"
#include "tools.h"
#include "PKDataLight4.h"
#include "PKDataLight6.h"
#include "PKDataStrong4.h"
#include "PKDataStrong6.h"
/* Constructor */
ServerOps::ServerOps(){
this->reset();
}
ServerOps::~ServerOps(){
this->reset();
}
void ServerOps::reset(){
promiscuous=false;
promiscuous_set=false;
daemonize=true;
daemonize_set=false;
data_link_header_len=14;
data_link_header_len_set=false;
open_time=60;
open_time_set=false;
memset(bpf_filter, 0, sizeof(bpf_filter));
bpf_filter_set=false;
} /* End if reset() */
/** Sets Promiscuous.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int ServerOps::setPromiscuous(bool val){
this->promiscuous=val;
this->promiscuous_set=true;
return OP_SUCCESS;
} /* End of setPromiscuous() */
/** Returns value of attribute promiscuous */
bool ServerOps::getPromiscuous(){
return this->promiscuous;
} /* End of getPromiscuous() */
/* Returns true if option has been set */
bool ServerOps::issetPromiscuous(){
return this->promiscuous_set;
} /* End of issetPromiscuous() */
/** Sets open time in seconds.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int ServerOps::setOpenTime(int val){
printf("ServerOPs time set: %i \n", val);
this->open_time=val;
this->open_time_set=true;
return OP_SUCCESS;
} /* End of setOpenTime() */
/** Returns value of attribute open_time */
int ServerOps::getOpenTime(){
return this->open_time;
} /* End of getLoggingLevel() */
/* Returns true if option has been set */
bool ServerOps::issetOpenTime(){
return this->open_time_set;
}
/** Sets Daemonize.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int ServerOps::setDaemonize(bool val){
this->daemonize=val;
this->daemonize_set=true;
return OP_SUCCESS;
} /* End of setDaemonize() */
/** Returns value of attribute daemonize */
bool ServerOps::getDaemonize(){
return this->daemonize;
} /* End of getDaemonize() */
/* Returns true if option has been set */
bool ServerOps::issetDaemonize(){
return this->daemonize_set;
} /* End of issetDaemonize() */
/** Sets LinkHeaderLength.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int ServerOps::setLinkHeaderLength(u16 val){
this->data_link_header_len=val;
this->data_link_header_len_set=true;
return OP_SUCCESS;
} /* End of setLinkHeaderLength() */
/** Returns value of attribute data_link_header_len */
u16 ServerOps::getLinkHeaderLength(){
return this->data_link_header_len;
} /* End of getLinkHeaderLength() */
/* Returns true if option has been set */
bool ServerOps::issetLinkHeaderLength(){
return this->data_link_header_len_set;
} /* End of issetLinkHeaderLength() */
/** Sets BPF.
* @return OP_SUCCESS on success and OP_FAILURE in case of error. */
int ServerOps::setBPF(const char * val){
strncpy(this->bpf_filter, val, sizeof(this->bpf_filter)-1);
this->bpf_filter_set=true;
return OP_SUCCESS;
} /* End of setBPF() */
/** Returns value of attribute bpf_filter */
char * ServerOps::getBPF(){
return this->bpf_filter;
} /* End of getBPF() */
/* Returns true if option has been set */
bool ServerOps::issetBPF(){
return this->bpf_filter_set;
} /* End of issetBPF() */
int ServerOps::validateConfiguration(){
/* -> If no IP version is set, go with IP version 4 */
if( !this->issetIPVersion() )
this->setIPVersion(AF_INET);
/*----------------------------*
* OPERATION MODE (PK or SPA) *
*----------------------------*/
/* If no mode was specified, but only one port was supplied -> SPA */
if( !this->issetMode() && this->getNumberOfSequencePorts() == 1)
this->setMode(MODE_SPA);
/* If no mode was specified, but many ports were supplied -> PK */
if( !this->issetMode() && this->getNumberOfSequencePorts() > 1)
this->setMode(MODE_PORTKNOCKING);
/* If no mode was specified, set default */
if( !this->issetMode() )
this->setMode(DEFAULT_MODE);
/*---------------------------------*
* AUTHENTICATION & COVERT CHANNEL *
*---------------------------------*/
if( this->getMode()==MODE_PORTKNOCKING ){
/* If no auth was set, set default */
if( !this->issetAuthType() )
this->setAuthType(DEFAULT_AUTH);
/* If no cover channel heder field was set, set default */
if(!this->issetField() )
this->setField(DEFAULT_COVERT_FIELD);
}
/*-------------------*
* PORT SEQUENCE *
*-------------------*/
if( this->getMode()==MODE_PORTKNOCKING ){
/* Determine how many ports form the knocking port sequence */
u32 flen=field2len(this->getField());
size_t ports_needed;
if(this->getAuthType()==AUTH_TYPE_LIGHT && this->getIPVersion()==AF_INET){
ports_needed = PK_LIGHT_IPv4_DATA_LEN/flen;
}else if(this->getAuthType()==AUTH_TYPE_LIGHT && this->getIPVersion()==AF_INET6){
ports_needed = PK_LIGHT_IPv6_DATA_LEN/flen;
}else if(this->getAuthType()==AUTH_TYPE_STRONG && this->getIPVersion()==AF_INET){
ports_needed = PK_STRONG_IPv4_DATA_LEN/flen;
}else{ /* AUTH_TYPE_STRONG && AF_INET6 */
ports_needed = PK_STRONG_IPv6_DATA_LEN/flen;
}
/* Check user supplied the correct number of ports */
if( !this->issetSequencePorts()){
this->derivePortSequence(ports_needed);
}else if(this->getNumberOfSequencePorts() < ports_needed)
fatal(OUT_2, "Not enough target ports. %lu port numbers expected.", (unsigned long)ports_needed);
else if (this->getNumberOfSequencePorts() > ports_needed)
fatal(OUT_2, "Too many target ports. %lu port numbers expected.", (unsigned long)ports_needed);
}else{ /* MODE_SPA */
/* If no target port was supplied in SPA mode, use the default */
if( this->getNumberOfSequencePorts()==0 ){
this->derivePortSequence(1);
}else if( this->getNumberOfSequencePorts()>1 ){
fatal(OUT_2, "Too many target ports. SPA mode requires a single port number.");
}
}
/*----------------------------------*
* VERBOSITY AND LOGGING LEVELS *
*----------------------------------*/
/* -> If verbosity level was not specified, set default */
if ( !this->issetVerbosityLevel() ){
this->setVerbosityLevel(DEFAULT_VERBOSITY_SERVER);
}
/* -> If logging level was not specified, set default */
if ( !this->issetLoggingLevel() ){
this->setLoggingLevel(DEFAULT_LOGGING_SERVER);
}
/*--------------------------------*
* PRIVILEGES AND PERMISSIONS *
*--------------------------------*/
/* Determine if user is root */
this->setIsRoot((geteuid()==0) ? true : false);
/* For PK, user must be root */
if (!this->isRoot()){
fatal(OUT_2, "ERROR: You need to be root to run Aldaba Server.\n");
}
/*------------------*
* CRYPTOGRAPHY *
*------------------*/
/* If no encryption algorithm was specified, set default */
if (!this->issetCipher()){
if( this->getMode()==MODE_PORTKNOCKING)
this->setCipher(DEFAULT_ALG_PK);
else
this->setCipher(DEFAULT_ALG_SPA);
}else {
if( this->getMode()==MODE_PORTKNOCKING && this->getCipher()!=ALG_BLOWFISH )
fatal(OUT_2, "Sorry but use of Port Knocking is restricted to the Blowfish cipher");
}
/* If no cipher block mode was specified, set default */
if( !this->issetCipherMode() ){
if( this->getMode()==MODE_PORTKNOCKING)
this->setCipherMode(BLOCK_MODE_ECB);
else
this->setCipherMode(DEFAULT_BLOCK_MODE);
}else {
if( this->getMode()==MODE_PORTKNOCKING && this->getCipherMode()!=BLOCK_MODE_ECB )
fatal(OUT_2, "Sorry but use of Port Knocking is restricted to the ECB block cipher mode.");
}
/* Select a network interface */
if (!this->issetInterface()){
char *iface=NULL;
if( (iface=select_iface( this->getIPVersion() ))==NULL )
fatal(OUT_2, "Couldn't select network interface for capture. Please use option -i <iface>");
else
this->setInterface(iface);
}
/* If no passphrase was supplied, ask interactively if possible */
if(!this->issetPassphrase()){
if( this->getDaemonize()==false){
size_t read_bytes=0;
char buffer[MAX_PASSPHRASE_LEN];
memset(buffer, 0, MAX_PASSPHRASE_LEN);
printf("Please enter the passphrase: ");
read_password(buffer, MAX_PASSPHRASE_LEN-1, &read_bytes);
printf("\n");
if( strlen(buffer) < MIN_PASSPHRASE_LEN ){
fatal(OUT_2, "Supplied passphrase is too short. Passphrases need to contain at least %d characters", MIN_PASSPHRASE_LEN );
}else{
this->setPassphrase(buffer);
}
}else{
fatal(OUT_2, "Aldaba Server cannot be run in daemon mode without supplying a passphrase through the command line.");
}
}
this->computeCipherKey();
this->computeMacKey();
return OP_SUCCESS;
} /* End of validateConfiguration() */