将c枚举位域转换为python

2024-05-18 18:57:38 发布

您现在位置:Python中文网/ 问答频道 /正文

查看kismet在packet\u ieee80211.h中的源代码是部分

enum crypt_type {
    crypt_none = 0,
crypt_unknown = 1,
crypt_wep = (1 << 1),
crypt_layer3 = (1 << 2),
// Derived from WPA headers
crypt_wep40 = (1 << 3),
crypt_wep104 = (1 << 4),
crypt_tkip = (1 << 5),
crypt_wpa = (1 << 6),
crypt_psk = (1 << 7),
crypt_aes_ocb = (1 << 8),
crypt_aes_ccm = (1 << 9),
//WPA Migration Mode
crypt_wpa_migmode = (1 << 19),
// Derived from data traffic
crypt_leap = (1 << 10),
crypt_ttls = (1 << 11),
crypt_tls = (1 << 12),
crypt_peap = (1 << 13),
crypt_isakmp = (1 << 14),
    crypt_pptp = (1 << 15),
crypt_fortress = (1 << 16),
crypt_keyguard = (1 << 17),
crypt_unknown_nonwep = (1 << 18),
};

我理解这是一种转移,但仅此而已。假设我有int706,我如何将这个数字分解成上面定义的密码集,也就是说,我如何提取使用的密码,给706特别移植到python

谢谢


Tags: fromnone密码源代码packettypeenumunknown
2条回答

因此,您需要理解的是,这个枚举定义了一系列bitmasks。在本例中,这些枚举值(二进制)中的每一个都只包含一个1。例如

crypt_wep = (1 << 1) = 0b10
crypt_wpa = (1 << 6) = 0b1000000

等等。使用bitshift运算符只是表示的一种简单方法,我希望第n+1个二进制数字作为标志'

这使我们可以按位or将这些值组合在一起,得到一个幻数,它将这些加密值的组合唯一地描述为位标志。为了测试一个幻数是否包含一个值,我们可以简单地按位and它与我们想要测试的值

^{pr2}$

当且仅当magic_number包含这些位标志时,has_wep和{}将是{}。在

让我们看一下数字706,它在二进制中是0b1011000010。我们可以看到,它一定是用crypt_wepcrypt_wpacrypt_pskcrypt_aes_ccm构建的,因为为这些值设置了正确的位。在

那么如何移植到python呢?Python有{A2},就像C/C++一样,如果你在Python 3.4或更高版本中。因此,您只需在python中创建相同的enum表,并应用相同的位测试来确定您的幻数表示什么。如果您使用的是没有枚举的python版本,您可以简单地定义一个包含一些静态常量的类来获得相同的效果(并构建一个方法来测试幻数包含的密码集)。这样的类可以是这样的:

class CryptKeys(object):
    crypt_masks = {
                    'crypt_unknown':1,
                     ....
                    'crypt_unknown_nonwep': (1 << 18)
                   }
    @classmethod
    def find_crypts(cls, magic_number):
        if magic_number == 0:
            return ['crypt_none']
        else:
            return [name for name, mask in cls.crypt_masks.items() if magic_number & mask == mask]

aruistante的答案很好,我只想扩展一下这个答案,如果您必须使用3.4版本之前的Python,就像PyPI上的does exist a backport

from enum import IntEnum

class Crypt(IntEnum):
    none = 0
    unknown = 1
    wep = (1 << 1)
    layer3 = (1 << 2)
    # Derived from WPA headers
    wep40 = (1 << 3)
    wep104 = (1 << 4)
    tkip = (1 << 5)
    wpa = (1 << 6)
    psk = (1 << 7)
    aes_ocb = (1 << 8)
    aes_ccm = (1 << 9)
    # WPA Migration Mode
    wpa_migmode = (1 << 19)
    # Derived from data traffic
    leap = (1 << 10)
    ttls = (1 << 11)
    tls = (1 << 12)
    peap = (1 << 13)
    isakmp = (1 << 14)
    pptp = (1 << 15)
    fortress = (1 << 16)
    keyguard = (1 << 17)
    unknown_nonwep = (1 << 18)

    @classmethod
    def find_crypts(cls, magic_number):
        crypts = []
        for mask in cls:
            if magic_number & mask == mask:
                crypts.append(mask)
        if len(crypts) > 1:
            # remove false positive of none
            crypts = crypts[1:]
        return crypts

print Crypt.find_crypts(0)
[<Crypt.none: 0>]
print Crypt.find_crypts(706)
[<Crypt.wep: 2>, <Crypt.wpa: 64>, <Crypt.psk: 128>, <Crypt.aes_ccm: 512>]

相关问题 更多 >

    热门问题