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# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.

2#

3# This software is provided under under a slightly modified version

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

5# for more information.

6#

7

8 from impacket import structure

9

10 O_ETH = 0

11 O_IP = 1

12 O_ARP = 1

13 O_UDP = 2

14 O_TCP = 2

15 O_ICMP = 2

16 O_UDP_DATA = 3

17 O_ICMP_DATA = 3

18

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

20

21 class PCapFileHeader(structure.Structure):

22 structure = (

23 ('magic', MAGIC),

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

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

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

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

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

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

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

31 )

32

33 class PCapFilePacket(structure.Structure):

34 structure = (

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

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

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

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

39 ('data',':'),

40 )

41

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

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

44 self['data'] = b''

45

46 class PcapFile:

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

48 if fileName is not None:

49 self.file = open(fileName, mode)

50 self.hdr = None

51 self.wroteHeader = False

52

53 def reset(self):

54 self.hdr = None

55 self.file.seek(0)

56

57 def close(self):

58 self.file.close()

59

60 def fileno(self):

61 return self.file.fileno()

62

63 def setFile(self, file):

64 self.file = file

65

66 def setSnapLen(self, snapLen):

67 self.createHeaderOnce()

68 self.hdr['maxLength'] = snapLen

69

70 def getSnapLen(self):

71 self.readHeaderOnce()

72 return self.hdr['maxLength']

73

74 def setLinkType(self, linkType):

75 self.createHeaderOnce()

76 self.hdr['linkType'] = linkType

77

78 def getLinkType(self):

79 self.readHeaderOnce()

80 return self.hdr['linkType']

81

82 def readHeaderOnce(self):

83 if self.hdr is None:

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

85

86 def createHeaderOnce(self):

87 if self.hdr is None:

88 self.hdr = PCapFileHeader()

89

90 def writeHeaderOnce(self):

91 if not self.wroteHeader:

92 self.wroteHeader = True

93 self.file.seek(0)

94 self.createHeaderOnce()

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

96

97 def read(self):

98 self.readHeaderOnce()

99 try:

100 pkt = PCapFilePacket.fromFile(self.file)

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

102 return pkt

103 except:

104 return None

105

106 def write(self, pkt):

107 self.writeHeaderOnce()

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

109

110 def packets(self):

111 self.reset()

112 while 1:

113 answer = self.read()

114 if answer is None:

115 break

116 yield answer