Coverage for /root/GitHubProjects/impacket/impacket/pcapfile.py: 37%

# Coverage for /root/GitHubProjects/impacket/impacket/pcapfile.py : 37%

## 73 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1# Impacket - Collection of Python classes for working with network protocols.

2#

3# SECUREAUTH LABS. Copyright (C) 2018 SecureAuth Corporation. All rights reserved.

4#

5# This software is provided under a slightly modified version

6# of the Apache Software License. See the accompanying LICENSE file

7# for more information.

8#

9

10 from impacket import structure

11

12 O_ETH = 0

13 O_IP = 1

14 O_ARP = 1

15 O_UDP = 2

16 O_TCP = 2

17 O_ICMP = 2

18 O_UDP_DATA = 3

19 O_ICMP_DATA = 3

20

21 MAGIC = '"\xD4\xC3\xB2\xA1'

22

23 class PCapFileHeader(structure.Structure):

24 structure = (

25 ('magic', MAGIC),

26 ('versionMajor', '<H=2'),

27 ('versionMinor', '<H=4'),

28 ('GMT2localCorrection', '<l=0'),

29 ('timeAccuracy', '<L=0'),

30 ('maxLength', '<L=0xffff'),

31 ('linkType', '<L=1'),

32 ('packets','*:=[]'),

33 )

34

35 class PCapFilePacket(structure.Structure):

36 structure = (

37 ('tsec', '<L=0'),

38 ('tmsec', '<L=0'),

39 ('savedLength', '<L-data'),

40 ('realLength', '<L-data'),

41 ('data',':'),

42 )

43

44 def __init__(self, *args, **kargs):

45 structure.Structure.__init__(self, *args, **kargs)

46 self['data'] = b''

47

48 class PcapFile:

49 def __init__(self, fileName = None, mode = 'rb'):

50 if fileName is not None:

51 self.file = open(fileName, mode)

52 self.hdr = None

53 self.wroteHeader = False

54

55 def reset(self):

56 self.hdr = None

57 self.file.seek(0)

58

59 def close(self):

60 self.file.close()

61

62 def fileno(self):

63 return self.file.fileno()

64

65 def setFile(self, file):

66 self.file = file

67

68 def setSnapLen(self, snapLen):

69 self.createHeaderOnce()

70 self.hdr['maxLength'] = snapLen

71

72 def getSnapLen(self):

73 self.readHeaderOnce()

74 return self.hdr['maxLength']

75

76 def setLinkType(self, linkType):

77 self.createHeaderOnce()

78 self.hdr['linkType'] = linkType

79

80 def getLinkType(self):

81 self.readHeaderOnce()

82 return self.hdr['linkType']

83

84 def readHeaderOnce(self):

85 if self.hdr is None:

86 self.hdr = PCapFileHeader.fromFile(self.file)

87

88 def createHeaderOnce(self):

89 if self.hdr is None:

90 self.hdr = PCapFileHeader()

91

92 def writeHeaderOnce(self):

93 if not self.wroteHeader:

94 self.wroteHeader = True

95 self.file.seek(0)

96 self.createHeaderOnce()

97 self.file.write(self.hdr.getData())

98

99 def read(self):

100 self.readHeaderOnce()

101 try:

102 pkt = PCapFilePacket.fromFile(self.file)

103 pkt['data'] = self.file.read(pkt['savedLength'])

104 return pkt

105 except:

106 return None

107

108 def write(self, pkt):

109 self.writeHeaderOnce()

110 self.file.write(str(pkt))

111

112 def packets(self):

113 self.reset()

114 while 1:

115 answer = self.read()

116 if answer is None:

117 break

118 yield answer