Ejemplo n.º 1
0
int rm_pigsty_entry(pigsty_entry_ctx **entries, const char *signature_name) {
    pigsty_entry_ctx *ep = NULL, *lp = NULL, *np = NULL;
    int rt = 0;

    if (entries == NULL || signature_name == NULL) {
        return 0;
    }

    ep = *entries;

    while (ep != NULL) {
        if (strglob(ep->signature_name, signature_name)) {
            np = ep->next;

            if (lp == NULL) {
                *entries = ep->next;
                ep->next = NULL;
            } else {
                lp->next = ep->next;
                ep->next = NULL;
            }

            del_pigsty_entry(ep);
            rt++;

            ep = np;
        } else {
            lp = ep;
            ep = ep->next;
        }
    }

    return rt;
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: huap/pig
static int run_pig_run(const char *signatures, const char *targets, const char *timeout, const char *single_test, const char *gw_addr, const char *nt_mask, const char *loiface) {
    int timeo = 10000;
    pigsty_entry_ctx *pigsty = NULL;
    size_t signatures_count = 0, addr_count = 0;
    pigsty_entry_ctx *signature = NULL, *sp = NULL;
    pig_target_addr_ctx *addr = NULL, *addr_p = NULL;
    pig_hwaddr_ctx *hwaddr = NULL;
    int sockfd = -1;
    int retval = 0;
    unsigned int nt_mask_addr[4] = { 0, 0, 0, 0 };
    unsigned char *gw_hwaddr = NULL, *temp = NULL;
    in_addr_t gw_in_addr = 0;
    if (timeout != NULL) {
        timeo = atoi(timeout);
    }
    timeo = timeo * 1000;
    if (!should_be_quiet) {
        printf("pig INFO: starting up pig engine...\n\n");
    }
    sockfd = init_raw_socket(loiface);
    if (sockfd == -1) {
        printf("pig PANIC: unable to create the socket.\npig ERROR: aborted.\n");
        return 1;
    }
    pigsty = load_signatures(signatures);
    if (pigsty == NULL) {
        printf("pig ERROR: aborted.\n");
        deinit_raw_socket(sockfd);
        return 1;
    }
    if (targets != NULL) {
        if (!should_be_quiet) {
            printf("\npig INFO: parsing the supplied targets...\n");
            printf("pig INFO: all targets were parsed.\n");
        }
        addr = parse_targets(targets);
    }
    if (is_targets_option_required(pigsty) && addr == NULL) {
        printf("pig PANIC: --targets option is required by some loaded signatures.\n");
        deinit_raw_socket(sockfd);
        del_pigsty_entry(pigsty);
        return 1;
    }
    signatures_count = get_pigsty_entry_count(pigsty);
    if (!should_be_quiet) {
        printf("\npig INFO: done (%d signature(s) read).\n\n", signatures_count);
    }
    if (nt_mask == NULL) {
        printf("\npig PANIC: --net-mask option is required.\n");
        deinit_raw_socket(sockfd);
        del_pigsty_entry(pigsty);
        return 1;
    }
    //  WARN(Santiago): by now IPv4 only.
    if (verify_ipv4_addr(nt_mask) == 0) {
        printf("pig PANIC: --net-mask has an invalid ip address.\n");
        deinit_raw_socket(sockfd);
        del_pigsty_entry(pigsty);
        return 1;
    }
    nt_mask_addr[0] = htonl(inet_addr(nt_mask));
    if (gw_addr != NULL && loiface != NULL) {
        gw_in_addr = inet_addr(gw_addr);
        temp = get_mac_by_addr(gw_in_addr, loiface, 2);
        if (!should_be_quiet && temp != NULL) {
            gw_hwaddr = mac2byte(temp, strlen(temp));
            printf("pig INFO: the gateway's physical address is \"%s\"...\n"
                   "pig INFO: the local interface is \"%s\"...\n"
                   "pig INFO: the network mask is \"%s\"...\n\n", temp, loiface, nt_mask);
            free(temp);
        }
    }
    if (gw_hwaddr != NULL) {
        signature = get_pigsty_entry_by_index(rand() % signatures_count, pigsty);
        if (single_test == NULL) {
            while (!should_exit) {
                if (signature == NULL) {
                    continue; //  WARN(Santiago): It should never happen. However... Sometimes... The World tends to be a rather weird place.
                }
                if (oink(signature, &hwaddr, addr, sockfd, gw_hwaddr, nt_mask_addr, loiface) != -1) {
                    if (!should_be_quiet) {
                        printf("pig INFO: a packet based on signature \"%s\" was sent.\n", signature->signature_name);
                    }
                    usleep(timeo);
                }
                signature = get_pigsty_entry_by_index(rand() % signatures_count, pigsty);
            }
        } else {
            retval = (oink(signature, &hwaddr, addr, sockfd, gw_hwaddr, nt_mask_addr, loiface) != -1 ? 0 : 1);
            if (retval == 0) {
                if (!should_be_quiet) {
                    printf("pig INFO: a packet based on signature \"%s\" was sent.\n", signature->signature_name);
                }
            }
        }
        free(gw_hwaddr);
    } else {
        printf("\npig PANIC: unable to get the gateway's physical address.\n");
    }
    del_pigsty_entry(pigsty);
    del_pig_target_addr(addr);
    del_pig_hwaddr(hwaddr);
    deinit_raw_socket(sockfd);
    return retval;
}