| 1 | <?php\r |
| 2 | //Harvie's MAC sniffing toolkit (2oo7)\r |
| 3 | //Vice informaci cesky: https://www.soom.cz/articles/print.php?aid=406\r |
| 4 | \r |
| 5 | /*\r |
| 6 | This if primary for MS Windows (may work at other system, depending on 3rd side programs' output)\r |
| 7 | 3rd side programs:\r |
| 8 | - ping\r |
| 9 | - arp\r |
| 10 | - ngrep (requires WinPCap for Windows or LibPCap for Unixs)\r |
| 11 | */\r |
| 12 | \r |
| 13 | ///SETTINGS/////////////////////////////////////\r |
| 14 | $ngrep = "ngrep"; //NGREP binary\r |
| 15 | $ping = "ping -n 1"; //PING with arguments\r |
| 16 | $arp = "arp -a"; //ARP with arguments to show all ARP records\r |
| 17 | \r |
| 18 | ///FUNCTIONS////////////////////////////////////\r |
| 19 | \r |
| 20 | //Get HW (MAC) address from IP address\r |
| 21 | function get_mac($ip) {\r |
| 22 | $ip = trim($ip);\r |
| 23 | shell_exec($GLOBALS["ping"]." ".$ip);\r |
| 24 | $arp = shell_exec($GLOBALS["arp"]);\r |
| 25 | $arp = explode("\n", $arp);\r |
| 26 | foreach($arp as $line) {\r |
| 27 | if(ereg(": $ip ---", $line)) { return("This is your adapter, to find MAC try \"ipconfig /all\""); }\r |
| 28 | if(ereg(" $ip ", $line)) {\r |
| 29 | //echo($line."\n"); //Debug\r |
| 30 | $line = explode($ip, $line);\r |
| 31 | $line = trim($line[1]);\r |
| 32 | $line = explode("dynamic", $line);\r |
| 33 | $line = trim($line[0]);\r |
| 34 | //echo($line."\n"); //Debug\r |
| 35 | return($line);\r |
| 36 | }\r |
| 37 | }\r |
| 38 | return("Not found. Couldn't broadcast to IP.");\r |
| 39 | }\r |
| 40 | \r |
| 41 | //Passive scan for active computers (IPs) in network (it's 100% stealth),\r |
| 42 | //but you can use "nmap" (for example) for scanning more more quickly and efectively...\r |
| 43 | //This is waiting in infinite loop...\r |
| 44 | function sniff_ips($device = 1, $subnet = "") {\r |
| 45 | $device = trim($device);\r |
| 46 | $subnet = trim($subnet);\r |
| 47 | $ngrep = ($GLOBALS["ngrep"]." -d ".$device);\r |
| 48 | $fp = popen($ngrep, "r");\r |
| 49 | \r |
| 50 | $ips[0] = "";\r |
| 51 | $i = 0;\r |
| 52 | while($fp && !feof($fp)) {\r |
| 53 | $line = fgets($fp);\r |
| 54 | if(ereg("$subnet.*:.* -> .*:.*", $line)) {\r |
| 55 | $line = explode(" ", $line);\r |
| 56 | $line = explode(":", $line[1]);\r |
| 57 | $ip = trim($line[0]);\r |
| 58 | \r |
| 59 | if(!in_array($ip, $ips)) {\r |
| 60 | $ips[$i] = $ip;\r |
| 61 | $i++;\r |
| 62 | \r |
| 63 | //You have $ip, you can do anything, that you want:\r |
| 64 | echo($ip." = ".get_mac($ip)."\n"); //Get it's MAC and print it\r |
| 65 | \r |
| 66 | }\r |
| 67 | }\r |
| 68 | }\r |
| 69 | }\r |
| 70 | \r |
| 71 | //Quick active scan for MACs and IPS\r |
| 72 | function quick_ipmac_scan($subnet = "192.168.1") {\r |
| 73 | for($i=1;$i<256;$i++) {\r |
| 74 | //Mega threaded ( This will open 255 processes ;))\r |
| 75 | $fp[$i] = popen($GLOBALS["ping"]." ".$subnet.".".$i, "r");\r |
| 76 | }\r |
| 77 | for($i=1;$i<256;$i++) {\r |
| 78 | while( $fp[$i] && !feof($fp[$i]) ) { fgets($fp[$i]); }\r |
| 79 | }\r |
| 80 | system($GLOBALS["arp"]);\r |
| 81 | }\r |
| 82 | \r |
| 83 | ///Examples of usage://///////////////////////////////////////////////////////\r |
| 84 | //You have to modify this script, to get that output format, that you want...\r |
| 85 | \r |
| 86 | \r |
| 87 | //Sniff for IPs:\r |
| 88 | echo("Sniffing for IP/MAC addresses\nC-c for stop\n\n");\r |
| 89 | //This will sniff on 3rd device ("ngrep -L" for device listing)\r |
| 90 | //And only IPs that starts with "192.168" will be accepted\r |
| 91 | sniff_ips(3, "192.168"); //ngrep -d 3 | grep 192.168.*:.* -> .*:.*\r |
| 92 | \r |
| 93 | /*\r |
| 94 | Example output:\r |
| 95 | Sniffing for IP/MAC addresses\r |
| 96 | C-c for stop\r |
| 97 | \r |
| 98 | 192.168.15.82 = This is your adapter, to find MAC try "ipconfig /all"\r |
| 99 | 192.168.15.65 = 00-00-24-c1-e7-e8\r |
| 100 | 192.168.15.84 = 00-04-e2-cb-bc-6a\r |
| 101 | 192.168.15.77 = Not found. Couldn't broadcast to IP.\r |
| 102 | 192.168.15.80 = Not found. Couldn't broadcast to IP.\r |
| 103 | */\r |
| 104 | \r |
| 105 | //--------------------------------------------------------------------------\r |
| 106 | \r |
| 107 | \r |
| 108 | //Quick active scan for MACs/IPs:\r |
| 109 | echo("Scanning for IP/MAC addresses\nC-c for stop\n");\r |
| 110 | quick_ipmac_scan("192.168.1");\r |
| 111 | \r |
| 112 | /*\r |
| 113 | Example output:\r |
| 114 | Scanning for IP/MAC addresses\r |
| 115 | C-c for stop\r |
| 116 | \r |
| 117 | Rozhrani: 192.168.15.82 --- 0x40003\r |
| 118 |