示例#1
0
class TestIpAddress(unittest.TestCase):
    def setUp(self):
        self.ipa = IpAddress("foo")

    def test_trivial(self):
        pass

    def test_parse(self):
        self.assertEqual(self.ipa.parse(b"\x7f\x80\x81\x82"),
                         "127.128.129.130")

    def test_build(self):
        self.assertEqual(self.ipa.build("127.1.2.3"), b"\x7f\x01\x02\x03")

    def test_build_invalid(self):
        self.assertRaises(ValueError, self.ipa.build, "300.1.2.3")
示例#2
0
class TestIpAddress(unittest.TestCase):

    def setUp(self):
        self.ipa = IpAddress("foo")

    def test_trivial(self):
        pass

    def test_parse(self):
        self.assertEqual(self.ipa.parse(six.b("\x7f\x80\x81\x82")),
                         "127.128.129.130")

    def test_build(self):
        self.assertEqual(self.ipa.build("127.1.2.3"),
                         six.b("\x7f\x01\x02\x03"))

    def test_build_invalid(self):
        self.assertRaises(ValueError, self.ipa.build, "300.1.2.3")
示例#3
0
"""
What : Internet Group Management Protocol, Version 2
 How : http://www.ietf.org/rfc/rfc2236.txt
 Who : jesse @ housejunkie . ca
"""

from binascii import unhexlify

from construct import Byte, Enum, Struct, UBInt16
from construct.protocols.layer3.ipv4 import IpAddress

igmp_type = Enum(
    Byte("igmp_type"),
    MEMBERSHIP_QUERY=0x11,
    MEMBERSHIP_REPORT_V1=0x12,
    MEMBERSHIP_REPORT_V2=0x16,
    LEAVE_GROUP=0x17,
)

igmpv2_header = Struct(
    "igmpv2_header",
    igmp_type,
    Byte("max_resp_time"),
    UBInt16("checksum"),
    IpAddress("group_address"),
)

if __name__ == '__main__':
    capture = unhexlify(b"1600FA01EFFFFFFD")
    print(igmpv2_header.parse(capture))
示例#4
0
    netlog.error("SMSG_LOGIN_ERROR {}".format(
        error_codes.get(data.code, "Unknown error")))

    server.close()


protodef = {
    0x7531: (smsg_server_version, Struct("data", ULInt32("hi"),
                                         ULInt32("lo"))),
    0x0063: (smsg_update_host,
             Struct("data", ULInt16("length"),
                    StringZ("host", lambda ctx: ctx.length - 4))),
    0x0069: (smsg_login_data,
             Struct(
                 "data", ULInt16("length"), ULInt32("session1"),
                 ULInt32("account"), ULInt32("session2"), IpAddress("oldip"),
                 StringZ("lastlogin", 24), Padding(2), Gender("gender"),
                 Array(
                     lambda ctx: (ctx.length - 47) / 32,
                     Struct("worlds", IpAddress("address"), ULInt16("port"),
                            StringZ("name", 20), ULInt16("onlineusers"),
                            Padding(4))))),
    0x006a: (smsg_login_error, Struct("data", Byte("code"),
                                      StringZ("date", 20)))
}


def cmsg_server_version_request():
    netlog.info("CMSG_SERVER_VERSION_REQUEST")
    ULInt16("opcode").build_stream(0x7530, server)
