From fe2a0dab79bd4c029c849d9dd18113735a527d19 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Fri, 2 Jun 2023 14:02:15 +1000 Subject: [PATCH] Push me and then just touch me --- .gitignore | 4 ++- main.nasm | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 main.nasm diff --git a/.gitignore b/.gitignore index ba9e2b3..b152b76 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -./main* +main +main_hello +*.o diff --git a/main.nasm b/main.nasm new file mode 100644 index 0000000..ad4032d --- /dev/null +++ b/main.nasm @@ -0,0 +1,97 @@ +; Comments start with a semicolon! +BITS 64 ; +CPU X64 ; + +section .rodata + +sun_path: db "/tmp/.X11-unix/X0", 0 +static sun_path:data + +section .text + +%define AF_UNIX 1 +%define SOCK_STREAM 1 + +%define SYSCALL_WRITE 1 +%define SYSCALL_SOCKET 41 +%define SYSCALL_CONNECT 42 +%define SYSCALL_EXIT 60 + + + +global _start + +; Create a UNIX domain socket and connect to the X11 server. +; @returns the socket file descriptor +x11_connect_to_server: +static x11_connect_to_server:function + push rbp + mov rbp, rsp + + ; Open a Unix socket: socket(2) + mov rax, SYSCALL_SOCKET + mov rdi, AF_UNIX ; Unix socket + mov rsi, SOCK_STREAM ; TCP like + mov rdx, 0 ; Automatic protocol + syscall + + cmp rax, 0 ; Check if we made the socket + jle die + + mov rdi, rax ; Store the socket fs in rdi for the remainder of the function + + sub rsp, 112 ; Store the struct sockaddr_un on the stack + + mov WORD [rsp], AF_UNIX ; Set the sockaddr_un.sun_family to AF_UNIX + ; Fil lsoccaddr_un.sun_path with: "/tmp/.X11-unix/X0" + lea rsi, sun_path + mov r12, rdi ; Save the socket fd from rdi to r12 + lea rdi, [rsp + 2] + cld + mov ecx, 19 + rep movsb ; Copy. + + ; Connect ot the server + mov rax, SYSCALL_CONNECT + mov rdi, r12 + lea rsi, [rsp] + %define SIZEOF_SOCKADDR_UN 2+108 + mov rdx, SIZEOF_SOCKADDR_UN + syscall + + cmp rax, 0 + jne die + + mov rax, rdi + + add rsp, 112 + + pop rbp + ret + +; Send the handshake to the X11 server and read the returned system information +; @param rdi The socket fd +; @returns The window root id (uint32_t) in rax +x11_send_handshake: +static x11_send_handshake:function + push rdp + mov rbp, rsp + + sub rsp, 1<<15 + mov BYTE [rsp + 0], 'l' ; Tell X11 we are little endian + mov WORD [rsp + 2], 11; Set the major version to 11 + + ; Send the + +die: + mov rax, SYSCALL_EXIT + mov rdi, 1 + syscall + +_start: + call x11_connect_to_server + + ; Exit the program + mov rax, SYSCALL_EXIT + mov rdi, 0 + syscall