Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Last active January 26, 2016 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterHaslem/a6dced9d64435da9e77f to your computer and use it in GitHub Desktop.
Save DexterHaslem/a6dced9d64435da9e77f to your computer and use it in GitHub Desktop.
bastard operator from hell
;; bofh.asm fasm 1.71
;; dexter.haslem@gmail.com
;; example socket program that connects to BOFH quote generator and prints it
format PE console 4.0
entry start
include 'win32ax.inc'
section '.rdata' data readable
err_cant_connect db 'failed to connect to host', 10, 0
err_init db 'failed to init winsock', 10, 0
err_socket db 'failed to create socket', 10, 0
err_read db 'failed to read data from socket', 10,0
szHost db 'towel.blinkenlights.nl',0
msg_received db 'received msg from server:', 10, 0
iPort equ 666d
buffsize equ 2048
section '.data' data readable writeable
wsaData WSADATA
sock dd 0
sin_addr dd ?
sin_port dd ?
saddr sockaddr_in
buffer rb buffsize
section '.code' code readable executable
start:
invoke WSAStartup, 0202h, wsaData
.if eax <> 0
invoke printf, err_init
jmp .cleanup
.endif
invoke socket, AF_INET, SOCK_STREAM, 0
.if eax = -1
invoke printf, err_socket
jmp .cleanup
.endif
mov [sock], eax
mov [saddr.sin_family], AF_INET
invoke htons, iPort
mov [saddr.sin_port], ax
; resolve host, assumes no dns failure
invoke gethostbyname, szHost
;; use a data alias here to make it easier
;; to dig out h_addr_list instead of hardcoded offset into struct
virtual at eax
.host hostent
end virtual
mov eax, [.host.h_addr_list];; [eax+12]
mov eax, [eax] ; first entry
mov eax, [eax] ; first pointer to host
mov [saddr.sin_addr], eax
invoke connect, [sock], saddr, 16
.if eax <> 0
invoke printf, err_cant_connect
jmp .cleanup
.endif
; read the entire msg
invoke recv, [sock], buffer, buffsize, 0
.if eax < 1
invoke printf, err_read
.else
invoke printf, msg_received
invoke printf, buffer
.endif
.cleanup:
invoke closesocket, [sock]
invoke WSACleanup
invoke ExitProcess, 0
section '.idata' import data readable writeable
library kernel,'KERNEL32.DLL',\
winsock,'WSOCK32.DLL', \
msvcrt, 'msvcrt.dll'
import msvcrt, printf, 'printf'
import kernel,\
ExitProcess,'ExitProcess',\
Sleep,'Sleep'
import winsock,\
WSAStartup,'WSAStartup',\
WSACleanup,'WSACleanup',\
socket,'socket',\
connect,'connect',\
inet_addr,'inet_addr',\
htons,'htons',\
closesocket,'closesocket',\
gethostbyname,'gethostbyname', \
recv, 'recv'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment