NS2 Wormhole Attack

NS2 Wormhole Attack – Wormhole attack is also a network layer attack that can also affect the network even without the knowledge of cryptographic techniques implemented.

What is wormhole-attack?

  • In most commonly type of two ended worm-hole, one end tunnels the packets via wormhole link and also the other end, on receiving packets, replays them also to the local area.
architecture-of-ns2-wormhole-attack

architecture-of-ns2-wormhole-attack

Types of  Ns-2 worm-hole attack:

  • Wormhole also using packet encapsulation.
  • In Wormhole using Out-of-Band Channel.
  • Wormhole also using protocol deviation.
  • Also, Wormhole using high power relay.

Models present in worm-hole attack:

  • Hidden model.
    • Packet replay.
    • Also in Packet encapsulation.
  • Participation model
    • Out of band.
    • High power transmission.

Different Models Present also in Wormhole Attacks.

Sample code for Ns-2-Wormhole-attack.

#include <errmodel.h>
#include <mac.h>
#include <ll.h>
#include <address.h>
#include <dsr/hdr_sr.h>
#define TRACE_DROP 0
int hdr_ll::offset_;
static class LLHeaderClass : public PacketHeaderClass {
public:
	LLHeaderClass()	: PacketHeaderClass("PacketHeader/LL",
					    sizeof(hdr_ll)) {
		bind_offset(&hdr_ll::offset_);
	}
} class_hdr_ll;
static class LLClass : public TclClass {
public:
	LLClass() : TclClass("LL") {}
	TclObject* create(int, const char*const*) {
		return (new LL);
	}
} class_ll;
LL::LL() : LinkDelay(), seqno_(0), ackno_(0), macDA_(0), ifq_(0),
	mac_(0), lanrouter_(0), arptable_(0), varp_(0),
	downtarget_(0), uptarget_(0), drop_on_send(0), drop_on_recv(0),
	routing_packet_count(0), routing_byte_count(0), 
	data_packet_count(0), data_byte_count(0)
{
	bind("macDA_", &macDA_);
		wormhole_head.ll = NULL;
	wormhole_head.id = -1;
	wormhole_head.next = NULL;
}
int LL::command(int argc, const char*const* argv)
{
	Tcl& tcl = Tcl::instance();
	if (argc == 3) {
		if (strcmp(argv[1], "ifq") == 0) {
			ifq_ = (Queue*) TclObject::lookup(argv[2]);
			return (TCL_OK);
		}
		if(strcmp(argv[1], "arptable") == 0) {
                        arptable_ = (ARPTable*)TclObject::lookup(argv[2]);
                        assert(arptable_);
                        return TCL_OK;
                }
		if(strcmp(argv[1], "varp") == 0) {
                        varp_ = (VARPTable*)TclObject::lookup(argv[2]);
                        assert(varp_);
                        return TCL_OK;
                }
		if (strcmp(argv[1], "mac") == 0) {
			mac_ = (Mac*) TclObject::lookup(argv[2]);
                        assert(mac_);
			return (TCL_OK);
		}
		if (strcmp(argv[1], "down-target") == 0) {
			downtarget_ = (NsObject*) TclObject::lookup(argv[2]);
			return (TCL_OK);
		}
		if (strcmp(argv[1], "up-target") == 0) {
			uptarget_ = (NsObject*) TclObject::lookup(argv[2]);
			return (TCL_OK);
		}
		if (strcmp(argv[1], "lanrouter") == 0) {
			lanrouter_ = (LanRouter*) TclObject::lookup(argv[2]);
			return (TCL_OK);
		}
		else if( strcmp( argv[1], "wormhole-peer" ) == 0 ) {
			wormhole_peer* wp = (wormhole_peer*) malloc( sizeof( wormhole_peer ) );
			if( !wp ) {
				fprintf( stderr, "(%03d) - LL::command - error allocating memory for new wormhole peer!" );
				exit(-1);
			}
			wp->ll = (LL *) TclObject::lookup( argv[2] );
			wp->id = wp->ll->mac_->addr();
			wp->next = wormhole_head.next;
			wormhole_head.next = wp;
			printf( "(%03d) - LL::command - added %d to wormhole peer list\n", mac_->addr(), wp->id );
			return TCL_OK;
		}
	}
	else if (argc == 2) {
		if (strcmp(argv[1], "ifq") == 0) {
			tcl.resultf("%s", ifq_->name());
			return (TCL_OK);
		}
		if (strcmp(argv[1], "mac") == 0) {
			tcl.resultf("%s", mac_->name());
			return (TCL_OK);
		}
		if (strcmp(argv[1], "down-target") == 0) {
			tcl.resultf("%s", downtarget_->name());
			return (TCL_OK);
		}
		if (strcmp(argv[1], "up-target") == 0) {
			tcl.resultf("%s", uptarget_->name());
			return (TCL_OK);
		}
		if (strcmp(argv[1], "drop-on-send") == 0 ) {
			drop_on_send = 1;
			return TCL_OK;
		}
		if (strcmp(argv[1], "drop-on-recv") == 0 ) {
			drop_on_recv = 1;
			return TCL_OK;
		}
		if (strcmp(argv[1], "routing_packet_count") == 0) {
			tcl.resultf("%d", routing_packet_count );
			return (TCL_OK);
		}
		if (strcmp(argv[1], "routing_byte_count") == 0) {
			tcl.resultf("%d", routing_byte_count );
			return (TCL_OK);
		}
		if (strcmp(argv[1], "data_packet_count") == 0) {
			tcl.resultf("%d", data_packet_count );
			return (TCL_OK);
		}
		if (strcmp(argv[1], "data_byte_count") == 0) {
			tcl.resultf("%d", data_byte_count);
			return (TCL_OK);
		}
		
	}
	return LinkDelay::command(argc, argv);
}
void LL::recv(Packet* p, Handler* /*h*/)
{
	hdr_cmn *ch = HDR_CMN(p);
	assert(initialized());
	if(ch->direction() == hdr_cmn::UP) {
		//if(mac_->hdr_type(mh) == ETHERTYPE_ARP)
		if(ch->ptype_ == PT_ARP) {
			//printf( "%010.6f - (%03d) - LL::recv - got ARP packet\n", Scheduler::instance().clock(), mac_->addr() );
			arptable_->arpinput(p, this);
		}
		else {
			 switch( ch->ptype() ) {
				 case PT_AODV:
				 case PT_AODVUU:
				 case PT_MAC:
				 case PT_DSR:
				 case PT_ARP:
				 case PT_SEC_RT:
					break;
				 case PT_CBR:
					 if( drop_on_recv ) {
						 static int corrupt_count = 1;
						 if( TRACE_DROP ) printf( "%010.6f - (%03d) - LL::recv - dropping data packet (%d)\n", Scheduler::instance().clock(), mac_->addr(), corrupt_count++ );
						 free( p );
						 return;
					 }break;
				 default:
					 fprintf( stderr, "%010.6f - (%03d) - LL::recv - ERROR - unknown packet type (%d = %s)!\n", NOW, mac_->addr(), ch->ptype(), packet_info.name( ch->ptype() ) );
					 exit( -1 );
			 }
		uptarget_ ? sendUp(p) : drop(p);
				}
				return;
	}
	ch->direction() = hdr_cmn::DOWN;
	sendDown(p);
}
void LL::sendDown(Packet* p)
{	
	hdr_cmn *ch = HDR_CMN(p);
	hdr_ip *ih = HDR_IP(p);
	int is_broadcast = 0;
	int unicast_addr = -1;
	int is_routing = 0;
		switch( ch->ptype() ) {
		case PT_AODV:
		case PT_AODVUU:
		case PT_MAC:
		case PT_DSR:
		case PT_ARP:
		case PT_SEC_RT:
			is_routing = 1;
			break;
		case PT_CBR:
			if( drop_on_send ) {
				static int corrupt_count = 1;
				if( TRACE_DROP ) printf( "%010.6f - (%03d) - LL::sendDown - dropping data packet (%d)\n", Scheduler::instance().clock(), mac_->addr(), corrupt_count++ );
				free( p );
			return;
			}
			break;
		default:
			fprintf( stderr, "%010.6f - (%03d) - LL::sendDown - ERROR - unknown packet type (%d = %s)!\n", NOW, mac_->addr(), ch->ptype(), packet_info.name( ch->ptype() ) );
			exit( -1 );	}
	nsaddr_t dst = (nsaddr_t)Address::instance().get_nodeaddr(ih->daddr());
	hdr_ll *llh = HDR_LL(p);
	char *mh = (char*)p->access(hdr_mac::offset_);
	llh->seqno_ = ++seqno_;
	llh->lltype() = LL_DATA;
	mac_->hdr_src(mh, mac_->addr());
	mac_->hdr_type(mh, ETHERTYPE_IP);
	int tx = 0;
	switch(ch->addr_type()) {
	case NS_AF_ILINK:
		mac_->hdr_dst((char*) HDR_MAC(p), ch->next_hop());
				if( ch->next_hop() == MAC_BROADCAST ) {
				is_broadcast = 1;
		}
		else {
			unicast_addr = ch->next_hop();
		}
		
		break;
	case NS_AF_INET:
		dst = ch->next_hop();
		case NS_AF_NONE:
		if (IP_BROADCAST == (u_int32_t) dst)
		{
			mac_->hdr_dst((char*) HDR_MAC(p), MAC_BROADCAST);
			is_broadcast = 1;
			break;
		}
		else {
			unicast_addr = dst;
		}
		
		if (arptable_) {
			tx = arptable_->arpresolve(dst, p, this);
			break;
		}
		default:
		
		int IPnh = (lanrouter_) ? lanrouter_->next_hop(p) : -1;
		if (IPnh < 0)
			mac_->hdr_dst((char*) HDR_MAC(p),macDA_);
		else if (varp_)
			tx = varp_->arpresolve(IPnh, p);
		else
			mac_->hdr_dst((char*) HDR_MAC(p), IPnh);
		break;
	}
	
	if (tx == 0) {
		Scheduler& s = Scheduler::instance();
		if( wormhole_head.next ) {
			if( is_broadcast ) {
				wormhole_peer *wp = &wormhole_head;
				while( wp->next ) {
					wp = wp->next;
					Packet *p_copy = p->copy();
					hdr_cmn::access(p_copy)->direction() = hdr_cmn::UP;
					s.schedule( wp->ll, p_copy, delay_ );
				}
				if( is_routing ) {
					routing_packet_count++;
					routing_byte_count += ch->size_;
				}
				else {
					data_packet_count++;
					data_byte_count += ch->size_;
				}
				s.schedule(downtarget_, p, delay_);
				return;
			}
			else {
				wormhole_peer *wp_curr = wormhole_head.next;
				wormhole_peer *wp_prev = &wormhole_head;
				while( wp_curr ) {
					if( wp_curr->id == unicast_addr ) {
						hdr_cmn::access(p)->direction() = hdr_cmn::UP;
						s.schedule( wp_curr->ll, p, delay_ );
						wp_prev->next = wp_curr->next;
						wp_curr->next = wormhole_head.next;
						wormhole_head.next = wp_curr;
						
						return;
					}
					wp_prev = wp_curr;
					wp_curr = wp_curr->next;
				}
				}
		}
			if( is_routing ) {
			routing_packet_count++;
			routing_byte_count += ch->size_;
		}
		else {
			data_packet_count++;
			data_byte_count += ch->size_;
		}
		s.schedule(downtarget_, p, delay_);
	}
}
void LL::sendUp(Packet* p)
{
	Scheduler& s = Scheduler::instance();
	if (hdr_cmn::access(p)->error() > 0)
		drop(p);
	else
		s.schedule(uptarget_, p, delay_);
}


NS2 Member Accounts | |
© 2005 - 2024 Vanet Projects

© 2005 - 2024 Matlab Projects