Esempio n. 1
0
TEST_F(IPTest, StackedProtocols) {
    IP ip = IP() / TCP();
    IP::serialization_type buffer = ip.serialize();
    EXPECT_TRUE(IP(&buffer[0], buffer.size()).find_pdu<TCP>());
    
    ip = IP() / UDP();
    buffer = ip.serialize();
    EXPECT_TRUE(IP(&buffer[0], buffer.size()).find_pdu<UDP>());
    
    ip = IP() / ICMP();
    buffer = ip.serialize();
    EXPECT_TRUE(IP(&buffer[0], buffer.size()).find_pdu<ICMP>());
}
Esempio n. 2
0
TEST_F(IPTest, SpoofedOptions) {
    IP pdu;
    uint8_t a[] = { 1,2,3,4,5,6 };
    pdu.add_option(
        IP::option(IP::NOOP, 250, a, a + sizeof(a))
    );
    pdu.add_option(
        IP::option(IP::NOOP, 250, a, a + sizeof(a))
    );
    pdu.add_option(
        IP::option(IP::NOOP, 250, a, a + sizeof(a))
    );
    // probably we'd expect it to crash if it's not working, valgrind plx
    EXPECT_EQ(3U, pdu.options().size());
    EXPECT_EQ(pdu.serialize().size(), pdu.size());
}
Esempio n. 3
0
static int cb(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
              struct nfq_data *nfa, void *data)
{
    Mode mode = ((nf_data*)data)->mode;
    u_int32_t id = ntohl(nfq_get_msg_packet_hdr(nfa)->packet_id);
    unsigned char *packet;
    int size = nfq_get_payload(nfa, &packet);

    std::vector<uint8_t> result;
    u_int32_t verdict = NF_ACCEPT;

    try {
        IP ip = IP(packet, size);
        //cout << ip.src_addr() << " -> " << ip.dst_addr() << endl;
        NetworkInterface interface(ip.dst_addr());
        IP::address_type interface_addr = interface.addresses().ip_addr;

        RR *rr = ip.find_pdu<RR>();

        if (mode == ROUTER) {
            if (rr != 0) {
                //cout << "Adding to existing RR" << endl;
            } else {
                create_rr(ip);
                rr = ip.find_pdu<RR>();
                if (is_spoof && ip.src_addr() == IP::address_type("192.168.30.10")) {
                    rr->route().push_back(RREntry(IP::address_type("192.168.100.20"), 42, 42));
                }
            }

            // TODO Figure out what the magic numbers are for the source address
            RREntry src_hop(ip.src_addr(), 0x0, 0x0);
            RREntry last_hop = src_hop;
            if (rr != 0 && rr->route().size() >= 1)
                last_hop = rr->route().back();

            AITF_packet aitf;
            if (should_intercept(ip, rr, &aitf)) {
                //cout << "Intercepting packet" << endl;
                send_AITF_message(aitf, IP::address_type("192.168.10.100"));
                verdict = NF_DROP;
            }
            else if (is_blocked(last_hop, ip.dst_addr()) || (is_shadow && rr->route().size() > 1 && is_blocked(src_hop, ip.dst_addr()))) {
                //cout << "Packet blocked" << endl;
                verdict = NF_DROP;
            } else if (hosts.isLegacyHost(ip.dst_addr())) {
                //cout << "Legacy host detected" << endl;
                if (rr != 0) {
                    //print_route(rr->route());
                    strip_rr(ip, *rr);
                } else {
                    //cout << "No RR table present" << endl;
                }
            } else {
                NetworkInterface interface(ip.dst_addr());
                rr->route().push_back(RREntry(interface_addr, hash_for_destination(ip.dst_addr(), 0), hash_for_destination(ip.dst_addr(), 1)));

                if (rr->route().size() > rr->route_capacity()) {
                    //cout << "RR table filled. Dropping packet." << endl;
                    verdict = NF_DROP;
                } //else print_route(rr->route());
            }
        } else if (mode == HOST) {
            if (rr != 0) {
                // Check if this is a bad packet
                // TODO don't hard code attacker ip addresses
                bool is_bad = false;

                try {
                    UDP udp(&rr->payload()[0], rr->payload().size());
                    RawPDU* raw = udp.find_pdu<RawPDU>();
                    if (raw != 0) {
                        is_bad = raw->payload()[0] == 1;
                    }
                } catch (malformed_packet e) { }


                if (is_bad && is_victim) {
                    if (!last_event_set) {
                        printf("Attack detected\n");
                        gettimeofday(&last_event, NULL);
                        last_event_set = true;
                        event_delay = 500000;
                    } else {
                        struct timeval now;
                        gettimeofday(&now, NULL);
                        if ((now.tv_sec - last_event.tv_sec) * 1000000 + now.tv_usec - last_event.tv_usec > event_delay) {
                            printf("Enforce sent\n");
                            print_route(rr->route());

                            vector<RRFilter> filters;
                            filters.push_back(RRFilter(is_wildcard ? 1 : 0, ip.src_addr(), 0x0, 0x0));
                            for (int i = 0; i < rr->route().size(); i++) {
                                const RREntry& entry = rr->route().at(i);
                                filters.push_back(RRFilter(is_wildcard ? 1 : 0, entry.address(), entry.random_number_1(), entry.random_number_2()));
                            }

                            AITF_packet enforce(0, 0, 0, 1, filters, IP::address_type("192.168.10.10"), filters.size());
                            send_AITF_message(enforce, IP::address_type("192.168.10.100"));

                            last_event = now;
                            event_delay = is_aggressive ? 1000000 : 10000000;
                        }
                    }
                }

                //print_route(rr->route());
                strip_rr(ip, *rr);
            } else {
                //cout << "No RR table present" << endl;
            }
        }

        result = ip.serialize();
        //cout << endl;

    } catch (malformed_packet e) {
        cout << "malformed packet " << e.what() << endl;
    }
    
    return nfq_set_verdict(qh, id, verdict,
        verdict == NF_DROP ? 0 : result.size(),
        verdict == NF_DROP ? 0 : &result[0]);
}