Created
March 15, 2016 00:04
-
-
Save engelmarkus/e0d5a05b965679073d5f to your computer and use it in GitHub Desktop.
Sending an ARP packet using x86 assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Just for fun. | |
; Compile with | |
; nasm -f elf32 -o arp.o arp.asm | |
; gcc -m32 -o arp arp.o | |
; Run it | |
; sudo ./arp | |
BITS 32 | |
SEGMENT .data | |
struc Packet | |
destMac: resb 6 | |
srcMac: resb 6 | |
type: resw 1 | |
ar_hrd: resw 1 | |
ar_pro: resw 1 | |
ar_hln: resb 1 | |
ar_pln: resb 1 | |
ar_op: resw 1 | |
arp_sha: resb 6 | |
arp_spa: resb 4 | |
arp_tha: resb 6 | |
arp_tpa: resb 4 | |
endstruc | |
struc Sockaddr_ll | |
sll_family: resw 1 | |
sll_protocol: resw 1 | |
sll_ifindex: resd 1 | |
sll_hatype: resw 1 | |
sll_pkttype: resb 1 | |
sll_halen: resb 1 | |
sll_addr: resb 8 | |
endstruc | |
AF_PACKET equ 17 | |
SOCK_RAW equ 3 | |
ETH_P_ALL equ 0x0003 | |
ETH_ALEN equ 6 | |
request: | |
istruc Packet | |
at destMac, db 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF | |
; source mac | |
at srcMac, db 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 | |
at type, dw 0x0608 | |
at ar_hrd, dw 0x0100 | |
at ar_pro, dw 0x0008 | |
at ar_hln, db 6 | |
at ar_pln, db 4 | |
at ar_op, dw 0x0100 | |
; source mac and ip | |
at arp_sha, db 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 | |
at arp_spa, db 192, 168, 178, 32 | |
at arp_tha, db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
; target ip | |
at arp_tpa, db 192, 168, 178, 1 | |
iend | |
requestLength equ $ - request | |
sockaddr: | |
istruc Sockaddr_ll | |
at sll_family, dw AF_PACKET | |
at sll_protocol, dw 0 | |
; network device for sending | |
at sll_ifindex, dd 2 | |
at sll_pkttype, db 0 | |
at sll_halen, db ETH_ALEN | |
at sll_addr, db 0, 0, 0, 0, 0, 0, 0, 0 | |
iend | |
addrLength equ $ - sockaddr | |
SEGMENT .bss | |
sd resd 1 | |
SEGMENT .rodata | |
openingSocket db "Trying to create socket", 0 | |
sendPacket db "Sending ARP packet", 0 | |
SEGMENT .text | |
EXTERN socket | |
EXTERN perror | |
EXTERN sendto | |
EXTERN close | |
GLOBAL main | |
main: | |
enter 0, 0 | |
push ETH_P_ALL | |
push SOCK_RAW | |
push AF_PACKET | |
call socket | |
mov [sd], eax | |
add esp, 3 * 4 | |
push openingSocket | |
call perror | |
add esp, 1 * 4 | |
push addrLength | |
push sockaddr | |
push 0 | |
push requestLength | |
push request | |
push dword [sd] | |
call sendto | |
add esp, 6 * 4 | |
push sendPacket | |
call perror | |
add esp, 1 * 4 | |
push dword [sd] | |
call close | |
add esp, 1 * 4 | |
leave | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment