├── .gitignore ├── LICENSE.txt ├── README.md ├── composer.json ├── example.php └── src └── BlakeGardner └── MacAddress.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Blake Gardner 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP MAC Address 2 | 3 | This is a PHP class for MAC address manipulation on top of Unix, Linux and Mac 4 | OS X operating systems. it was primarily written to help with spoofing for 5 | wireless security audits. 6 | 7 | ## Capabilities 8 | 9 | * Verify you are executing it from the command line 10 | * Verify you are running the script as an administrator 11 | * Generate new random MAC addresses 12 | * Validate MAC addresses 13 | * Get the current system’s MAC address 14 | * Set or “spoof” any MAC address you want 15 | 16 | ## Usage 17 | 18 | ``` php 19 | // require the class 20 | require_once './src/BlakeGardner/MacAddress.php'; 21 | 22 | // import the class 23 | use BlakeGardner\MacAddress; 24 | 25 | // get the mac address of the eth0 interface 26 | var_dump(MacAddress::getCurrentMacAddress('eth0')); 27 | 28 | // generate a random mac address 29 | var_dump(MacAddress::generateMacAddress()); 30 | 31 | // validate an MAC address 32 | var_dump(MacAddress::validateMacAddress('00-B0-D0-86-BB-F7')); 33 | 34 | // set a randomly generated MAC address on the eth0 interface 35 | var_dump(MacAddress::setFakeMacAddress('eth0')); 36 | 37 | // set a specific MAC address on the eth0 interface 38 | var_dump(MacAddress::setFakeMacAddress('eth0', '00:E4:01:2C:79:DA')); 39 | 40 | // get the mac address of the eth0 interface using the ifconfig path that we define 41 | var_dump(MacAddress::getCurrentMacAddress('eth0', '/usr/local/sbin/ifconfig')); 42 | echo "\n"; 43 | ``` 44 | 45 | For more see the example.php file. You can run the example on the command line 46 | as root. `php example.php` 47 | 48 | ## Planned Features 49 | 50 | * List all interfaces on the system 51 | * OS detection 52 | * Suppress errors on the command line 53 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blake-gardner/mac-address", 3 | "description": "Get, validate and generate MAC addresses.", 4 | "license": "MIT", 5 | "keywords": ["MAC", "Address", "IP", "ifconfig", "DHCP"], 6 | "authors": [ 7 | { 8 | "name": "Blake Gardner", 9 | "email": "blakegardner@cox.net" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.3.2" 14 | }, 15 | "require-dev": { 16 | "squizlabs/php_codesniffer": "dev-phpcs-fixer" 17 | }, 18 | "autoload": { 19 | "psr-0": { 20 | "BlakeGardner\\MacAddress": "src/" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright Copyright (c) 2012, Blake Gardner 11 | * @license MIT License (see License.txt) 12 | */ 13 | class MacAddress 14 | { 15 | 16 | /** 17 | * Regular expression for matching and validating a MAC address 18 | * @var string 19 | */ 20 | private static $valid_mac = "([0-9A-F]{2}[:-]){5}([0-9A-F]{2})"; 21 | 22 | /** 23 | * An array of valid MAC address characters 24 | * @var array 25 | */ 26 | private static $mac_address_vals = array( 27 | "0", "1", "2", "3", "4", "5", "6", "7", 28 | "8", "9", "A", "B", "C", "D", "E", "F" 29 | ); 30 | 31 | /** 32 | * Path where ifconfig will be searched by default 33 | */ 34 | public static function getIfconfig() { 35 | $paths = array( 36 | "/bin/ifconfig", 37 | "/sbin/ifconfig", 38 | "/usr/bin/ifconfig", 39 | "/usr/sbin/ifconfig" 40 | ); 41 | foreach ($paths as $path) { 42 | if (file_exists($path)) { 43 | return $path; 44 | } 45 | } 46 | return "ifconfig"; 47 | } 48 | 49 | /** 50 | * Change the MAC address of the network interface specified 51 | * @param string $interface Name of the interface e.g. eth0 52 | * @param string $mac The new MAC address to be set to the interface 53 | * @return bool Returns true on success else returns false 54 | */ 55 | public static function setFakeMacAddress($interface, $mac = null, $ifconfig = null) 56 | { 57 | 58 | // if a valid mac address was not passed then generate one 59 | if (!self::validateMacAddress($mac)) { 60 | $mac = self::generateMacAddress(); 61 | } 62 | 63 | // if ifconfig is not defined, the default value is used. 64 | if (is_null($ifconfig)) { 65 | $ifconfig = self::getIfconfig(); 66 | } 67 | 68 | // bring the interface down, set the new mac, bring it back up 69 | self::runCommand($ifconfig. " {$interface} down"); 70 | self::runCommand($ifconfig. " {$interface} hw ether {$mac}"); 71 | self::runCommand($ifconfig. " {$interface} up"); 72 | 73 | // TODO: figure out if there is a better method of doing this 74 | // run DHCP client to grab a new IP address 75 | self::runCommand("dhclient {$interface}"); 76 | 77 | // run a test to see if the operation was a success 78 | if (self::getCurrentMacAddress($interface) == $mac) { 79 | return true; 80 | } 81 | 82 | // by default just return false 83 | return false; 84 | } 85 | 86 | /** 87 | * @return string generated MAC address 88 | */ 89 | public static function generateMacAddress() 90 | { 91 | $vals = self::$mac_address_vals; 92 | if (count($vals) >= 1) { 93 | $mac = array("00"); // set first two digits manually 94 | while (count($mac) < 6) { 95 | shuffle($vals); 96 | $mac[] = $vals[0] . $vals[1]; 97 | } 98 | $mac = implode(":", $mac); 99 | } 100 | return $mac; 101 | } 102 | 103 | /** 104 | * Make sure the provided MAC address is in the correct format 105 | * @param string $mac 106 | * @return bool true if valid; otherwise false 107 | */ 108 | public static function validateMacAddress($mac) 109 | { 110 | return (bool) preg_match("/^" . self::$valid_mac . "$/i", $mac); 111 | } 112 | 113 | /** 114 | * Run the specified command and return it's output 115 | * @param string $command 116 | * @return string Output from command that was ran 117 | */ 118 | protected static function runCommand($command) 119 | { 120 | return shell_exec($command); 121 | } 122 | 123 | /** 124 | * Get the system's current MAC address 125 | * @param string $interface The name of the interface e.g. eth0 126 | * @return string|bool Systems current MAC address; otherwise false on error 127 | */ 128 | public static function getCurrentMacAddress($interface, $ifconfig = null) 129 | { 130 | // if ifconfig is not defined, the default value is used. 131 | if (is_null($ifconfig)) { 132 | $ifconfig = self::getIfconfig(); 133 | } 134 | 135 | $ifconfig = self::runCommand($ifconfig . " {$interface}"); 136 | preg_match("/" . self::$valid_mac . "/i", $ifconfig, $ifconfig); 137 | if (isset($ifconfig[0])) { 138 | return trim(strtoupper($ifconfig[0])); 139 | } 140 | return false; 141 | } 142 | } 143 | --------------------------------------------------------------------------------