Advertisement
Guest User

ICMP Chat

a guest
Jun 26th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;######################## icmpchat.inc ########################
  2. include     windows.inc
  3. include     kernel32.inc
  4. include     user32.inc
  5. include     Comctl32.inc
  6. include     shell32.inc
  7. include     ws2_32.inc
  8. include     wsock32.inc
  9.  
  10. includelib  kernel32.lib
  11. includelib  user32.lib
  12. includelib  Comctl32.lib
  13. includelib  shell32.lib
  14. includelib  ws2_32.lib
  15.  
  16. DlgProc         PROTO   :HWND,:UINT,:WPARAM,:LPARAM
  17. sendmsg         PROTO
  18. init_ws         PROTO
  19. icmpchecksum    PROTO   :DWORD,:DWORD
  20. recvmsg         PROTO   :DWORD
  21.  
  22. .const
  23.  
  24. IDD_MAIN        EQU 101
  25. IDC_ENT_TEXT    EQU 102
  26. IDC_CHAT        EQU 103
  27. IDC_IPADDRESS   EQU 104
  28. WM_FINISH       EQU WM_USER+100h
  29.  
  30. WSVERSION       EQU 202h
  31. PORT_ADDR       EQU 0h
  32. SIO_RCVALL      EQU 98000001h
  33.  
  34. icmp    STRUC
  35.     typ     db  ?
  36.     cod     db  ?
  37.     chksum  dw  ?
  38.     ident   dw  ?
  39.     seq     dw  ?
  40.     dat     db  400h dup (?)
  41. icmp ends
  42.  
  43. ;#########################################################################
  44. .data
  45. msglong         db          "Message is long!",0
  46. optval          dd          10000h
  47. pusto           db          " ",0
  48.  
  49. .data?
  50. addr_ip         db          10h dup (?)
  51. compname        db          80h dup (?)
  52. textstr         db          400h dup(?)
  53. buf             db          400h dup (?)
  54. hInstance       dd          ?
  55. ws              WSADATA     <>
  56. saddr           sockaddr_in <>
  57. inaddr          sockaddr_in <>
  58. lsaddr          dd          ?
  59. sock            dd          ?
  60. packet          icmp        <>
  61. sflag           dd          ?
  62. rflag           dd          ?
  63. pThread         dd          ?
  64.  
  65.  
  66.  
  67. ;#########################################################################
  68.  
  69. ;######################## icmpchat.rc ########################
  70.  
  71. #define IDD_MAIN 101
  72. #define IDC_ENT_TEXT 102
  73. #define IDC_CHAT 103
  74. #define IDC_IPADDRESS 104
  75. #define IDC_STC1 105
  76.  
  77. IDD_MAIN DIALOGEX 6,6,195,126
  78. CAPTION "ICMP Chat"
  79. FONT 8,"Tahoma",0,0,0
  80. STYLE 0x10CF0800
  81. BEGIN
  82.   CONTROL "Send",1,"Button",0x50010000,144,108,48,13
  83.   CONTROL "Exit",2,"Button",0x50010000,6,108,48,13
  84.   CONTROL "",IDC_ENT_TEXT,"Edit",0x50010000,9,87,174,12,0x00000200
  85.   CONTROL "",IDC_CHAT,"Edit",0x58010000,9,24,174,57,0x00000200
  86.   CONTROL "",IDC_IPADDRESS,"SysIPAddress32",0x50010000,78,6,105,12
  87.   CONTROL "IP Address:",IDC_STC1,"Static",0x50000000,24,9,45,9
  88. END
  89.  
  90. ;######################## icmpchat.asm ########################
  91.  
  92. .386
  93. .model flat, stdcall  ;32 bit memory model
  94. option casemap :none  ;case sensitive
  95.  
  96. include icmpchat.inc
  97.  
  98. .code
  99.  
  100. start:
  101.     invoke  GetModuleHandle,NULL
  102.     mov     hInstance,eax
  103.     invoke  InitCommonControls
  104.     invoke  DialogBoxParam,hInstance,IDD_MAIN,NULL,addr DlgProc,NULL
  105.     invoke  ExitProcess,0
  106.  
  107. ;########################################################################
  108.  
  109. DlgProc proc    hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
  110.     mov eax,uMsg
  111.     .if eax==WM_INITDIALOG
  112.         ;initialization here
  113.         invoke  init_ws
  114.     .elseif eax==WM_COMMAND
  115.         mov edx,wParam
  116.         movzx eax,dx
  117.         shr edx,16
  118.         .if edx==BN_CLICKED
  119.             .if eax==IDOK
  120.             invoke  GetDlgItemText,hWin, IDC_IPADDRESS, offset addr_ip, 10h
  121.             invoke  GetDlgItemText,hWin, IDC_ENT_TEXT, offset textstr, 400h
  122.             .if eax < 400h
  123.                 invoke  sendmsg
  124.                 invoke  SetDlgItemText, hWin, IDC_ENT_TEXT, offset pusto
  125.                 .if rflag == 0
  126.                     mov     eax, offset recvmsg
  127.                     invoke  CreateThread, NULL, 10240, eax, hWin, NULL, pThread
  128. ;                   invoke  recvmsg, hWin
  129.                 .endif
  130.             .else
  131.                 invoke  MessageBox, hWin, offset msglong, sizeof msglong, MB_OK
  132.             .endif
  133.             .elseif eax==IDCANCEL
  134.                 invoke  SendMessage,hWin,WM_CLOSE,NULL,NULL
  135.             .endif
  136.         .endif
  137.     .elseif eax==WM_FINISH
  138.         lea     eax, DWORD PTR [buf+1ch]
  139.         invoke  SetDlgItemText, hWin, IDC_CHAT, eax
  140. ;       invoke MessageBox,NULL,ADDR icmpa,ADDR icmpa,MB_OK
  141.     .elseif eax==WM_CLOSE
  142.         invoke  EndDialog,hWin,0
  143.         invoke  closesocket,sock
  144.         invoke  WSACleanup
  145.     .else
  146.         mov eax,FALSE
  147.         ret
  148.     .endif
  149.     mov eax,TRUE
  150.     ret
  151. DlgProc endp
  152.  
  153. init_ws proc
  154.     invoke  gethostname, offset compname, sizeof compname
  155.     invoke  WSAStartup, WSVERSION, offset ws
  156.     invoke  socket, AF_INET, SOCK_RAW, IPPROTO_ICMP
  157.     mov     sock, eax
  158.     ret
  159. init_ws endp
  160.  
  161. sendmsg proc
  162.     invoke  inet_addr, offset addr_ip
  163.     mov     saddr.sin_addr,eax
  164.     invoke  htons, PORT_ADDR
  165.     mov     saddr.sin_port,ax
  166.     mov     saddr.sin_family,AF_INET
  167.     invoke  setsockopt, sock, SOL_SOCKET, SO_RCVTIMEO, optval, sizeof optval
  168.     mov     packet.typ, 8h
  169.     mov     packet.chksum, 0h
  170.     mov     packet.ident, 6863h
  171.     invoke  lstrcpy, offset packet.dat, offset textstr
  172.     invoke  icmpchecksum, offset packet, sizeof packet
  173.     mov     packet.chksum, ax
  174.     invoke  sendto, sock, offset packet, sizeof packet, 0, offset saddr, sizeof saddr
  175.     ret
  176. sendmsg endp
  177.  
  178. recvmsg proc hWin:DWORD
  179. LOCAL sock2:DWORD
  180.     mov     rflag, 1
  181.     invoke  socket, AF_INET, SOCK_RAW, IPPROTO_ICMP
  182.     mov     sock2, eax
  183.     invoke  gethostbyname, offset compname
  184.     mov     eax, DWORD PTR [eax+1Ch]
  185.     mov     inaddr.sin_addr,eax
  186.     mov     inaddr.sin_family,AF_INET
  187.     invoke  bind, sock2, offset inaddr, sizeof inaddr
  188.     mov     lsaddr, sizeof inaddr
  189.     mov     sflag, TRUE
  190.     invoke  ioctlsocket, sock2, SIO_RCVALL,offset sflag
  191. nextpacket:
  192.     invoke  recvfrom,sock2, offset buf, sizeof buf, 0, ADDR inaddr, ADDR lsaddr
  193.     mov     eax, DWORD PTR [buf+0Ch] ; source address
  194.     .if saddr.sin_addr != eax
  195.         jmp fchk
  196.     .endif
  197.     mov     al, BYTE PTR [buf+14h] ; protocol type
  198.     .if al != 8h
  199.         jmp fchk
  200.     .endif
  201.     mov     ax, WORD PTR [buf+18h] ; ident
  202.     .if ax != 6863h
  203.         jmp fchk
  204.     .endif
  205.     lea     eax, DWORD PTR [buf+1ch] ; text
  206.     invoke  PostMessage, hWin, WM_FINISH,NULL, NULL
  207. fchk:
  208.     jmp     nextpacket
  209.     ret
  210. recvmsg endp
  211.  
  212. icmpchecksum proc icmppacket:DWORD, psize:DWORD
  213.     mov     edi, icmppacket
  214.     mov     ax, WORD PTR [edi]
  215.     xchg    al, ah
  216.     mov     ecx, 2h
  217. nextword:  
  218.     mov     bx, WORD PTR [edi+ecx]
  219.     xchg    bl, bh
  220.     add     ax, bx
  221.     jnc     noverflow
  222.     inc     ax
  223. noverflow:
  224.     add     ecx, 2h
  225.     .if ecx >= psize
  226.         jmp endchk
  227.     .endif
  228.     jmp nextword
  229. endchk:
  230.     not     ax
  231.     xchg    al, ah
  232.     ret
  233. icmpchecksum endp
  234.  
  235. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement