Ticket #466: airbase-ng-r1112-multi-essid-v0.03.patch

File airbase-ng-r1112-multi-essid-v0.03.patch, 12.9 KB (added by hdm@…, 16 months ago)

Patch to airbase for ticket #466

  • src/airbase-ng.c

     
    5858#include <getopt.h> 
    5959#include <sys/file.h> 
    6060#include <fcntl.h> 
     61#include <ctype.h> 
    6162 
    6263#include "version.h" 
    6364#include "pcap.h" 
     
    196197"      -Z type          : same as -z, but for WPA2\n" 
    197198"      -V type          : fake EAPOL 1=MD5 2=SHA1 3=auto\n" 
    198199"      -F prefix        : write all sent and received frames into pcap file\n" 
     200"      -P               : respond to all probes, even when specifying ESSIDs\n" 
     201"      -I interval      : sets the beacon interval value in ms\n" 
     202"      -C seconds       : enables beaconing of probed ESSID values (requires -P)\n" 
    199203"\n" 
    200204"  Filter options:\n" 
    201205"      --bssid MAC      : BSSID to filter/use\n" 
     
    245249    int weplen, crypt; 
    246250 
    247251    int f_essid; 
     252    int promiscuous; 
     253    int beacon_cache; 
    248254    int channel; 
    249255    int setWEP; 
    250256    int quiet; 
    251257    int mitm; 
    252258    int external; 
    253259    int hidden; 
     260    int interval; 
    254261    int forceska; 
    255262    int skalen; 
    256263    int filter; 
     
    311318    char            *essid; 
    312319    unsigned char   len; 
    313320    pESSID_t        next; 
     321        time_t          expire; 
    314322}; 
    315323 
    316324typedef struct MAC_list* pMAC_t; 
     
    420428        alarmed++; 
    421429} 
    422430 
    423 int addESSID(char* essid, int len) 
     431int addESSID(char* essid, int len, int expiration) 
    424432{ 
    425     pESSID_t cur = rESSID; 
    426  
     433    pESSID_t tmp; 
     434        pESSID_t cur = rESSID; 
     435        time_t now; 
     436         
    427437    if(essid == NULL) 
    428438        return -1; 
    429439 
     
    433443    if(rESSID == NULL) 
    434444        return -1; 
    435445 
    436     while(cur->next != NULL) 
     446    while(cur->next != NULL) { 
     447        // if it already exists, just update the expiration time 
     448        if(cur->len == len && ! memcmp(cur->essid, essid, len)) { 
     449            if(cur->expire && expiration) { 
     450                time(&now); 
     451                cur->expire = now + expiration; 
     452            } 
     453            return 0; 
     454        } 
    437455        cur = cur->next; 
     456    } 
    438457 
    439458    //alloc mem 
    440     cur->next = (pESSID_t) malloc(sizeof(struct ESSID_list)); 
    441     cur = cur->next; 
     459    tmp = (pESSID_t) malloc(sizeof(struct ESSID_list)); 
    442460 
    443461    //set essid 
    444     cur->essid = (char*) malloc(len+1); 
    445     memcpy(cur->essid, essid, len); 
    446     cur->essid[len] = 0x00; 
    447     cur->len = len; 
     462    tmp->essid = (char*) malloc(len+1); 
     463    memcpy(tmp->essid, essid, len); 
     464    tmp->essid[len] = 0x00; 
     465    tmp->len = len; 
     466         
     467    // set expiration date 
     468    if(expiration) { 
     469        time(&now); 
     470        tmp->expire = now + expiration; 
     471    } else { 
     472        tmp->expire = 0;    
     473    } 
    448474 
    449     cur->next = NULL; 
    450  
     475    tmp->next = NULL; 
     476        cur->next = tmp; 
     477                 
    451478    return 0; 
    452479} 
    453480 
     
    921948    return -1; 
    922949} 
    923950 
     951 
     952void flushESSID(void) 
     953{ 
     954    pESSID_t old; 
     955        pESSID_t cur = rESSID; 
     956        time_t now; 
     957 
     958    if(rESSID == NULL) 
     959        return; 
     960 
     961    while(cur->next != NULL) 
     962    { 
     963        old = cur->next; 
     964        if(old->expire) 
     965        { 
     966            time(&now); 
     967            if(now > old->expire) 
     968            { 
     969                //got it 
     970                cur->next = old->next; 
     971 
     972                free(old->essid); 
     973                old->essid = NULL; 
     974                old->next = NULL; 
     975                old->len = 0; 
     976                free(old); 
     977                return; 
     978            } 
     979        } 
     980        cur = cur->next; 
     981    } 
     982} 
     983 
     984 
    924985int delMAC(pMAC_t pMAC, char* mac) 
    925986{ 
    926987    pMAC_t old, cur = pMAC; 
     
    10651126{ 
    10661127    FILE *list; 
    10671128    char essid[256]; 
    1068  
     1129        int x; 
     1130         
    10691131    list = fopen(filename, "r"); 
    10701132    if(list == NULL) 
    10711133    { 
     
    10751137 
    10761138    while( fgets(essid, 256, list) != NULL ) 
    10771139    { 
    1078         addESSID(essid, strlen(essid)); 
     1140        // trim trailing whitespace 
     1141        x = strlen(essid) - 1; 
     1142        while (x >= 0 && isspace(essid[x])) 
     1143            essid[x--] = 0; 
     1144 
     1145        if(strlen(essid)) 
     1146            addESSID(essid, strlen(essid), 0); 
    10791147    } 
    10801148 
    10811149    fclose(list); 
     
    28372905            tag = parse_tags(packet+z, 0, length-z, &len); 
    28382906            if(tag != NULL && tag[0] >= 32 && tag[0] < 127 && len <= 255) //directed probe 
    28392907            { 
    2840                 if( !opt.f_essid || gotESSID((char*)tag, len) == 1) 
     2908                if( opt.promiscuous || !opt.f_essid || gotESSID((char*)tag, len) == 1) 
    28412909                { 
    28422910                    bzero(essid, 256); 
    28432911                    memcpy(essid, tag, len); 
     
    28472915                        if( essid[i] > 0 && essid[i] < ' ' ) 
    28482916                            goto skip_probe; 
    28492917 
    2850                     /* got a valid ASCII probed ESSID, check if it's 
    2851                     already in the ring buffer */ 
    2852  
     2918                    /* got a valid ASCII probed ESSID */ 
     2919                     
     2920                    /* add this to the beacon queue */ 
     2921                    if(opt.beacon_cache) 
     2922                        addESSID(essid, len, opt.beacon_cache); 
     2923                     
     2924                    /* check if it's already in the ring buffer */ 
    28532925                    for( i = 0; i < NB_PRB; i++ ) 
    28542926                        if( memcmp( st_cur->probes[i], essid, len ) == 0 ) 
    28552927                            goto skip_probe; 
     
    29373009 
    29383010                    send_packet(packet, length); 
    29393011 
    2940                     send_packet(packet, length); 
     3012                    //send_packet(packet, length); 
    29413013 
    2942                     send_packet(packet, length); 
     3014                    //send_packet(packet, length); 
    29433015                    return 0; 
    29443016                } 
    29453017            } 
     
    33393411    unsigned char beacon[512]; 
    33403412    int beacon_len=0; 
    33413413    int seq=0, i=0, n=0; 
     3414    int essid_len; 
     3415    char *essid = ""; 
     3416    pESSID_t cur_essid = rESSID; 
    33423417    float f, ticks[3]; 
    33433418 
    33443419    memcpy(&apc, arg, sizeof(struct AP_conf)); 
    33453420 
    3346     memcpy(beacon, "\x80\x00\x00\x00", 4);  //type/subtype/framecontrol/duration 
    3347     beacon_len+=4; 
    3348     memcpy(beacon+beacon_len , BROADCAST, 6);        //destination 
    3349     beacon_len+=6; 
    3350     if(!opt.adhoc) 
    3351         memcpy(beacon+beacon_len, apc.bssid, 6);        //source 
    3352     else 
    3353         memcpy(beacon+beacon_len, opt.r_smac, 6);        //source 
    3354     beacon_len+=6; 
    3355     memcpy(beacon+beacon_len, apc.bssid, 6);        //bssid 
    3356     beacon_len+=6; 
    3357     memcpy(beacon+beacon_len, "\x00\x00", 2);       //seq+frag 
    3358     beacon_len+=2; 
    3359  
    3360     memcpy(beacon+beacon_len, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12);  //fixed information 
    3361     beacon[beacon_len+8] = (apc.interval) & 0xFF;       //beacon interval 
    3362     beacon[beacon_len+9] = (apc.interval >> 8) & 0xFF; 
    3363     memcpy(beacon+beacon_len+10, apc.capa, 2);          //capability 
    3364     beacon_len+=12; 
    3365  
    3366     beacon[beacon_len] = 0x00; //essid tag 
    3367     beacon[beacon_len+1] = apc.essid_len; //essid tag 
    3368     beacon_len+=2; 
    3369     memcpy(beacon+beacon_len, apc.essid, apc.essid_len); //actual essid 
    3370     beacon_len+=apc.essid_len; 
    3371  
    3372     memcpy(beacon+beacon_len, RATES, 16); //rates+extended rates 
    3373     beacon_len+=16; 
    3374  
    3375     beacon[beacon_len] = 0x03; //channel tag 
    3376     beacon[beacon_len+1] = 0x01; 
    3377     beacon[beacon_len+2] = wi_get_channel(_wi_in); //current channel 
    3378     beacon_len+=3; 
    3379  
    3380     if( opt.allwpa ) 
    3381     { 
    3382         memcpy(beacon+beacon_len, WPA_TAGS, 0x56); 
    3383         beacon_len += 0x56; 
    3384     } 
    3385  
    3386     if(opt.wpa2type > 0) 
    3387     { 
    3388         memcpy(beacon+beacon_len, WPA2_TAG, 22); 
    3389         beacon[beacon_len+7] = opt.wpa2type; 
    3390         beacon[beacon_len+13] = opt.wpa2type; 
    3391         beacon_len += 22; 
    3392     } 
    3393  
    3394     if(opt.wpa1type > 0) 
    3395     { 
    3396         memcpy(beacon+beacon_len, WPA1_TAG, 24); 
    3397         beacon[beacon_len+11] = opt.wpa1type; 
    3398         beacon[beacon_len+17] = opt.wpa1type; 
    3399         beacon_len += 24; 
    3400     } 
    3401  
    34023421    ticks[0]=0; 
    34033422    ticks[1]=0; 
    34043423    ticks[2]=0; 
     
    34463465 
    34473466//             printf( "4 " ); 
    34483467            fflush(stdout); 
     3468                         
     3469 
     3470            if(cur_essid == NULL) cur_essid = rESSID; 
     3471            if(cur_essid == NULL) { 
     3472                    essid = "default"; 
     3473                    essid_len = strlen(essid);   
     3474            } else { 
     3475                 
     3476                /* flush expired ESSID entries */ 
     3477                flushESSID(); 
     3478                     
     3479                    essid     = cur_essid->essid; 
     3480                    essid_len = cur_essid->len; 
     3481                    cur_essid = cur_essid->next;                         
     3482            } 
     3483 
     3484            beacon_len = 0; 
     3485 
     3486            memcpy(beacon, "\x80\x00\x00\x00", 4);  //type/subtype/framecontrol/duration 
     3487            beacon_len+=4; 
     3488            memcpy(beacon+beacon_len , BROADCAST, 6);        //destination 
     3489            beacon_len+=6; 
     3490            if(!opt.adhoc) 
     3491                memcpy(beacon+beacon_len, apc.bssid, 6);        //source 
     3492            else 
     3493                memcpy(beacon+beacon_len, opt.r_smac, 6);        //source 
     3494            beacon_len+=6; 
     3495            memcpy(beacon+beacon_len, apc.bssid, 6);        //bssid 
     3496            beacon_len+=6; 
     3497            memcpy(beacon+beacon_len, "\x00\x00", 2);       //seq+frag 
     3498            beacon_len+=2; 
     3499 
     3500            memcpy(beacon+beacon_len, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12);  //fixed information 
     3501 
     3502            beacon[beacon_len+8] = (apc.interval * MAX(getESSIDcount(), 1) ) & 0xFF;       //beacon interval 
     3503            beacon[beacon_len+9] = (apc.interval * MAX(getESSIDcount(), 1) >> 8) & 0xFF; 
     3504            memcpy(beacon+beacon_len+10, apc.capa, 2);          //capability 
     3505            beacon_len+=12; 
     3506 
     3507            beacon[beacon_len] = 0x00; //essid tag 
     3508            beacon[beacon_len+1] = essid_len; //essid tag 
     3509            beacon_len+=2; 
     3510            memcpy(beacon+beacon_len, essid, essid_len); //actual essid 
     3511            beacon_len+=essid_len; 
     3512 
     3513            memcpy(beacon+beacon_len, RATES, 16); //rates+extended rates 
     3514            beacon_len+=16; 
     3515 
     3516            beacon[beacon_len] = 0x03; //channel tag 
     3517            beacon[beacon_len+1] = 0x01; 
     3518            beacon[beacon_len+2] = wi_get_channel(_wi_in); //current channel 
     3519            beacon_len+=3; 
     3520 
     3521            if( opt.allwpa ) 
     3522            { 
     3523                memcpy(beacon+beacon_len, WPA_TAGS, 0x56); 
     3524                beacon_len += 0x56; 
     3525            } 
     3526 
     3527            if(opt.wpa2type > 0) 
     3528            { 
     3529                memcpy(beacon+beacon_len, WPA2_TAG, 22); 
     3530                beacon[beacon_len+7] = opt.wpa2type; 
     3531                beacon[beacon_len+13] = opt.wpa2type; 
     3532                beacon_len += 22; 
     3533            } 
     3534 
     3535            if(opt.wpa1type > 0) 
     3536            { 
     3537                memcpy(beacon+beacon_len, WPA1_TAG, 24); 
     3538                beacon[beacon_len+11] = opt.wpa1type; 
     3539                beacon[beacon_len+17] = opt.wpa1type; 
     3540                beacon_len += 24; 
     3541            }                                    
     3542 
     3543                 
    34493544            //copy timestamp into beacon; a mod 2^64 counter incremented each microsecond 
    34503545            for(i=0; i<8; i++) 
    34513546            { 
     
    34573552 
    34583553//             printf( "5 " ); 
    34593554            fflush(stdout); 
     3555 
    34603556            if( send_packet( beacon, beacon_len ) < 0 ) 
    34613557            { 
    34623558                printf("Error sending beacon!\n"); 
     
    37773873    opt.ringbuffer  = 10; 
    37783874    opt.nb_arp      = 0; 
    37793875    opt.f_index     = 1; 
    3780  
     3876    opt.interval    = 0x64; 
     3877    opt.beacon_cache = 0; /* disable by default */ 
     3878         
    37813879    srand( time( NULL ) ); 
    37823880 
    37833881    while( 1 ) 
     
    37853883        int option_index = 0; 
    37863884 
    37873885        static struct option long_options[] = { 
     3886            {"beacon-cache",1, 0, 'C'}, 
    37883887            {"bssid",       1, 0, 'b'}, 
    37893888            {"bssids",      1, 0, 'B'}, 
    37903889            {"channel",     1, 0, 'c'}, 
     
    37923891            {"clients",     1, 0, 'D'}, 
    37933892            {"essid",       1, 0, 'e'}, 
    37943893            {"essids",      1, 0, 'E'}, 
     3894            {"promiscuous", 0, 0, 'P'}, 
     3895            {"interval",    1, 0, 'I'}, 
    37953896            {"mitm",        0, 0, 'M'}, 
    37963897            {"hidden",      0, 0, 'X'}, 
    37973898            {"caffe-latte", 0, 0, 'L'}, 
     
    38033904        }; 
    38043905 
    38053906        int option = getopt_long( argc, argv, 
    3806                         "a:h:i:r:w:He:E:c:d:D:f:W:qMY:b:B:XsS:Lx:vAz:Z:yV:0NF:", 
     3907                        "a:h:i:C:I:r:w:HPe:E:c:d:D:f:W:qMY:b:B:XsS:Lx:vAz:Z:yV:0NF:", 
    38073908                        long_options, &option_index ); 
    38083909 
    38093910        if( option < 0 ) break; 
     
    38903991 
    38913992            case 'e' : 
    38923993 
    3893                 if( addESSID(optarg, strlen(optarg)) != 0 ) 
     3994                if( addESSID(optarg, strlen(optarg), 0) != 0 ) 
    38943995                { 
    38953996                    printf( "Invalid ESSID, too long\n" ); 
    38963997                    printf("\"%s --help\" for help.\n", argv[0]); 
     
    39094010                opt.f_essid = 1; 
    39104011 
    39114012                break; 
     4013                                 
     4014            case 'P' : 
    39124015 
     4016                opt.promiscuous = 1; 
     4017 
     4018                break; 
     4019                                 
     4020            case 'I' : 
     4021 
     4022                opt.interval = atoi(optarg); 
     4023 
     4024                break; 
     4025                                 
     4026            case 'C' : 
     4027 
     4028                opt.beacon_cache = atoi(optarg); 
     4029 
     4030                break; 
     4031                                                         
    39134032            case 'A' : 
    39144033 
    39154034                opt.adhoc = 1; 
     
    44614580        apc.essid = "\x00"; 
    44624581        apc.essid_len = 1; 
    44634582    } 
    4464     apc.interval = 0x0064; 
     4583    apc.interval = opt.interval; 
    44654584    apc.capa[0] = 0x00; 
    44664585    if(opt.adhoc) 
    44674586        apc.capa[0] |= 0x02;