|
| 1 | +/** |
| 2 | + Copyright © 2019 Odzhan. All Rights Reserved. |
| 3 | +
|
| 4 | + Redistribution and use in source and binary forms, with or without |
| 5 | + modification, are permitted provided that the following conditions are |
| 6 | + met: |
| 7 | +
|
| 8 | + 1. Redistributions of source code must retain the above copyright |
| 9 | + notice, this list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | + 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in the |
| 13 | + documentation and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | + 3. The name of the author may not be used to endorse or promote products |
| 16 | + derived from this software without specific prior written permission. |
| 17 | +
|
| 18 | + THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS OR |
| 19 | + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 22 | + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 27 | + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + POSSIBILITY OF SUCH DAMAGE. */ |
| 29 | + |
| 30 | +// https://gnutls.org/manual/html_node/How-to-use-GnuTLS-in-applications.html#How-to-use-GnuTLS-in-applications |
| 31 | + |
| 32 | +#define _GNU_SOURCE |
| 33 | + |
| 34 | +#include <stdio.h> |
| 35 | +#include <stdlib.h> |
| 36 | +#include <stdint.h> |
| 37 | +#include <string.h> |
| 38 | + |
| 39 | +#include <signal.h> |
| 40 | +#include <sys/epoll.h> |
| 41 | +#include <errno.h> |
| 42 | +#include <unistd.h> |
| 43 | +#include <netinet/in.h> |
| 44 | +#include <arpa/inet.h> |
| 45 | +#include <sys/types.h> |
| 46 | +#include <sys/socket.h> |
| 47 | +#include <dirent.h> |
| 48 | +#include <glob.h> |
| 49 | + |
| 50 | +#include <link.h> |
| 51 | +#include <elf.h> |
| 52 | +#include <fcntl.h> |
| 53 | +#include <dlfcn.h> |
| 54 | +#include <sys/mman.h> |
| 55 | + |
| 56 | +#include <gnutls/gnutls.h> |
| 57 | + |
| 58 | +// system calls required to query base address of host process |
| 59 | +long _open(const char *, unsigned long, long); |
| 60 | +int _read(long, char *, unsigned long); |
| 61 | +int _close(unsigned int); |
| 62 | +void *_mmap(void *, unsigned long, unsigned long, unsigned long, long, unsigned long); |
| 63 | +int _munmap(void *, size_t); |
| 64 | +int _fstat(long, void *); |
| 65 | + |
| 66 | +typedef pid_t (*fork_t)(void); |
| 67 | +typedef int (*dup2_t)(int oldfd, int newfd); |
| 68 | +typedef int (*execve_t)(const char *filename, char *const argv[], char *const envp[]); |
| 69 | +typedef int (*pipe_t)(int pipefd[2]); |
| 70 | +typedef int (*open_t)(const char *pathname, int flags); |
| 71 | +typedef ssize_t (*write_t)(int fd, const void *buf, size_t count); |
| 72 | +typedef ssize_t (*read_t)(int fd, void *buf, size_t count); |
| 73 | +typedef int (*close_t)(int fd); |
| 74 | +typedef void *(*malloc_t)(size_t size); |
| 75 | +typedef void (*free_t)(void *ptr); |
| 76 | +typedef int (*kill_t)(pid_t pid, int sig); |
| 77 | + |
| 78 | +typedef int (*globfunc_t)(const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob); |
| 79 | +typedef void (*globfree_t)(glob_t *pglob); |
| 80 | + |
| 81 | +typedef int (*dlinfo_t)(void *handle, int request, void *info); |
| 82 | +typedef void *(*dlopen_t)(const char *filename, int flag); |
| 83 | +typedef void *(*dlsym_t)(void *handle, const char *symbol); |
| 84 | + |
| 85 | +typedef int (*connect_t)(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
| 86 | +typedef int (*socket_t)(int domain, int type, int protocol); |
| 87 | +typedef in_addr_t (*inet_addr_t)(const char *cp); |
| 88 | +typedef uint16_t (*htons_t)(uint16_t hostshort); |
| 89 | +typedef ssize_t (*send_t)(int sockfd, const void *buf, size_t len, int flags); |
| 90 | +typedef ssize_t (*recv_t)(int sockfd, void *buf, size_t len, int flags); |
| 91 | +typedef int (*shutdown_t)(int socket, int how); |
| 92 | + |
| 93 | +typedef int (*epoll_create1_t)(int flags); |
| 94 | +typedef int (*epoll_ctl_t)(int epfd, int op, int fd, struct epoll_event *event); |
| 95 | +typedef int (*epoll_wait_t)(int epfd, struct epoll_event *events, int maxevents, int timeout); |
| 96 | + |
| 97 | +ssize_t _tls_data_push_cb(gnutls_transport_ptr_t ptr, const void *in, size_t inlen); |
| 98 | +ssize_t _tls_data_pull_cb(gnutls_transport_ptr_t ptr, void *out, size_t outlen); |
| 99 | + |
| 100 | +typedef int (*gnutls_certificate_allocate_credentials_t)(gnutls_certificate_credentials_t * res); |
| 101 | +typedef int (*gnutls_certificate_set_x509_system_trust_t)(gnutls_certificate_credentials_t cred); |
| 102 | +typedef int (*gnutls_init_t)(gnutls_session_t * session, unsigned int flags); |
| 103 | +typedef int (*gnutls_set_default_priority_t)(gnutls_session_t session); |
| 104 | +typedef int (*gnutls_credentials_set_t)(gnutls_session_t session, gnutls_credentials_type_t type, void * cred); |
| 105 | +typedef int (*gnutls_server_name_set_t)(gnutls_session_t session, gnutls_server_name_type_t type, const void * name, size_t name_length); |
| 106 | +typedef int (*gnutls_priority_init_t)(gnutls_priority_t * priority_cache, const char * priorities, const char ** err_pos); |
| 107 | +typedef int (*gnutls_priority_set_t)(gnutls_session_t session, gnutls_priority_t priority); |
| 108 | +typedef ssize_t (*gnutls_record_recv_t)(gnutls_session_t session, void * data, size_t sizeofdata); |
| 109 | +typedef ssize_t (*gnutls_record_send_t)(gnutls_session_t session, const void * data, size_t sizeofdata); |
| 110 | +typedef int (*gnutls_bye_t)(gnutls_session_t session, gnutls_close_request_t how); |
| 111 | +typedef void (*gnutls_deinit_t)(gnutls_session_t session); |
| 112 | +typedef void (*gnutls_global_deinit_t)(void); |
| 113 | +typedef int (*gnutls_error_is_fatal_t)(int error); |
| 114 | +typedef int (*gnutls_handshake_t)(gnutls_session_t session); |
| 115 | +typedef int (*gnutls_transport_set_int2_t)(gnutls_session_t session, int recv_fd, int send_fd); |
| 116 | +typedef void (*gnutls_transport_set_ptr_t)(gnutls_session_t session, gnutls_transport_ptr_t ptr); |
| 117 | +typedef void (*gnutls_transport_set_push_function_t)(gnutls_session_t session, gnutls_push_func push_func); |
| 118 | +typedef void (*gnutls_transport_set_pull_function_t)(gnutls_session_t session, gnutls_pull_func pull_func); |
| 119 | + |
| 120 | +Elf64_Dyn *elf_get_dyn(void *base, int tag); |
| 121 | + |
| 122 | +typedef struct _data_t { |
| 123 | + int s; // socket file descriptor |
| 124 | + |
| 125 | + union { |
| 126 | + uint64_t hash[64]; |
| 127 | + void *addr[64]; |
| 128 | + struct { |
| 129 | + // gnu c library functions |
| 130 | + pipe_t _pipe; |
| 131 | + fork_t _fork; |
| 132 | + socket_t _socket; |
| 133 | + inet_addr_t _inet_addr; |
| 134 | + htons_t _htons; |
| 135 | + connect_t _connect; |
| 136 | + dup2_t _dup2; |
| 137 | + close_t _close; |
| 138 | + execve_t _execve; |
| 139 | + epoll_create1_t _epoll_create1; |
| 140 | + epoll_ctl_t _epoll_ctl; |
| 141 | + epoll_wait_t _epoll_wait; |
| 142 | + open_t _open; |
| 143 | + write_t _write; |
| 144 | + read_t _read; |
| 145 | + shutdown_t _shutdown; |
| 146 | + kill_t _kill; |
| 147 | + send_t _send; |
| 148 | + recv_t _recv; |
| 149 | + globfunc_t _glob; |
| 150 | + globfree_t _globfree; |
| 151 | + malloc_t _malloc; |
| 152 | + free_t _free; |
| 153 | + |
| 154 | + // gnu dynamic linker functions |
| 155 | + dlsym_t _dlsym; |
| 156 | + dlopen_t _dlopen; |
| 157 | + dlinfo_t _dlinfo; |
| 158 | + |
| 159 | + // gnu tls functions |
| 160 | + gnutls_certificate_allocate_credentials_t _gnutls_certificate_allocate_credentials; |
| 161 | + gnutls_certificate_set_x509_system_trust_t _gnutls_certificate_set_x509_system_trust; |
| 162 | + gnutls_init_t _gnutls_init; |
| 163 | + gnutls_set_default_priority_t _gnutls_set_default_priority; |
| 164 | + gnutls_credentials_set_t _gnutls_credentials_set; |
| 165 | + gnutls_server_name_set_t _gnutls_server_name_set; |
| 166 | + gnutls_priority_init_t _gnutls_priority_init; |
| 167 | + gnutls_priority_set_t _gnutls_priority_set; |
| 168 | + gnutls_record_recv_t _gnutls_record_recv; |
| 169 | + gnutls_record_send_t _gnutls_record_send; |
| 170 | + gnutls_bye_t _gnutls_bye; |
| 171 | + gnutls_deinit_t _gnutls_deinit; |
| 172 | + gnutls_global_deinit_t _gnutls_global_deinit; |
| 173 | + gnutls_error_is_fatal_t _gnutls_error_is_fatal; |
| 174 | + gnutls_handshake_t _gnutls_handshake; |
| 175 | + gnutls_transport_set_ptr_t _gnutls_transport_set_ptr; |
| 176 | + gnutls_transport_set_push_function_t _gnutls_transport_set_push_function; |
| 177 | + gnutls_transport_set_pull_function_t _gnutls_transport_set_pull_function; |
| 178 | + gnutls_transport_set_int2_t _gnutls_transport_set_int2; |
| 179 | + }; |
| 180 | + } api; |
| 181 | +} data_t; |
| 182 | + |
| 183 | +int init_ld(data_t *ds); |
| 184 | +int init_libc(data_t *ds); |
| 185 | +int init_gnutls(data_t *ds); |
| 186 | + |
| 187 | +void *get_proc_address(void *module, const char *name); |
| 188 | + |
| 189 | +void *get_proc_address2(void *module, uint32_t hash); // using base address |
| 190 | +void *get_proc_address3(const char *path, uint32_t hash); // using file path |
| 191 | + |
| 192 | +void *get_module_handle(const char *module); |
| 193 | +void *load_module(data_t *ds, const char *path, const char *name); |
| 194 | + |
| 195 | +void *get_module_handle1(const char *module); |
| 196 | +void *get_module_handle2(const char *module); |
| 197 | +void *get_base(void); |
| 198 | + |
| 199 | +uint32_t gnu_hash(const uint8_t *name); |
0 commit comments