00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #include <sys/types.h>
00114 #include <stdlib.h>
00115 #include <ctype.h>
00116
00117 #include "generators.h"
00118 #include "log.h"
00119 #include "detect.h"
00120 #include "decode.h"
00121 #include "event.h"
00122 #include "plugbase.h"
00123 #include "parser.h"
00124 #include "debug.h"
00125 #include "mstring.h"
00126 #include "util.h"
00127 #include "event_queue.h"
00128
00129 #include "inline.h"
00130
00131 #include "snort.h"
00132
00133 #define BACKORIFICE_DEFAULT_KEY 31337
00134 #define BACKORIFICE_MAGIC_SIZE 8
00135 #define BACKORIFICE_MIN_SIZE 18
00136 #define BACKORIFICE_DEFAULT_PORT 31337
00137 #define BO_TYPE_PING 1
00138 #define BO_FROM_UNKNOWN 0
00139 #define BO_FROM_CLIENT 1
00140 #define BO_FROM_SERVER 2
00141
00142 #define BO_BUF_SIZE 8
00143 #define BO_BUF_ATTACK_SIZE 1024
00144
00145
00146 #define START_LIST "{"
00147 #define END_LIST "}"
00148 #define CONF_SEPARATORS " \t\n\r"
00149 #define BO_ALERT_GENERAL 0x0001
00150 #define BO_ALERT_CLIENT 0x0002
00151 #define BO_ALERT_SERVER 0x0004
00152 #define BO_ALERT_SNORT_ATTACK 0x0008
00153
00154
00155
00156 void BoInit(u_char *);
00157 void BoProcess(Packet *);
00158 void BoFind(Packet *, void *);
00159
00160
00161 static int BoGetDirection(Packet *p, char *pkt_data);
00162 static void PrecalcPrefix();
00163 static char BoRand();
00164 static void ProcessArgs(u_char *args);
00165 static int ProcessOptionList(void);
00166 static void PrintConfig(void);
00167
00168 #define MODNAME "spp_bo"
00169
00170
00171
00172 static long holdrand = 1L;
00173
00174
00175 int brute_force_enable = 1;
00176 int default_key;
00177
00178 static u_int16_t noalert_flags = 0;
00179 static u_int16_t drop_flags = 0;
00180
00181
00182 u_int16_t lookup1[65536][3];
00183 u_int16_t lookup2[65536];
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 void SetupBo()
00197 {
00198
00199
00200 RegisterPreprocessor("bo", BoInit);
00201 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,
00202 "Preprocessor: Back Orifice is setup...\n"););
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 void BoInit(u_char *args)
00217 {
00218 static int bIsInitialized = 0;
00219
00220
00221 if ( !bIsInitialized )
00222 {
00223 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Preprocessor: Bo Initialized\n"););
00224
00225
00226 PrecalcPrefix();
00227
00228
00229 AddFuncToPreprocList(BoFind);
00230
00231 bIsInitialized = 1;
00232 }
00233
00234
00235 ProcessArgs(args);
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 static void ProcessArgs(u_char *args)
00256 {
00257 char *arg;
00258
00259 if ( args == NULL )
00260 return;
00261
00262 arg = strtok(args, CONF_SEPARATORS);
00263
00264 while ( arg != NULL )
00265 {
00266 if ( !strcasecmp("noalert", arg) )
00267 {
00268 noalert_flags = ProcessOptionList();
00269 }
00270 else if ( !strcasecmp("drop", arg) )
00271 {
00272 drop_flags = ProcessOptionList();
00273 }
00274 else
00275 {
00276 FatalError("%s(%d) => Unknown bo option %s.\n",
00277 file_name, file_line, arg);
00278 }
00279
00280 arg = strtok(NULL, CONF_SEPARATORS);
00281 }
00282
00283 PrintConfig();
00284
00285 return;
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 static int ProcessOptionList(void)
00300 {
00301 char *arg;
00302 int retFlags = 0;
00303 int endList = 0;
00304
00305 arg = strtok(NULL, CONF_SEPARATORS);
00306
00307 if ( arg == NULL || strcmp(START_LIST, arg) )
00308 {
00309 FatalError("%s(%d) => Invalid bo option.\n",
00310 file_name, file_line);
00311 return 0;
00312 }
00313
00314 while ( (arg = strtok(NULL, CONF_SEPARATORS)) )
00315 {
00316 if ( !strcmp(END_LIST, arg) )
00317 {
00318 endList = 1;
00319 break;
00320 }
00321
00322 if ( !strcasecmp("general", arg) )
00323 {
00324 retFlags |= BO_ALERT_GENERAL;
00325 }
00326 else if ( !strcasecmp("client", arg) )
00327 {
00328 retFlags |= BO_ALERT_CLIENT;
00329 }
00330 else if ( !strcasecmp("server", arg) )
00331 {
00332 retFlags |= BO_ALERT_SERVER;
00333 }
00334 else if ( !strcasecmp("snort_attack", arg) )
00335 {
00336 retFlags |= BO_ALERT_SNORT_ATTACK;
00337 }
00338 else
00339 {
00340 FatalError("%s(%d) => Invalid bo option argument %s.\n",
00341 file_name, file_line, arg);
00342 }
00343 }
00344
00345 if ( !endList )
00346 {
00347 FatalError("%s(%d) => Must end configuration list with %s.\n",
00348 file_name, file_line, END_LIST);
00349 return 0;
00350 }
00351
00352 return retFlags;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 static void PrintConfig(void)
00366 {
00367 if ( noalert_flags != 0 || drop_flags != 0 )
00368 LogMessage("Back Orifice Config:\n");
00369
00370 if ( noalert_flags != 0 )
00371 {
00372 LogMessage(" Disable alerts:");
00373 if ( noalert_flags & BO_ALERT_CLIENT )
00374 LogMessage(" client");
00375 if ( noalert_flags & BO_ALERT_SERVER )
00376 LogMessage(" server");
00377 if ( noalert_flags & BO_ALERT_GENERAL )
00378 LogMessage(" general");
00379 if ( noalert_flags & BO_ALERT_SNORT_ATTACK )
00380 LogMessage(" snort_attack");
00381 LogMessage("\n");
00382 }
00383 if ( drop_flags != 0 )
00384 {
00385 LogMessage(" Drop packets (inline only) on alerts:");
00386 if ( drop_flags & BO_ALERT_CLIENT )
00387 LogMessage(" client");
00388 if ( drop_flags & BO_ALERT_SERVER )
00389 LogMessage(" server");
00390 if ( drop_flags & BO_ALERT_GENERAL )
00391 LogMessage(" general");
00392 if ( drop_flags & BO_ALERT_SNORT_ATTACK )
00393 LogMessage(" snort_attack");
00394 LogMessage("\n");
00395 }
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 static char BoRand()
00408 {
00409 holdrand = holdrand * 214013L + 2531011L;
00410 return (char) (((holdrand >> 16) & 0x7fff) & 0xFF);
00411 }
00412
00413
00414
00415
00416
00417
00418
00419 static void PrecalcPrefix()
00420 {
00421 u_int8_t cookie_cyphertext[BACKORIFICE_MAGIC_SIZE];
00422 char *cookie_plaintext = "*!*QWTY?";
00423 int key;
00424 int cookie_index;
00425 char *cp_ptr;
00426 u_int16_t cyphertext_referent;
00427
00428 for(key=0;key<65536;key++)
00429 {
00430
00431 holdrand = key;
00432 cp_ptr = cookie_plaintext;
00433
00434
00435 for(cookie_index=0;cookie_index<BACKORIFICE_MAGIC_SIZE;cookie_index++)
00436 {
00437 cookie_cyphertext[cookie_index] =(u_int8_t)(*cp_ptr^(BoRand()));
00438 cp_ptr++;
00439 }
00440
00441
00442
00443
00444
00445 cyphertext_referent = (u_int16_t) (cookie_cyphertext[0] << 8) & 0xFF00;
00446 cyphertext_referent |= (u_int16_t) (cookie_cyphertext[1]) & 0x00FF;
00447
00448
00449 if(lookup1[cyphertext_referent][0] != 0)
00450 {
00451 if(lookup1[cyphertext_referent][1] != 0)
00452 {
00453 lookup1[cyphertext_referent][2] = key;
00454 }
00455 else
00456 {
00457 lookup1[cyphertext_referent][1] = key;
00458 }
00459 }
00460 else
00461 {
00462 lookup1[cyphertext_referent][0] = key;
00463 }
00464
00465
00466
00467
00468
00469 cyphertext_referent = (u_int16_t) (cookie_cyphertext[6] << 8) & 0xFF00;
00470 cyphertext_referent |= (u_int16_t) (cookie_cyphertext[7]) & 0x00FF;
00471
00472
00473
00474
00475 lookup2[key] = cyphertext_referent;
00476 }
00477 }
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 void BoFind(Packet *p, void *context)
00492 {
00493 u_int16_t cyphertext_referent;
00494 u_int16_t cyphertext_suffix;
00495 u_int16_t key;
00496 char *magic_cookie = "*!*QWTY?";
00497 char *pkt_data;
00498 char *magic_data;
00499 char *end;
00500 char plaintext;
00501 int i;
00502 int bo_direction = 0;
00503
00504
00505 if(!(p->preprocessors & PP_BO))
00506 {
00507 return;
00508 }
00509
00510
00511 if(!PacketIsUDP(p))
00512 {
00513 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,
00514 " -> spp_bo: Not UDP\n"););
00515 return;
00516 }
00517
00518 if(p->dsize < BACKORIFICE_MIN_SIZE)
00519 {
00520 return;
00521 }
00522
00523
00524
00525
00526
00527 cyphertext_referent = (u_int16_t) (p->data[0] << 8) & 0xFF00;
00528 cyphertext_referent |= (u_int16_t) (p->data[1]) & 0x00FF;
00529
00530
00531
00532
00533
00534 cyphertext_suffix = (u_int16_t) (p->data[6] << 8) & 0xFF00;
00535 cyphertext_suffix |= (u_int16_t) (p->data[7]) & 0x00FF;
00536
00537 for(i=0;i<3;i++)
00538 {
00539
00540 key = lookup1[cyphertext_referent][i];
00541
00542
00543
00544
00545
00546
00547
00548
00549 if(lookup2[key] == cyphertext_suffix)
00550 {
00551 holdrand = key;
00552 pkt_data = (char*)p->data;
00553 end = (char*)p->data + BACKORIFICE_MAGIC_SIZE;
00554 magic_data = magic_cookie;
00555
00556 while(pkt_data<end)
00557 {
00558 plaintext = (char) (*pkt_data ^ BoRand());
00559
00560 if(*magic_data != plaintext)
00561 {
00562 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,
00563 "Failed check one on 0x%X : 0x%X\n",
00564 *magic_data, plaintext););
00565 return;
00566 }
00567
00568 magic_data++;
00569 pkt_data++;
00570 }
00571
00572
00573 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,
00574 "Detected Back Orifice Data!\n");
00575 DebugMessage(DEBUG_PLUGIN, "hash value: %d\n", key););
00576
00577 bo_direction = BoGetDirection(p, pkt_data);
00578
00579 if ( bo_direction == BO_FROM_CLIENT )
00580 {
00581 if ( !(noalert_flags & BO_ALERT_CLIENT) )
00582 {
00583 SnortEventqAdd(GENERATOR_SPP_BO, BO_CLIENT_TRAFFIC_DETECT, 1, 0, 0,
00584 BO_CLIENT_TRAFFIC_DETECT_STR, 0);
00585 }
00586 if ( (drop_flags & BO_ALERT_CLIENT) && InlineMode() )
00587 {
00588 p->packet_flags |= PKT_INLINE_DROP;
00589 InlineDrop();
00590 }
00591 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Client packet\n"););
00592 }
00593 else if ( bo_direction == BO_FROM_SERVER )
00594 {
00595 if ( !(noalert_flags & BO_ALERT_SERVER) )
00596 {
00597 SnortEventqAdd(GENERATOR_SPP_BO, BO_SERVER_TRAFFIC_DETECT, 1, 0, 0,
00598 BO_SERVER_TRAFFIC_DETECT_STR, 0);
00599 }
00600 if ( (drop_flags & BO_ALERT_SERVER) && InlineMode() )
00601 {
00602 p->packet_flags |= PKT_INLINE_DROP;
00603 InlineDrop();
00604 }
00605 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Server packet\n"););
00606 }
00607 else
00608 {
00609 if ( !(noalert_flags & BO_ALERT_GENERAL) )
00610 {
00611 SnortEventqAdd(GENERATOR_SPP_BO, BO_TRAFFIC_DETECT, 1, 0, 0,
00612 BO_TRAFFIC_DETECT_STR, 0);
00613 }
00614 if ( (drop_flags & BO_ALERT_GENERAL) && InlineMode() )
00615 {
00616 p->packet_flags |= PKT_INLINE_DROP;
00617 InlineDrop();
00618 }
00619 }
00620 }
00621 }
00622
00623 return;
00624 }
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651 static int BoGetDirection(Packet *p, char *pkt_data)
00652 {
00653 u_int32_t len = 0;
00654 u_int32_t id = 0;
00655 u_int32_t l, i;
00656 char type;
00657 static char buf1[BO_BUF_SIZE];
00658 char plaintext;
00659
00660
00661 if ( p->dp == BACKORIFICE_DEFAULT_PORT )
00662 {
00663 return BO_FROM_CLIENT;
00664 }
00665 else if ( p->sp == BACKORIFICE_DEFAULT_PORT )
00666 {
00667 return BO_FROM_SERVER;
00668 }
00669
00670
00671
00672
00673 for ( i = 0; i < 4; i++ )
00674 {
00675 plaintext = (char) (*pkt_data ^ BoRand());
00676 l = (u_int32_t) plaintext;
00677 len += l << (8*i);
00678 pkt_data++;
00679 }
00680
00681
00682 for ( i = 0; i < 4; i++ )
00683 {
00684 plaintext = (char) (*pkt_data ^ BoRand() );
00685 l = ((u_int32_t) plaintext) & 0x000000FF;
00686 id += l << (8*i);
00687 pkt_data++;
00688 }
00689
00690 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Data length = %lu\n", len););
00691 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "ID = %lu\n", id););
00692
00693
00694
00695 if ( len >= BO_BUF_ATTACK_SIZE )
00696 {
00697 if ( !(noalert_flags & BO_ALERT_SNORT_ATTACK) )
00698 {
00699 SnortEventqAdd(GENERATOR_SPP_BO, BO_SNORT_BUFFER_ATTACK, 1, 0, 0,
00700 BO_SNORT_BUFFER_ATTACK_STR, 0);
00701 }
00702 if ( (drop_flags & BO_ALERT_SNORT_ATTACK) && InlineMode() )
00703 {
00704 p->packet_flags |= PKT_INLINE_DROP;
00705 InlineDrop();
00706 }
00707
00708 return BO_FROM_UNKNOWN;
00709 }
00710
00711
00712 len -= BACKORIFICE_MIN_SIZE;
00713
00714 if( len == 0 )
00715 {
00716
00717 return BO_FROM_UNKNOWN;
00718 }
00719
00720 if( len > 7 )
00721 {
00722 len = 7;
00723 }
00724
00725
00726
00727 if( p->dsize < len )
00728 {
00729
00730 return BO_FROM_UNKNOWN;
00731 }
00732
00733
00734 type = (char) (*pkt_data ^ BoRand());
00735 pkt_data++;
00736
00737 if ( type & 0x80 )
00738 {
00739 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Partial packet\n"););
00740 }
00741 if ( type & 0x40 )
00742 {
00743 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Continued packet\n"););
00744 }
00745
00746
00747 type = type & 0x3F;
00748
00749 DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Type = 0x%x\n", type););
00750
00751
00752 if ( type == BO_TYPE_PING )
00753 {
00754 if ( len < 7 )
00755 {
00756 return BO_FROM_CLIENT;
00757 }
00758
00759 for(i=0;i<len;i++ )
00760 {
00761 buf1[i] = (char) (pkt_data[i] ^ BoRand());
00762 if ( buf1[i] == 0 )
00763 {
00764 return BO_FROM_UNKNOWN;
00765 }
00766 }
00767
00768 if( ( buf1[3] == 'P' || buf1[3] == 'p' ) &&
00769 ( buf1[4] == 'O' || buf1[4] == 'o' ) &&
00770 ( buf1[5] == 'N' || buf1[5] == 'n' ) &&
00771 ( buf1[6] == 'G' || buf1[6] == 'g' ) )
00772 {
00773 return BO_FROM_SERVER;
00774 }
00775 else
00776 {
00777 return BO_FROM_CLIENT;
00778 }
00779 }
00780
00781 return BO_FROM_UNKNOWN;
00782 }