Code:
' Who's on my network ' ' With a popularity of (uggg) wireless networks, it is sometimes possible for someone else ' with a wireless adaptor to accidently or on purpose get into your own home network. ' ' If you suspect a hack, run this program and it should alert you that someone is there. ' Now what you can do about it....Well, your guess is probably better than mine. ' ' One issue I haven't been able to resolve. ' ' If you have static IP addressing, there shouldn't be a problem but you may run across a ' problem if you have dynamic IP addressing since they can change from time to time. ' ' Written in CC4.04, I have "borrowed" several routines from the forums to hodge-podge this ' program together. I tried to give credit where credit is due but I probably forgot somebody. ' Apologies. ' ' I key'd the detection routine to the MAC address instead of DeviceNames$ since the latter ' sometimes returns a nul string and it's also faster. Mac addresses, on the other hand, are ' always there. ' ' Note: If a device (printer, network PC or other device is off, nothing will be returned ' for that device. ' ' Of course, questions/comments are always welcome. Please post them in the CC forum. DECLARE FUNCTION SendARP LIB "iphlpapi.dll" ALIAS "SendARP" _ (BYVAL DestIP AS DWORD, BYVAL SrcIP AS DWORD, pMacAddr AS DWORD, _ PhyAddrLen AS DWORD) AS DWORD ' DECLARE FUNCTION inet_addr LIB "wsock32.dll" ALIAS "inet_addr" (cp AS ASCIIZ) AS DWORD DECLARE FUNCTION GetMACaddress(ASCIIZ) AS STRING ' ' %no_error = 0 ' ' FUNCTION PBMAIN() ' LOCAL ipLong AS LONG ' LOCAL w,x,y,z AS LONG ' LOCAL flag AS LONG ' LOCAL StringIP AS STRING ' LOCAL sMac AS STRING ' LOCAL tm AS ASCIIZ * 16 ' ' fi$ = "SafeList.txt" ' OPEN fi$ FOR BINARY AS #1 ' Just to make sure the file CLOSE #1 ' exists ' f$ = "###" ' f1$ = "###,###,###,###" ' COLOR 14,1 ' CLS ' ' FOR w = 192 TO 192 ' FOR x = 168 TO 168 ' FOR y = 1 TO 1 ' FOR z = 100 TO 125 '<--Starting address of my home network ' REM ******************************* ' REM * Build the string IP address * ' REM ******************************* ' ' StringIP = USING$(f$,w) + "." ' StringIP = StringIP + USING$(f$,x) + "." ' StringIP = StringIP + USING$(f$,y) + "." ' StringIP = StringIP + USING$(f$,z) ' StringIP = REMOVE$(StringIP," ") ' ' REM ******************************************************************** REM * Convert StringIP to a long integer * REM * Eros Olmi * REM * http://www.powerbasic.com/support/pbforums/showthread.php?t=38093* REM ******************************************************************** ' IpLong = ((VAL(PARSE$(StringIP, ".", 4)) * _ ' 256 + VAL(PARSE$(StringIP, ".", 3))) * _ ' 256 + VAL(PARSE$(StringIP, ".", 2))) * _ ' 256 + VAL(PARSE$(StringIP, ".", 1)) ' ' REM ********************************************************************* REM * Get the MAC address of the attached device * REM * John McWilliams * REM * http://www.powerbasic.com/support/pbforums/showthread.php?t=24169 * REM ********************************************************************* ' tm = StringIP ' sMAC = GetMACaddress(tm) '<== What's the MAC address ' of this device? REM ********************************************* ' REM * It's a lot faster getting the device name * ' REM * after making sure there's a Mac Address * ' REM * attached it it. * ' REM ********************************************* ' ' IF sMac <> "" THEN '<: HOST NAME (ipLong) TO DeviceName$ ' | END IF '<: ' IF DeviceName$ = "" THEN '<: DeviceName$ = "Unknown Device" ' | END IF '<: ' PRINT;StringIP ; TAB(20) ; USING$(f1$,iplong) ; _ ' TAB(40);DeviceName$;TAB(60);sMac ' IF LEN(sMac) <> 17 THEN sMac = "" ' ' IF sMac <> "" THEN '<--: flag = 0 ' | OPEN fi$ FOR INPUT AS #1 ' | DO UNTIL EOF(1) '<: | INPUT #1,t1$ , t2$ , t3$ ' | | String IP Address IF t3$ = sMac THEN flag = 1 ' | | LOOP '<: | CLOSE #1 ' | ' | REM ******************************* ' | REM * Match not found. Query user * ' | REM ******************************* ' | IF flag = 0 THEN '<-:| CLS ' || LOCATE 5,30 : PRINT;StringIP ' || LOCATE 7,30 : PRINT;DeviceName$ ' || LOCATE 9,30 : PRINT;sMac ' || LOCATE 13,30 : PRINT;"not found. Accept? (Y/N)" ' || DO '<:|| WHILE INSTAT = 0 : WEND ' ||| an$ = UCASE$(INKEY$) ' ||| IF an$ = "Y" OR an$ = "N" THEN EXIT LOOP ' ||| BEEP ' ||| LOOP '<:|| ' || IF an$ = "Y" THEN '<:|| OPEN fi$ FOR BINARY AS #1 ' ||| SEEK #1,LOF(1) + 1 ' ||| PUT$ #1,StringIP + "," ' ||| PUT$ #1,DeviceName$ + "," ' ||| PUT$ #1,sMac + $CRLF ' ||| CLOSE #1 ' ||| END IF '<:|| END IF '<-:| END IF '<--: ' ' StringIP = "" ' IPlong = 0 ' DeviceName$ = "" ' sMac = "" ' ' IF INKEY$ = CHR$(27) THEN EXIT FUNCTION ' ' NEXT : NEXT : NEXT : NEXT ' BEEP ' WAITKEY$ ' END FUNCTION ' ' ' FUNCTION GetMACaddress(szIP AS ASCIIZ) AS STRING ' ' 'John McWilliams ' 'http://www.powerbasic.com/support/pbforums/showthread.php?t=24169 ' ' Input: IP address from which you want the MAC address ' Output: The MAC address as "00-00-00-00-00-00" on success ' null string on failure. Bad IP addresses returns a ' null string. ' 'Depends: win32API.inc, iphlpapi.inc, wsock32.inc ' LOCAL Result AS LONG LOCAL MACbytes AS LONG DIM ipArray(0) AS DWORD DIM p AS BYTE PTR ' FUNCTION = "" : IF LEN(TRIM$(szIP)) = 0 THEN EXIT FUNCTION ' 'The first six bytes of the array receive the physical address that corresponds to the IP 'address specified by szIP. See "SendARP" in MSDN for more info. MACbytes = 6 'MACbytes is NOT a statement about Apple Macintosh's ' Result = SendARP(inet_addr(szIP), 0, ipArray(0), MACbytes) IF Result = %NO_ERROR THEN 'Success p = VARPTR(ipArray(0)) FUNCTION = HEX$(@p, 2) & "-" & HEX$(@p[1], 2) & "-" & HEX$(@p[2], 2) & "-" & HEX$(@p[3], 2) & _ "-" & HEX$(@p[4], 2) & "-" & HEX$(@p[5], 2) END IF END FUNCTION
Comment