Author Topic: Retrieving the WAN IP address  (Read 1363 times)

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2583
Retrieving the WAN IP address
« on: April 30, 2020, 05:13:29 AM »
A small application to get the WAN IP adress :

Code: [Select]
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

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2583
Re: Retrieving the WAN IP address
« Reply #1 on: May 10, 2020, 10:36:31 PM »
Here is another version based on the Internet API functions. The application takes one command line parameter to read the WAN IP :

Code: [Select]
GetWanIP.exe http://bot.whatismyipaddress.com
Without any command line parameter, the default web site to get the IP information is http://icanhazip.com :

Code: [Select]
GetWanIP.exe
Code: [Select]
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

Grincheux

  • Member
  • ***
  • Posts: 330
  • Never be pleased, Always improve
Re: Retrieving the WAN IP address
« Reply #2 on: April 29, 2021, 03:59:43 AM »
What is this site "http://icanhazip.com/"
It returns good result.
Kenavo (Bye)
----------------------
Help me if you can, I'm feeling down...

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2583
Re: Retrieving the WAN IP address
« Reply #3 on: April 29, 2021, 05:56:46 AM »
Hi Grincheux,

That's a site to retrieve your WAN IP adress. More info :

https://major.io/icanhazip-com-faq/