示例#5
0
                  ULInt16("job"),
                  Padding(30),
                  BitStruct("coor",
                            BitField("x", 10),
                            BitField("y", 10),
                            Nibble("dir")),
                  Padding(4))),
 0x0091 : (smsg_player_warp,
           Struct("data",
                  StringZ("map", 16),
                  ULInt16("x"),
                  ULInt16("y"))),
 0x0020 : (smsg_ip_response,
           Struct("data",
                  ULInt32("id"),
                  IpAddress("ip"))),
 0x019a : (smsg_ignore, Field("data", 12)),    # pvp-set
 0x0081 : (smsg_connection_problem,
           Struct("data", Byte("code"))),
 0x009a : (smsg_gm_chat,
           Struct("data",
                  ULInt16("length"),
                  StringZ("message", lambda ctx: ctx.length - 4))),
 0x009e : (smsg_item_dropped,
           Struct("data",
                  ULInt32("id"),
                  ULInt16("type"),
                  Byte("identify"),
                  ULInt16("x"),
                  ULInt16("y"),
                  Byte("sub_x"),
示例#6
0
from construct.protocols.layer3.ipv4 import IpAddress, ProtocolEnum, ipv4_header
from construct import *


def checksum(m):
    m = m + bytes((0, ))
    cs = sum(map(lambda i: (m[i] << 8) + m[i + 1], range(0, len(m) - 1, 2)))
    return ((cs >> 16) + (cs & 0xffff)) ^ 0xffff


def IPchecksum(ip):
    ip.header.checksum = 0
    ip.header.checksum = checksum(ipv4_header.build(ip.header))


psh = Struct(None, IpAddress("src"), IpAddress("dst"),
             ProtocolEnum(UBInt16("protocol")), UBInt16("tcplen"))


def TCPchecksum(ip):
    tcp = ip.next
    tcp.header.checksum = 0
    pshbin = psh.build(
        Container(src=ip.header.source,
                  dst=ip.header.destination,
                  protocol=ip.header.protocol,
                  tcplen=ip.header.total_length - ip.header.header_length))
    tcp.header.checksum = checksum(pshbin + tcp_header.build(tcp.header) +
                                   tcp.next)

示例#7
0
 def setUp(self):
     self.ipa = IpAddress("foo")
示例#8
0
            # Probe("debug", show_stream=False, show_stack=False),
            Padding(17),
            Array(
                lambda ctx: (ctx["length"] - 24) / 106,
                Struct("chars",
                       ULInt32("id"), ULInt32("exp"), ULInt32("money"),
                       ULInt32("job"), ULInt32("job_level"), Padding(20),
                       ULInt16("charpoints"), ULInt16("hp"), ULInt16("max_hp"),
                       ULInt16("mp"), ULInt16("max_mp"), ULInt16("speed"),
                       Padding(6), ULInt16("level"), ULInt16("skillpoints"),
                       Padding(12), StringZ("name", 24), Padding(6),
                       Byte("slot"), Padding(1))))),
    0x006c: (smsg_char_login_error, Struct("data", Byte("code"))),
    0x0071: (smsg_char_map_info,
             Struct("data", ULInt32("char_id"), StringZ("map_name", 16),
                    IpAddress("address"), ULInt16("port"))),
    0x0081: (mapserv.smsg_connection_problem, Struct("data", Byte("code"))),
}


def cmsg_char_server_connect(account, session1, session2, proto, gender):
    netlog.info(
        ("CMSG_CHAR_SERVER_CONNECT account={} session1={} "
         "session2={} proto={} gender={}").format(account, session1, session2,
                                                  proto, gender))

    # save session data
    server.account = account
    server.session1 = session1
    server.session2 = session2
    server.gender = gender
示例#9
0
 def setUp(self):
     self.ipa = IpAddress("foo")
示例#10
0
                        ID_WHISPER=0x04,
                        ID_TALK=0x05,
                        ID_BROADCAST=0x06,
                        ID_CHANNEL=0x07,
                        ID_USERFLAGS=0x09,
                        ID_WHISPERSENT=0x0a,
                        ID_CHANNELFULL=0x0d,
                        ID_CHANNELDOESNOTEXIST=0x0e,
                        ID_CHANNELRESTRICTED=0x0f,
                        ID_INFO=0x12,
                        ID_ERROR=0x13,
                        ID_EMOTE=0x17,
                        ID_SYSTEMBLUE=0x18,
                        ID_SYSTEMRED=0x19,
                    ),
                    ULInt32("user_flags"),
                    ULInt32("ping"),
                    IpAddress("ip_address"),
                    ULInt32("account_number"),
                    ULInt32("registration_authority"),
                    CString("username"),
                    CString("text"),
                ),
            })))

rpackets = Struct(
    None, OptionalGreedyRange(rpacket),
    ExprAdapter(OptionalGreedyRange(Byte("tail")),
                encoder=lambda obj, ctx: list(obj),
                decoder=lambda obj, ctx: bytes(obj)))
示例#11
0
     SID_AUTH_INFO=0x50,
     SID_PING=0x25,
     SID_AUTH_CHECK=0x51,
     SID_LOGONRESPONSE2=0x3a,
     SID_ENTERCHAT=0x0a,
     SID_GETCHANNELLIST=0x0b,
     SID_JOINCHANNEL=0x0c,
     SID_CHATCOMMAND=0x0e,
 ), ULInt16("length"),
 Embed(
     Switch(
         None, lambda ctx: ctx.packet_id, {
             "SID_AUTH_INFO":
             Struct(None, ULInt32("protocol_id"), Bytes("platform_id", 4),
                    Bytes("product_id", 4), ULInt32("version_byte"),
                    Bytes("product_language", 4), IpAddress("local_ip"),
                    SLInt32("time_zone"), ULInt32("locale_id"),
                    ULInt32("language_id"), CString("country_abreviation"),
                    CString("country")),
             "SID_PING":
             Struct(
                 None,
                 ULInt32("value"),
             ),
             "SID_AUTH_CHECK":
             Struct(
                 None, ULInt32("client_token"), ULInt32("exe_version"),
                 ULInt32("exe_hash"), ULInt32("number_of_cd_keys"),
                 ULInt32("spawn_cd_key"),
                 Array(
                     lambda ctx: ctx["number_of_cd_keys"],