File: aim.py

package info (click to toggle)
python-dpkt 1.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 920 kB
  • sloc: python: 10,440; makefile: 144
file content (128 lines) | stat: -rw-r--r-- 4,660 bytes parent folder | download | duplicates (2)
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
# $Id: aim.py 23 2006-11-08 15:45:33Z dugsong $
# -*- coding: utf-8 -*-

"""AOL Instant Messenger."""
from __future__ import absolute_import

import struct

from . import dpkt

# OSCAR: http://iserverd1.khstu.ru/oscar/


class FLAP(dpkt.Packet):
    """Frame Layer Protocol.

    See more about the FLAP on \
    https://en.wikipedia.org/wiki/OSCAR_protocol#FLAP_header

    Attributes:
        __hdr__: Header fields of FLAP.
        data: Message data.
    """
    
    __hdr__ = (
        ('ast', 'B', 0x2a),  # '*'
        ('type', 'B', 0),
        ('seq', 'H', 0),
        ('len', 'H', 0)
    )

    def unpack(self, buf):
        dpkt.Packet.unpack(self, buf)
        if self.ast != 0x2a:
            raise dpkt.UnpackError('invalid FLAP header')
        if len(self.data) < self.len:
            raise dpkt.NeedData('%d left, %d needed' % (len(self.data), self.len))


class SNAC(dpkt.Packet):
    """Simple Network Atomic Communication.

    See more about the SNAC on \
    https://en.wikipedia.org/wiki/OSCAR_protocol#SNAC_data

    Attributes:
        __hdr__: Header fields of SNAC.
    """
    
    __hdr__ = (
        ('family', 'H', 0),
        ('subtype', 'H', 0),
        ('flags', 'H', 0),
        ('reqid', 'I', 0)
    )


def tlv(buf):
    n = 4
    try:
        t, l = struct.unpack('>HH', buf[:n])
    except struct.error:
        raise dpkt.UnpackError('invalid type, length fields')
    v = buf[n:n + l]
    if len(v) < l:
        raise dpkt.NeedData('%d left, %d needed' % (len(v), l))
    buf = buf[n + l:]
    return t, l, v, buf

# TOC 1.0: http://jamwt.com/Py-TOC/PROTOCOL

# TOC 2.0: http://www.firestuff.org/projects/firetalk/doc/toc2.txt

def testAIM():
    testdata = b'*\x02\xac\xf3\x00\x81\x00\x03\x00\x0b\x00\x00\xfaEUd\x0eusrnameremoved\x00\x00\x00\n\x00\x01\x00\x02\x12\x90\x00D\x00\x01\x00\x00\x03\x00\x04X\x90T6\x00E\x00\x04\x00\x00\x0f\x93\x00!\x00\x08\x00\x85\x00}\x00}\x00\x00\x00A\x00\x01\x00\x007\x00\x04\x00\x00\x00\x00\x00\r\x00\x00\x00\x19\x00\x00\x00\x1d\x00$\x00\x00\x00\x05\x02\x01\xd2\x04r\x00\x01\x00\x05\x02\x01\xd2\x04r\x00\x03\x00\x05+\x00\x00*\xcc\x00\x81\x00\x05+\x00\x00\x13\xf1'

    flap = FLAP(testdata)
    assert flap.ast == 0x2a
    assert flap.type == 0x02
    assert flap.seq == 44275
    assert flap.len == 129
    assert flap.data == b'\x00\x03\x00\x0b\x00\x00\xfaEUd\x0eusrnameremoved\x00\x00\x00\n\x00\x01\x00\x02\x12\x90\x00D\x00\x01\x00\x00\x03\x00\x04X\x90T6\x00E\x00\x04\x00\x00\x0f\x93\x00!\x00\x08\x00\x85\x00}\x00}\x00\x00\x00A\x00\x01\x00\x007\x00\x04\x00\x00\x00\x00\x00\r\x00\x00\x00\x19\x00\x00\x00\x1d\x00$\x00\x00\x00\x05\x02\x01\xd2\x04r\x00\x01\x00\x05\x02\x01\xd2\x04r\x00\x03\x00\x05+\x00\x00*\xcc\x00\x81\x00\x05+\x00\x00\x13\xf1'

    snac = SNAC(flap.data)
    assert snac.family == 3
    assert snac.subtype == 11
    assert snac.flags == 0
    assert snac.reqid == 0xfa455564
    assert snac.data == b'\x0eusrnameremoved\x00\x00\x00\n\x00\x01\x00\x02\x12\x90\x00D\x00\x01\x00\x00\x03\x00\x04X\x90T6\x00E\x00\x04\x00\x00\x0f\x93\x00!\x00\x08\x00\x85\x00}\x00}\x00\x00\x00A\x00\x01\x00\x007\x00\x04\x00\x00\x00\x00\x00\r\x00\x00\x00\x19\x00\x00\x00\x1d\x00$\x00\x00\x00\x05\x02\x01\xd2\x04r\x00\x01\x00\x05\x02\x01\xd2\x04r\x00\x03\x00\x05+\x00\x00*\xcc\x00\x81\x00\x05+\x00\x00\x13\xf1'

    #skip over the buddyname and TLV count in Oncoming Buddy message
    tlvdata = snac.data[19:]

    tlvCount = 0
    while tlvdata:
        t, l, v, tlvdata = tlv(tlvdata)
        tlvCount += 1
        if tlvCount == 1:
            # just check function return for first TLV
            assert t == 0x01
            assert l == 2
            assert v == b'\x12\x90'
            assert tlvdata == b'\x00D\x00\x01\x00\x00\x03\x00\x04X\x90T6\x00E\x00\x04\x00\x00\x0f\x93\x00!\x00\x08\x00\x85\x00}\x00}\x00\x00\x00A\x00\x01\x00\x007\x00\x04\x00\x00\x00\x00\x00\r\x00\x00\x00\x19\x00\x00\x00\x1d\x00$\x00\x00\x00\x05\x02\x01\xd2\x04r\x00\x01\x00\x05\x02\x01\xd2\x04r\x00\x03\x00\x05+\x00\x00*\xcc\x00\x81\x00\x05+\x00\x00\x13\xf1'

    # make sure we extracted 10 TLVs
    assert tlvCount == 10


def testExceptions():
    testdata = b'xxxxxx'
    try:
        flap = FLAP(testdata)
    except dpkt.UnpackError as e:
        assert str(e) == 'invalid FLAP header'
    testdata = b'*\x02\x12\x34\x00\xff'
    try:
        flap = FLAP(testdata)
    except dpkt.NeedData as e:
        assert str(e) == '0 left, 255 needed'
    try:
        t, l, v, _ = tlv(b'x')
    except dpkt.UnpackError as e:
        assert str(e) == 'invalid type, length fields'

    try:
        t, l, v, _ = tlv(b'\x00\x01\x00\xff')
    except dpkt.NeedData as e:
        assert str(e) == '0 left, 255 needed'