Hide keyboard shortcuts

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 

8import struct 

9import array 

10 

11from impacket.ImpactPacket import Header, array_frombytes 

12from impacket.IP6_Address import IP6_Address 

13from impacket.IP6_Extension_Headers import IP6_Extension_Header 

14 

15from impacket import LOG 

16 

17 

18class IP6(Header): 

19 #Ethertype value for IPv6 

20 ethertype = 0x86DD 

21 HEADER_SIZE = 40 

22 IP_PROTOCOL_VERSION = 6 

23 

24 def __init__(self, buffer = None): 

25 Header.__init__(self, IP6.HEADER_SIZE) 

26 self.set_ip_v(IP6.IP_PROTOCOL_VERSION) 

27 if (buffer): 

28 self.load_header(buffer) 

29 

30 def contains(self, aHeader): 

31 Header.contains(self, aHeader) 

32 if isinstance(aHeader, IP6_Extension_Header): 

33 self.set_next_header(aHeader.get_header_type()) 

34 

35 def get_header_size(self): 

36 return IP6.HEADER_SIZE 

37 

38 def __str__(self): 

39 protocol_version = self.get_ip_v() 

40 traffic_class = self.get_traffic_class() 

41 flow_label = self.get_flow_label() 

42 payload_length = self.get_payload_length() 

43 next_header = self.get_next_header() 

44 hop_limit = self.get_hop_limit() 

45 source_address = self.get_ip_src() 

46 destination_address = self.get_ip_dst() 

47 

48 s = "Protocol version: " + str(protocol_version) + "\n" 

49 s += "Traffic class: " + str(traffic_class) + "\n" 

50 s += "Flow label: " + str(flow_label) + "\n" 

51 s += "Payload length: " + str(payload_length) + "\n" 

52 s += "Next header: " + str(next_header) + "\n" 

53 s += "Hop limit: " + str(hop_limit) + "\n" 

54 s += "Source address: " + source_address.as_string() + "\n" 

55 s += "Destination address: " + destination_address.as_string() + "\n" 

56 return s 

57 

58 def get_pseudo_header(self): 

59 source_address = self.get_ip_src().as_bytes() 

60 #FIXME - Handle Routing header special case 

61 destination_address = self.get_ip_dst().as_bytes() 

62 reserved_bytes = [ 0x00, 0x00, 0x00 ] 

63 

64 upper_layer_packet_length = self.get_payload_length() 

65 upper_layer_protocol_number = self.get_next_header() 

66 

67 next_header = self.child() 

68 while isinstance(next_header, IP6_Extension_Header): 68 ↛ 71line 68 didn't jump to line 71, because the condition on line 68 was never true

69 # The length used in the pseudo-header is the Payload Length from the IPv6 header, minus 

70 # the length of any extension headers present between the IPv6 header and the upper-layer header 

71 upper_layer_packet_length -= next_header.get_header_size() 

72 

73 # If there are extension headers, fetch the correct upper-player protocol number by traversing the list 

74 upper_layer_protocol_number = next_header.get_next_header() 

75 

76 next_header = next_header.child() 

77 

78 pseudo_header = array.array('B') 

79 pseudo_header.extend(source_address) 

80 pseudo_header.extend(destination_address) 

81 array_frombytes(pseudo_header, struct.pack('!L', upper_layer_packet_length)) 

82 pseudo_header.fromlist(reserved_bytes) 

83 array_frombytes(pseudo_header, struct.pack('B', upper_layer_protocol_number)) 

84 return pseudo_header 

85 

86############################################################################ 

87 def get_ip_v(self): 

88 return (self.get_byte(0) & 0xF0) >> 4 

89 

90 def get_traffic_class(self): 

91 return ((self.get_byte(0) & 0x0F) << 4) | ((self.get_byte(1) & 0xF0) >> 4) 

92 

93 def get_flow_label(self): 

94 return (self.get_byte(1) & 0x0F) << 16 | (self.get_byte(2) << 8) | self.get_byte(3) 

95 

96 def get_payload_length(self): 

97 return (self.get_byte(4) << 8) | self.get_byte(5) 

98 

99 def get_next_header(self): 

100 return (self.get_byte(6)) 

101 

102 def get_hop_limit(self): 

103 return (self.get_byte(7)) 

104 

105 def get_ip_src(self): 

106 address = IP6_Address(self.get_bytes()[8:24]) 

107 return (address) 

108 

109 def get_ip_dst(self): 

110 address = IP6_Address(self.get_bytes()[24:40]) 

111 return (address) 

112 

113############################################################################ 

114 def set_ip_v(self, version): 

115 if (version != 6): 115 ↛ 116line 115 didn't jump to line 116, because the condition on line 115 was never true

116 raise Exception('set_ip_v - version != 6') 

117 

118 #Fetch byte, clear high nibble 

119 b = self.get_byte(0) & 0x0F 

120 #Store version number in high nibble 

121 b |= (version << 4) 

122 #Store byte in buffer 

123 #This behaviour is repeated in the rest of the methods  

124 self.set_byte(0, b) 

125 

126 

127 def set_traffic_class(self, traffic_class): 

128 b0 = self.get_byte(0) & 0xF0 

129 b1 = self.get_byte(1) & 0x0F 

130 b0 |= (traffic_class & 0xF0) >> 4 

131 b1 |= (traffic_class & 0x0F) << 4 

132 self.set_byte(0, b0) 

133 self.set_byte(1, b1) 

134 

135 

136 def set_flow_label(self, flow_label): 

137 b1 = self.get_byte(1) & 0xF0 

138 b1 |= (flow_label & 0xF0000) >> 16 

139 self.set_byte(1, b1) 

140 self.set_byte(2, (flow_label & 0x0FF00) >> 8) 

141 self.set_byte(3, (flow_label & 0x000FF)) 

142 

143 

144 def set_payload_length(self, payload_length): 

145 self.set_byte(4, (payload_length & 0xFF00) >> 8) 

146 self.set_byte(5, (payload_length & 0x00FF)) 

147 

148 

149 def set_next_header(self, next_header): 

150 self.set_byte(6, next_header) 

151 

152 def set_hop_limit(self, hop_limit): 

153 self.set_byte(7, hop_limit) 

154 

155 def set_ip_src(self, source_address): 

156 address = IP6_Address(source_address) 

157 bytes = self.get_bytes() 

158 bytes[8:24] = address.as_bytes() 

159 self.set_bytes(bytes) 

160 

161 def set_ip_dst(self, destination_address): 

162 address = IP6_Address(destination_address) 

163 bytes = self.get_bytes() 

164 bytes[24:40] = address.as_bytes() 

165 self.set_bytes(bytes) 

166 

167 def get_protocol_version(self): 

168 LOG.warning('deprecated soon') 

169 return self.get_ip_v() 

170 

171 def get_source_address(self): 

172 LOG.warning('deprecated soon') 

173 return self.get_ip_src() 

174 

175 def get_destination_address(self): 

176 LOG.warning('deprecated soon') 

177 return self.get_ip_dst() 

178 

179 def set_protocol_version(self, version): 

180 LOG.warning('deprecated soon') 

181 self.set_ip_v(version) 

182 

183 def set_source_address(self, source_address): 

184 LOG.warning('deprecated soon') 

185 self.set_ip_src(source_address) 

186 

187 def set_destination_address(self, destination_address): 

188 LOG.warning('deprecated soon') 

189 self.set_ip_dst(destination_address)