栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

x32 段描述符解析及转换脚本

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

x32 段描述符解析及转换脚本

友链

转换代码: htmL+js


















解析脚本代码: python
用法;

python 1.py 0x7c0001ff 0x00409800
# https://i.imgur.com/QraS1z5.png
# 低0x7c0001ff,高32 位是0x00409800

"""
# https://i.imgur.com/QraS1z5.png
# 低0x7c0001ff,高32 位是0x00409800

"""
高32bit

31~24
段基地址 31~24

23
G

22
D/B

21
L

20
AVL

19~16
段界限

15
P

14~13
DPL

12
S

11~8
TYPE

7~0
段基址 23~16

低32bit

31~16
段基地址 15~0

15~0
段界限 15~0
"""

"""
低0x7c0001ff,高32 位是0x00409800
先将16进制转换成2进制
"""
import sys

hex_string_to_int = {
    '0': 0,
    '1': 1,
    '2': 2,
    '3': 3,
    '4': 4,
    '5': 5,
    '6': 6,
    '7': 7,
    '8': 8,
    '9': 9,
    'a': 10,
    'b': 11,
    'c': 12,
    'd': 13,
    'e': 14,
    'f': 15
}
bin_to_hex = {
    "0000": '0',
    "0001": '1',
    "0010": '2',
    "0011": '3',
    "0100": '4',
    "0101": '5',
    "0110": '6',
    "0111": '7',
    "1000": '8',
    "1001": '9',
    "1010": 'A',
    "1011": 'B',
    "1100": 'C',
    "1101": 'D',
    "1110": 'E',
    "1111": 'F'
}
low_hex_presentation = sys.argv[1]
high_hex_presentation = sys.argv[2]


def hex_to_binary(hex_string):
    _binary_value = ""
    hex_string = hex_string.replace("0x", "")
    for c in hex_string:
        _binary_value += "{:04b}".format(hex_string_to_int[c])
    return _binary_value


# 先处理低32bit
binary_value = hex_to_binary(low_hex_presentation)

segment_edge_15_0 = binary_value[16:]
segment_base_addr_15_0 = binary_value[:16]

# 处理高32bit
binary_value = hex_to_binary(high_hex_presentation)

segment_base_addr_23_16 = binary_value[24:]
_type = binary_value[20:24]

# 解析type:https://i.imgur.com/kOSNpiJ.png
print('TYPE-----------------------------')
# 在初始化的时候,A位总是被至0,因此不用管
# 先看最高bit是0还是1,0表示数据,1表示代码
_type_type = ''
if _type[0:1] is '0':
    _type_type = 'data'
else:
    _type_type = 'code'
print('t' + _type_type + " segment:")
print("ttprivileges:")
if _type_type is 'data':
    print("tttread")
    if _type[1:2] is '1':
        print("tttstack down")
    else:
        print("tttstack up");
    if _type[2:3] is '1':
        print("tttwrite")
else:
    print("tttexecute")
    if _type[1:2] is '0':
        print("tttsame level")
    else:
        print("tttlow to high");
    if _type[2:3] is '1':
        print("ttread")

# 解析描述符类型
print('S--------------------------------')
if binary_value[19:20] is '1':
    print("tsystem segment")
else:
    print("tcode/data segment")

# 解析描述符的特权级  DPL
dpl = binary_value[17:19]
print('DPL------------------------------')
if dpl[0] is '0' and dpl[1] is '0':
    print('tlevel 0')
if dpl[0] is '0' and dpl[1] is '1':
    print('tlevel 1')
if dpl[0] is '1' and dpl[1] is '0':
    print('tlevel 2')
if dpl[0] is '1' and dpl[1] is '1':
    print('tlevel 3')

# 解析段存在位
# 这个位置是用于硬盘虚拟内存,如果P为0,则表示段不存在,此时会抛出一个异常
# 然后处理器就会从硬盘中将该段换回到内存中
print('P--------------------------------')
_p = binary_value[16:17]
if _p is '0':
    print("tsegment not exists")
else:
    print("tsegment exists")

segment_edge_19_16 = binary_value[12:16]

# 解析AVL
print('AVL------------------------------')
print("tnot so important, just set it to " + binary_value[11:12])

# 解析L
print('L--------------------------------')
_l = binary_value[10:11]
print("tthis bit is reserved for 64 bit processor, just set it to " + _l)

# 解析D/B,默认的操作数大小/默认的栈指针大小标志
print('D/B Flag-------------------------')
_d_b = binary_value[9:10]
if _d_b is '0':
    print("tpointer is 16 bit")
else:
    print("tpointer is 32 bit")

# 解析G,粒度,用于解释段界限的含义
print('G--------------------------------')
_g = binary_value[8:9]
if _g is '0':
    print("tbytes")
if _g is '1':
    print("t4*1024 bytes")

segment_base_addr_31_24 = binary_value[0:8]

segment_base_addr = segment_base_addr_31_24 + segment_base_addr_23_16 + segment_base_addr_15_0

segment_base_addr = 
    bin_to_hex[segment_base_addr[0:4]]   + 
    bin_to_hex[segment_base_addr[4:8]]   + 
    bin_to_hex[segment_base_addr[8:12]]  + 
    bin_to_hex[segment_base_addr[12:16]] + 
    bin_to_hex[segment_base_addr[16:20]] + 
    bin_to_hex[segment_base_addr[20:24]] + 
    bin_to_hex[segment_base_addr[24:28]] + 
    bin_to_hex[segment_base_addr[28:]]
print("Segment Base Address-------------")
print("t0x" + segment_base_addr)

segment_edge = segment_edge_19_16 + segment_edge_15_0
segment_edge = 
    bin_to_hex[segment_edge[0:4]]   + 
    bin_to_hex[segment_edge[4:8]]   + 
    bin_to_hex[segment_edge[8:12]]  + 
    bin_to_hex[segment_edge[12:16]] + 
    bin_to_hex[segment_edge[16:20]]

print("Segment Edge---------------------")
print("t0x" + segment_edge)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849246.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号