A small application to get the WAN IP adress :
include WanIP.inc
SIZE_OF_BUFFER = 280
.data
szURL db 'http://icanhazip.com',0
FileName db 'wanip.txt',0
.data?
buffer db SIZE_OF_BUFFER dup(?)
BytesRead dd ?
hMem dd ?
.code
start:
invoke GetTempPath,280,ADDR buffer
invoke lstrcat,ADDR buffer,ADDR FileName
xor eax,eax
invoke URLDownloadToFile,eax,ADDR szURL,\
ADDR buffer,eax,eax
invoke ReadFileToMem,ADDR buffer,\
ADDR hMem,ADDR BytesRead
mov eax,hMem
add eax,BytesRead
mov BYTE PTR [eax],0
invoke Sleep,1000
invoke StdOut,hMem
invoke VirtualFree,hMem,0,MEM_RELEASE
invoke ExitProcess,0
END start
Here is another version based on the Internet API functions. The application takes one command line parameter to read the WAN IP :
GetWanIP.exe http://bot.whatismyipaddress.com
Without any command line parameter, the default web site to get the IP information is http://icanhazip.com :
GetWanIP.exe
include GetWanIP.inc
.data
url db 'http://icanhazip.com',0
.code
start:
call main
invoke ExitProcess,0
main PROC uses esi ebx
LOCAL buffer[512]:BYTE
LOCAL buffer2[64]:BYTE
LOCAL App[128]:BYTE
LOCAL hInt:DWORD
LOCAL hUrl:DWORD
LOCAL BuffSize:DWORD
lea esi,buffer
invoke ParseCmdLine,esi
cmp eax,2
jz @f
mov DWORD PTR [esi+4],OFFSET url
@@:
invoke GetModuleFileName,0,ADDR App,128
xor ebx,ebx
invoke InternetOpen,ADDR App,\
INTERNET_OPEN_TYPE_PRECONFIG,\
ebx,ebx,ebx
test eax,eax
jne @f
ret
@@:
mov hInt,eax
invoke InternetOpenUrl,eax,\
DWORD PTR [esi+4],ebx,ebx,ebx,ebx
test eax,eax
jne @f
ret
@@:
mov hUrl,eax
lea esi,buffer2
@@:
invoke InternetReadFile,hUrl,esi,6,\
ADDR BuffSize
mov eax,BuffSize
test eax,eax
jz @f
add ebx,eax
add esi,eax
jmp @b
@@:
lea esi,buffer2
dec esi
@@:
inc esi
cmp BYTE PTR [esi],'.'
jne @b
@@:
inc esi
cmp BYTE PTR [esi],'.'
jne @b
@@:
inc esi
cmp BYTE PTR [esi],'.'
jne @b
inc esi
@@:
inc esi
movzx eax,BYTE PTR [esi]
cmp eax,48
jb @f
cmp eax,57
jna @b
@@:
mov BYTE PTR [esi],0
invoke StdOut,ADDR buffer2
invoke InternetCloseHandle,hUrl
invoke InternetCloseHandle,hInt
ret
main ENDP
END start
What is this site "http://icanhazip.com/"
It returns good result.
Hi Grincheux,
That's a site to retrieve your WAN IP adress. More info :
https://major.io/icanhazip-com-faq/
Here is another version eliminating some unnecessary code:
; Example of retrieving the WAN IP adress from an external source :
;
; GetWanIP.exe http://whatismyip.akamai.com
; https://ipecho.net/plain
;
; If no command line option is specified then the application
; defaults to the address http://icanhazip.com
include GetWanIP.inc
.data
url db 'http://icanhazip.com',0
.code
start:
call main
invoke ExitProcess,0
main PROC uses esi ebx
LOCAL buffer[512]:BYTE
LOCAL buffer2[68]:BYTE
LOCAL App[128]:BYTE
LOCAL hInt:DWORD
LOCAL hUrl:DWORD
LOCAL BuffSize:DWORD
lea esi,buffer
invoke ParseCmdLine,esi
cmp eax,2
jz @f
mov DWORD PTR [esi+4],OFFSET url
@@:
invoke GetModuleFileName,0,ADDR App,128
xor ebx,ebx
invoke InternetOpen,ADDR App,\
INTERNET_OPEN_TYPE_PRECONFIG,\
ebx,ebx,ebx
test eax,eax
jnz @f
ret
@@:
mov hInt,eax
invoke InternetOpenUrl,eax,\
DWORD PTR [esi+4],ebx,ebx,ebx,ebx
test eax,eax
jz _exit
mov hUrl,eax
lea esi,buffer2
@@:
invoke InternetReadFile,hUrl,esi,64,\
ADDR BuffSize
push eax
invoke InternetCloseHandle,hUrl
pop eax
test eax,eax
jz _exit
mov edx,BuffSize
mov ecx,32
mov BYTE PTR [esi+edx],ch
dec esi
@@:
inc esi
movzx eax,BYTE PTR [esi]
test eax,eax
jz @f
cmp BYTE PTR [esi],cl
jnb @b
mov BYTE PTR [esi],cl
jmp @b
@@:
invoke StdOut,ADDR buffer2
_exit:
invoke InternetCloseHandle,hInt
ret
main ENDP
END start