赞
踩
typedef struct _metadata_t{ uint64_t magic_num; /* The magic number identifying the file as a signed enclave image */ uint64_t version; /* The metadata version */ uint32_t size; /* The size of this structure */ uint32_t tcs_policy; /* TCS management policy */ uint32_t ssa_frame_size; /* The size of SSA frame in page */ uint32_t max_save_buffer_size; /* Max buffer size is 2632 */ uint32_t desired_misc_select; uint32_t tcs_min_pool; /* TCS min pool*/ uint64_t enclave_size; /* enclave virtual size */ sgx_attributes_t attributes; /* XFeatureMask to be set in SECS. */ enclave_css_t enclave_css; /* The enclave signature */ data_directory_t dirs[DIR_NUM]; uint8_t data[18592];}metadata_t;
version代表metadata的版本号,如:2.3/2.1/1.4,为了兼容所有的版本,一个enclave.signed.so里包含有三段metadata,据本人实测发现,这三段metadata仅version域不一样外,其他域完全一样,同时在Ubuntu16.04上安装测试版本的PSW_2.2.100.45311仅支持1.4版本,因此本文所述的sgx_repack_tool仅生成一段version为1.4的metadata。
attributes域的sgx_attributes_t数据结构如表8所示,我们需要将flags添加上SGX_FLAGS_DEBUG 属性,即debug位置1。
表8 sgx_attributes_t数据结构
typedef struct _attributes_t{ uint64_t flags; /* 包含有debug属性的旗标,各bit含义与Enclave Signature的一致 */ uint64_t xfrm;} sgx_attributes_t;/* Enclave Flags Bit Masks */#define SGX_FLAGS_INITTED 0x0000000000000001ULL /* If set, then the enclave is initialized */#define SGX_FLAGS_DEBUG 0x0000000000000002ULL /* If set, then the enclave is debug */#define SGX_FLAGS_MODE64BIT 0x0000000000000004ULL /* If set, then the enclave is 64 bit */#define SGX_FLAGS_PROVISION_KEY 0x0000000000000010ULL /* If set, then the enclave has access to provision key */#define SGX_FLAGS_EINITTOKEN_KEY 0x0000000000000020ULL /* If set, then the enclave has access to EINITTOKEN key */#define SGX_FLAGS_RESERVED (~(SGX_FLAGS_INITTED | SGX_FLAGS_DEBUG | SGX_FLAGS_MODE64BIT | SGX_FLAGS_PROVISION_KEY | SGX_FLAGS_EINITTOKEN_KEY))
表9 enclave_css_t数据结构
typedef struct _enclave_css_t { /* 1808 bytes */ css_header_t header; /* (0) */ css_key_t key; /* (128) */ css_body_t body; /* (900) */ css_buffer_t buffer; /* (1028) */} enclave_css_t;typedef struct _css_buffer_t { /* 780 bytes */ uint8_t reserved[12]; /* (1028) Must be 0 */ uint8_t q1[SE_KEY_SIZE]; /* (1040) Q1 value for RSA Signature Verification */ uint8_t q2[SE_KEY_SIZE]; /* (1424) Q2 value for RSA Signature Verification */} css_buffer_t;其中,q1 = floor(Signature^2 / Modulus);q2 = floor((Signature^3 - q1 * Signature * Modulus) / Modulus);
表9 enclave_css_t数据结构(续)
typedef struct _css_header_t { /* 128 bytes */ uint8_t header[12]; /* (0) must be (06000000E100000000000100H) */ uint32_t type; /* (12) bit 31: 0 = prod, 1 = debug; Bit 30-0: Must be zero */ uint32_t module_vendor; /* (16) Intel=0x8086, ISV=0x0000 */ uint32_t date; /* (20) build date as yyyymmdd */ uint8_t header2[16]; /* (24) must be (01010000600000006000000001000000H) */ uint32_t hw_version; /* (40) For Launch Enclaves: HWVERSION != 0. Others, HWVERSION = 0 */ uint8_t reserved[84]; /* (44) Must be 0 */} css_header_t;typedef struct _css_key_t { /* 772 bytes */ uint8_t modulus[SE_KEY_SIZE]; /* (128) Module Public Key (keylength=3072 bits) */ uint8_t exponent[SE_EXPONENT_SIZE]; /* (512) RSA Exponent = 3 */ uint8_t signature[SE_KEY_SIZE]; /* (516) Signature over Header and Body */} css_key_t;typedef struct _css_body_t { /* 128 bytes */ sgx_misc_select_t misc_select; /* (900) The MISCSELECT that must be set */ sgx_misc_select_t misc_mask; /* (904) Mask of MISCSELECT to enforce */ uint8_t reserved[20]; /* (908) Reserved. Must be 0. */ sgx_attributes_t attributes; /* (928) Enclave Attributes that must be set */ sgx_attributes_t attribute_mask; /* (944) Mask of Attributes to Enforce */ sgx_measurement_t enclave_hash; /* (960) MRENCLAVE - (32 bytes) */ uint8_t reserved2[32]; /* (992) Must be 0 */ uint16_t isv_prod_id; /* (1024) ISV assigned Product ID */ uint16_t isv_svn; /* (1026) ISV assigned SVN */} css_body_t;
enclave_css域代表的是Enclave Signature Structure,其代码形式的数据结构enclave_css_t如表9所示。该数据结构中必须修改body域中的attributes及对应的attribute_mask,将attributes的flags置上SGX_FLAGS_DEBUG,将attribute_mask的flags的 SGX_FLAGS_DEBUG清零;同时需要修改header域的type,将其第31位置1,代表需要debug;再将header域的module_vendor置成0,伪装成非Intel发布。因为修改header和body影响了key域的signature签名,所以须对Enclave Signature Structure进行重签名,操作时将key域的modulus置换成Enclave debug 私钥对应的公钥,再使用私钥对Enclave Signature Structure的header和body域进行签名。因为key域的改变,buffer域的q1和q2也需要根据公式
q1 = floor(Signature^2 / Modulus);
q2 = floor((Signature^3 – q1 * Signature * Modulus) / Modulus);
进行修正 。
至此,将Released Enclave转换成debug版本已经呼之欲出,我们通过编写一个sgx_repack_tool工具将上述的修改操作自动化完成,将enclave_release.signed.so的metadata的相应位域修改后生成enclave_debug.signed.so,这样通过SGX SDK发布的sgx-gdb工具可以对enclave_debug.signed.so进行调试。如图11所示,将随同 SGX SDK一起发布Samplecode的Enclave_private.pem和enclave_release.signed.so文件做为输入,经过sgx_repack_tool 工具转换后生成可debug的enclave_debug.signed.so。
图11 sgx_repack_tool
静态转换法所需的修改点,总结如表11所示。
表11 需要修改的metadata数据域
- End -
精彩推荐
公众号后台回复关键词 “渗透测试” 查看公众号渗透测试历史精选文章合集!公众号后台回复关键词 “漏洞分析” 查看经典漏洞分析合集get新思路!
觉得内容不错就点个“在看”吧!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。