Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | 2015-2016 |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | Copyright 2016 Open Networking Foundation (ONF) |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 5 | |
| 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 9 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
| 13 | ( at your option ) any later version. |
| 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | ScapyCliDriver is the basic driver which will handle the Scapy functions |
| 24 | |
| 25 | TODO: Add Explanation on how to install scapy |
| 26 | """ |
| 27 | import pexpect |
| 28 | import re |
| 29 | import sys |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 30 | import os |
| 31 | from drivers.common.cli.emulatordriver import Emulator |
| 32 | |
| 33 | |
| 34 | class ScapyCliDriver( Emulator ): |
| 35 | |
| 36 | """ |
| 37 | ScapyCliDriver is the basic driver which will handle |
| 38 | the Scapy functions""" |
| 39 | def __init__( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 40 | super( ScapyCliDriver, self ).__init__() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 41 | self.handle = self |
| 42 | self.name = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 43 | self.home = "~/" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 44 | self.wrapped = sys.modules[ __name__ ] |
| 45 | self.flag = 0 |
| 46 | # TODO: Refactor driver to use these everywhere |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 47 | self.hostPrompt = "\$" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 48 | self.scapyPrompt = ">>>" |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 49 | self.sudoRequired = True |
| 50 | self.ifaceName = None |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 51 | |
| 52 | def connect( self, **connectargs ): |
| 53 | """ |
| 54 | Here the main is the TestON instance after creating |
| 55 | all the log handles.""" |
| 56 | try: |
| 57 | for key in connectargs: |
| 58 | vars( self )[ key ] = connectargs[ key ] |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 59 | for key in self.options: |
| 60 | if key == "home": |
| 61 | self.home = self.options[ key ] |
| 62 | elif key == "name": |
| 63 | self.name = self.options[ key ] |
| 64 | elif key == "sudo_required": |
| 65 | self.sudoRequired = False if self.options[ key ] == "false" else True |
| 66 | elif key == "ifaceName": |
| 67 | self.ifaceName = self.options[ key ] |
| 68 | if self.ifaceName is None: |
| 69 | self.ifaceName = self.name + "-eth0" |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 70 | |
| 71 | # Parse route config |
| 72 | self.routes = [] |
| 73 | routes = self.options.get( 'routes' ) |
| 74 | if routes: |
| 75 | for route in routes: |
| 76 | route = routes[ route ] |
| 77 | iface = route.get( 'interface' ) |
| 78 | if not iface: |
| 79 | iface = None |
| 80 | self.routes.append( { 'network': route[ 'network' ], |
| 81 | 'netmask': route[ 'netmask' ], |
| 82 | 'gw': route.get( 'gw' ), |
| 83 | 'interface': iface } ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 84 | try: |
| 85 | if os.getenv( str( self.ip_address ) ) is not None: |
| 86 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 87 | else: |
| 88 | main.log.info( self.name + |
| 89 | ": Trying to connect to " + |
| 90 | self.ip_address ) |
| 91 | |
| 92 | except KeyError: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 93 | main.log.info( self.name + ": Invalid host name," + |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 94 | " connecting to local host instead" ) |
| 95 | self.ip_address = 'localhost' |
| 96 | except Exception as inst: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 97 | main.log.error( self.name + ": Uncaught exception: " + str( inst ) ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 98 | |
| 99 | self.handle = super( |
| 100 | ScapyCliDriver, |
| 101 | self ).connect( |
| 102 | user_name=self.user_name, |
| 103 | ip_address=self.ip_address, |
| 104 | port=None, |
| 105 | pwd=self.pwd ) |
| 106 | |
| 107 | if self.handle: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 108 | main.log.info( self.name + ": Connection successful to the host " + |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 109 | self.user_name + |
| 110 | "@" + |
| 111 | self.ip_address ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 112 | return self.handle |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 113 | else: |
| 114 | main.log.error( "Connection failed to the host " + |
| 115 | self.user_name + |
| 116 | "@" + |
| 117 | self.ip_address ) |
Jon Hall | 3a03cad | 2021-04-07 11:21:55 -0700 | [diff] [blame^] | 118 | main.log.error( "Failed to connect to the Host" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 119 | return main.FALSE |
| 120 | except pexpect.EOF: |
| 121 | main.log.error( self.name + ": EOF exception found" ) |
| 122 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 123 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 124 | except Exception: |
| 125 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 126 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 127 | |
| 128 | def disconnect( self ): |
| 129 | """ |
| 130 | Called at the end of the test to stop the scapy component and |
| 131 | disconnect the handle. |
| 132 | """ |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 133 | main.log.debug( self.name + ": Disconnecting" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 134 | response = main.TRUE |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 135 | try: |
| 136 | if self.handle: |
| 137 | self.handle.sendline( "exit" ) |
| 138 | self.handle.expect( "closed" ) |
| 139 | except pexpect.EOF: |
| 140 | main.log.error( self.name + ": EOF exception found" ) |
| 141 | main.log.error( self.name + ": " + self.handle.before ) |
| 142 | except Exception: |
| 143 | main.log.exception( self.name + ": Connection failed to the host" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 144 | response = main.FALSE |
| 145 | return response |
| 146 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 147 | def startScapy( self, mplsPath="", ifaceName=None ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 148 | """ |
| 149 | Start the Scapy cli |
| 150 | optional: |
| 151 | mplsPath - The path where the MPLS class is located |
| 152 | NOTE: This can be a relative path from the user's home dir |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 153 | ifaceName - the name of the default interface to use. |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 154 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 155 | mplsLines = [ 'import imp', |
| 156 | 'imp.load_source( "mplsClass", "{}mplsClass.py" )'.format( mplsPath ), |
| 157 | 'from mplsClass import MPLS', |
| 158 | 'bind_layers(Ether, MPLS, type = 0x8847)', |
| 159 | 'bind_layers(MPLS, MPLS, bottom_of_label_stack = 0)', |
| 160 | 'bind_layers(MPLS, IP)' ] |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 161 | |
| 162 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 163 | main.log.debug( self.name + ": Starting scapy" ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 164 | if self.sudoRequired: |
| 165 | self.handle.sendline( "sudo scapy" ) |
| 166 | else: |
| 167 | self.handle.sendline( "scapy" ) |
| 168 | i = self.handle.expect( [ "not found", "password for", self.scapyPrompt ] ) |
| 169 | if i == 1: |
| 170 | main.log.debug( "Sudo asking for password" ) |
| 171 | main.log.sendline( self.pwd ) |
| 172 | i = self.handle.expect( [ "not found", self.scapyPrompt ] ) |
| 173 | if i == 0: |
| 174 | output = self.handle.before + self.handle.after |
| 175 | self.handle.expect( self.prompt ) |
| 176 | output += self.handle.before + self.handle.after |
| 177 | main.log.debug( self.name + ": Scapy not installed, aborting test. \n" + output ) |
| 178 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 179 | self.handle.sendline( "conf.color_theme = NoTheme()" ) |
| 180 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 181 | response = self.cleanOutput( self.handle.before ) |
| 182 | self.handle.sendline( "conf.fancy_prompt = False" ) |
| 183 | self.handle.expect( self.scapyPrompt ) |
| 184 | response = self.cleanOutput( self.handle.before ) |
| 185 | self.handle.sendline( "conf.interactive = False" ) |
| 186 | self.handle.expect( "interactive" ) |
| 187 | self.handle.expect( self.scapyPrompt ) |
| 188 | response = self.cleanOutput( self.handle.before ) |
| 189 | self.handle.sendline( "" ) |
| 190 | self.handle.expect( self.scapyPrompt ) |
| 191 | response = self.cleanOutput( self.handle.before ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 192 | if mplsPath: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 193 | main.log.debug( self.name + ": Adding MPLS class" ) |
| 194 | main.log.debug( self.name + ": MPLS class path: " + mplsPath ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 195 | for line in mplsLines: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 196 | main.log.debug( self.name + ": sending line: " + line ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 197 | self.handle.sendline( line ) |
| 198 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 199 | response = self.cleanOutput( self.handle.before ) |
| 200 | |
| 201 | # Set interface |
| 202 | if ifaceName: |
| 203 | self.handle.sendline( 'conf.iface = "' + ifaceName + '"' ) |
| 204 | self.clearBuffer() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 205 | return main.TRUE |
| 206 | except pexpect.TIMEOUT: |
| 207 | main.log.exception( self.name + ": Command timed out" ) |
| 208 | return main.FALSE |
| 209 | except pexpect.EOF: |
| 210 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 211 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 212 | except Exception: |
| 213 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 214 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 215 | |
| 216 | def stopScapy( self ): |
| 217 | """ |
| 218 | Exit the Scapy cli |
| 219 | """ |
| 220 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 221 | main.log.debug( self.name + ": Stopping scapy" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 222 | self.handle.sendline( "exit()" ) |
| 223 | self.handle.expect( self.hostPrompt ) |
| 224 | return main.TRUE |
| 225 | except pexpect.TIMEOUT: |
| 226 | main.log.exception( self.name + ": Command timed out" ) |
| 227 | return main.FALSE |
| 228 | except pexpect.EOF: |
| 229 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 230 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 231 | except Exception: |
| 232 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 233 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 234 | |
| 235 | def buildEther( self, **kwargs ): |
| 236 | """ |
| 237 | Build an Ethernet frame |
| 238 | |
| 239 | Will create a frame class with the given options. If a field is |
| 240 | left blank it will default to the below value unless it is |
| 241 | overwritten by the next frame. |
| 242 | Default frame: |
| 243 | ###[ Ethernet ]### |
| 244 | dst= ff:ff:ff:ff:ff:ff |
| 245 | src= 00:00:00:00:00:00 |
| 246 | type= 0x800 |
| 247 | |
| 248 | Returns main.TRUE or main.FALSE on error |
| 249 | """ |
| 250 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 251 | main.log.debug( self.name + ": Building Ethernet Frame" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 252 | # Set the Ethernet frame |
| 253 | cmd = 'ether = Ether( ' |
| 254 | options = [] |
| 255 | for key, value in kwargs.iteritems(): |
| 256 | if isinstance( value, str ): |
| 257 | value = '"' + value + '"' |
| 258 | options.append( str( key ) + "=" + str( value ) ) |
| 259 | cmd += ", ".join( options ) |
| 260 | cmd += ' )' |
| 261 | self.handle.sendline( cmd ) |
| 262 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 263 | response = self.cleanOutput( self.handle.before ) |
| 264 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 265 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 266 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 267 | return main.FALSE |
| 268 | self.handle.sendline( "packet = ether" ) |
| 269 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 270 | response = self.cleanOutput( self.handle.before ) |
| 271 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 272 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 273 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 274 | return main.FALSE |
| 275 | return main.TRUE |
| 276 | except pexpect.TIMEOUT: |
| 277 | main.log.exception( self.name + ": Command timed out" ) |
| 278 | return main.FALSE |
| 279 | except pexpect.EOF: |
| 280 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 281 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 282 | except Exception: |
| 283 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 284 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 285 | |
| 286 | def buildIP( self, **kwargs ): |
| 287 | """ |
| 288 | Build an IP frame |
| 289 | |
| 290 | Will create a frame class with the given options. If a field is |
| 291 | left blank it will default to the below value unless it is |
| 292 | overwritten by the next frame. |
| 293 | Default frame: |
| 294 | ###[ IP ]### |
| 295 | version= 4 |
| 296 | ihl= None |
| 297 | tos= 0x0 |
| 298 | len= None |
| 299 | id= 1 |
| 300 | flags= |
| 301 | frag= 0 |
| 302 | ttl= 64 |
| 303 | proto= hopopt |
| 304 | chksum= None |
| 305 | src= 127.0.0.1 |
| 306 | dst= 127.0.0.1 |
| 307 | \options\ |
| 308 | |
| 309 | Returns main.TRUE or main.FALSE on error |
| 310 | """ |
| 311 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 312 | main.log.debug( self.name + ": Building IP Frame" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 313 | # Set the IP frame |
| 314 | cmd = 'ip = IP( ' |
| 315 | options = [] |
| 316 | for key, value in kwargs.iteritems(): |
| 317 | if isinstance( value, str ): |
| 318 | value = '"' + value + '"' |
| 319 | options.append( str( key ) + "=" + str( value ) ) |
| 320 | cmd += ", ".join( options ) |
| 321 | cmd += ' )' |
| 322 | self.handle.sendline( cmd ) |
| 323 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 324 | response = self.cleanOutput( self.handle.before ) |
| 325 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 326 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 327 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 328 | return main.FALSE |
| 329 | self.handle.sendline( "packet = ether/ip" ) |
| 330 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 331 | response = self.cleanOutput( self.handle.before ) |
| 332 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 333 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 334 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 335 | return main.FALSE |
| 336 | return main.TRUE |
| 337 | except pexpect.TIMEOUT: |
| 338 | main.log.exception( self.name + ": Command timed out" ) |
| 339 | return main.FALSE |
| 340 | except pexpect.EOF: |
| 341 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 342 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 343 | except Exception: |
| 344 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 345 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 346 | |
| 347 | def buildIPv6( self, **kwargs ): |
| 348 | """ |
| 349 | Build an IPv6 frame |
| 350 | |
| 351 | Will create a frame class with the given options. If a field is |
| 352 | left blank it will default to the below value unless it is |
| 353 | overwritten by the next frame. |
| 354 | Default frame: |
| 355 | ###[ IPv6 ]### |
| 356 | version= 6 |
| 357 | tc= 0 |
| 358 | fl= 0 |
| 359 | plen= None |
| 360 | nh= No Next Header |
| 361 | hlim= 64 |
| 362 | src= ::1 |
| 363 | dst= ::1 |
| 364 | |
| 365 | Returns main.TRUE or main.FALSE on error |
| 366 | """ |
| 367 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 368 | main.log.debug( self.name + ": Building IPv6 Frame" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 369 | # Set the IPv6 frame |
| 370 | cmd = 'ipv6 = IPv6( ' |
| 371 | options = [] |
| 372 | for key, value in kwargs.iteritems(): |
| 373 | if isinstance( value, str ): |
| 374 | value = '"' + value + '"' |
| 375 | options.append( str( key ) + "=" + str( value ) ) |
| 376 | cmd += ", ".join( options ) |
| 377 | cmd += ' )' |
| 378 | self.handle.sendline( cmd ) |
| 379 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 380 | response = self.cleanOutput( self.handle.before ) |
| 381 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 382 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 383 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 384 | return main.FALSE |
| 385 | self.handle.sendline( "packet = ether/ipv6" ) |
| 386 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 387 | response = self.cleanOutput( self.handle.before ) |
| 388 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 389 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 390 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 391 | return main.FALSE |
| 392 | return main.TRUE |
| 393 | except pexpect.TIMEOUT: |
| 394 | main.log.exception( self.name + ": Command timed out" ) |
| 395 | return main.FALSE |
| 396 | except pexpect.EOF: |
| 397 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 398 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 399 | except Exception: |
| 400 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 401 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 402 | |
| 403 | def buildTCP( self, ipVersion=4, **kwargs ): |
| 404 | """ |
| 405 | Build an TCP frame |
| 406 | |
| 407 | Will create a frame class with the given options. If a field is |
| 408 | left blank it will default to the below value unless it is |
| 409 | overwritten by the next frame. |
| 410 | |
| 411 | NOTE: Some arguments require quotes around them. It's up to you to |
| 412 | know which ones and to add them yourself. Arguments with an asterisk |
| 413 | do not need quotes. |
| 414 | |
| 415 | Options: |
| 416 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 417 | frame to use to encapsulate into |
| 418 | Default frame: |
| 419 | ###[ TCP ]### |
| 420 | sport= ftp_data * |
| 421 | dport= http * |
| 422 | seq= 0 |
| 423 | ack= 0 |
| 424 | dataofs= None |
| 425 | reserved= 0 |
| 426 | flags= S |
| 427 | window= 8192 |
| 428 | chksum= None |
| 429 | urgptr= 0 |
| 430 | options= {} |
| 431 | |
| 432 | Returns main.TRUE or main.FALSE on error |
| 433 | """ |
| 434 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 435 | main.log.debug( self.name + ": Building TCP" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 436 | # Set the TCP frame |
| 437 | cmd = 'tcp = TCP( ' |
| 438 | options = [] |
| 439 | for key, value in kwargs.iteritems(): |
| 440 | options.append( str( key ) + "=" + str( value ) ) |
| 441 | cmd += ", ".join( options ) |
| 442 | cmd += ' )' |
| 443 | self.handle.sendline( cmd ) |
| 444 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 445 | response = self.cleanOutput( self.handle.before ) |
| 446 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 447 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 448 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 449 | return main.FALSE |
| 450 | if str( ipVersion ) is '4': |
| 451 | self.handle.sendline( "packet = ether/ip/tcp" ) |
| 452 | elif str( ipVersion ) is '6': |
| 453 | self.handle.sendline( "packet = ether/ipv6/tcp" ) |
| 454 | else: |
| 455 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 456 | repr( ipVersion ) ) |
| 457 | return main.FALSE |
| 458 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 459 | response = self.cleanOutput( self.handle.before ) |
| 460 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 461 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 462 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 463 | return main.FALSE |
| 464 | return main.TRUE |
| 465 | except pexpect.TIMEOUT: |
| 466 | main.log.exception( self.name + ": Command timed out" ) |
| 467 | return main.FALSE |
| 468 | except pexpect.EOF: |
| 469 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 470 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 471 | except Exception: |
| 472 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 473 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 474 | |
| 475 | def buildUDP( self, ipVersion=4, **kwargs ): |
| 476 | """ |
| 477 | Build an UDP frame |
| 478 | |
| 479 | Will create a frame class with the given options. If a field is |
| 480 | left blank it will default to the below value unless it is |
| 481 | overwritten by the next frame. |
| 482 | |
| 483 | NOTE: Some arguments require quotes around them. It's up to you to |
| 484 | know which ones and to add them yourself. Arguments with an asterisk |
| 485 | do not need quotes. |
| 486 | |
| 487 | Options: |
| 488 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 489 | frame to use to encapsulate into |
| 490 | Default frame: |
| 491 | ###[ UDP ]### |
| 492 | sport= domain * |
| 493 | dport= domain * |
| 494 | len= None |
| 495 | chksum= None |
| 496 | |
| 497 | Returns main.TRUE or main.FALSE on error |
| 498 | """ |
| 499 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 500 | main.log.debug( self.name + ": Building UDP Frame" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 501 | # Set the UDP frame |
| 502 | cmd = 'udp = UDP( ' |
| 503 | options = [] |
| 504 | for key, value in kwargs.iteritems(): |
| 505 | options.append( str( key ) + "=" + str( value ) ) |
| 506 | cmd += ", ".join( options ) |
| 507 | cmd += ' )' |
| 508 | self.handle.sendline( cmd ) |
| 509 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 510 | response = self.cleanOutput( self.handle.before ) |
| 511 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 512 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 513 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 514 | return main.FALSE |
| 515 | if str( ipVersion ) is '4': |
| 516 | self.handle.sendline( "packet = ether/ip/udp" ) |
| 517 | elif str( ipVersion ) is '6': |
| 518 | self.handle.sendline( "packet = ether/ipv6/udp" ) |
| 519 | else: |
| 520 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 521 | repr( ipVersion ) ) |
| 522 | return main.FALSE |
| 523 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 524 | response = self.cleanOutput( self.handle.before ) |
| 525 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 526 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 527 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 528 | return main.FALSE |
| 529 | return main.TRUE |
| 530 | except pexpect.TIMEOUT: |
| 531 | main.log.exception( self.name + ": Command timed out" ) |
| 532 | return main.FALSE |
| 533 | except pexpect.EOF: |
| 534 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 535 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 536 | except Exception: |
| 537 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 538 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 539 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 540 | def buildSCTP( self, ipVersion=4, **kwargs ): |
| 541 | """ |
| 542 | Build an SCTP frame |
| 543 | |
| 544 | Will create a frame class with the given options. If a field is |
| 545 | left blank it will default to the below value unless it is |
| 546 | overwritten by the next frame. |
| 547 | |
| 548 | NOTE: Some arguments require quotes around them. It's up to you to |
| 549 | know which ones and to add them yourself. Arguments with an asterisk |
| 550 | do not need quotes. |
| 551 | |
| 552 | Options: |
| 553 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 554 | frame to use to encapsulate into |
| 555 | Default frame: |
| 556 | ###[ SCTP ]### |
| 557 | sport= domain * |
| 558 | dport= domain * |
| 559 | tag = None |
| 560 | chksum = None |
| 561 | |
| 562 | Returns main.TRUE or main.FALSE on error |
| 563 | """ |
| 564 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 565 | main.log.debug( self.name + ": Building SCTP Frame" ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 566 | # Set the SCTP frame |
| 567 | cmd = 'sctp = SCTP( ' |
| 568 | options = [ ] |
| 569 | for key, value in kwargs.iteritems( ): |
| 570 | options.append( str( key ) + "=" + str( value ) ) |
| 571 | cmd += ", ".join( options ) |
| 572 | cmd += ' )' |
| 573 | self.handle.sendline( cmd ) |
| 574 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 575 | response = self.cleanOutput( self.handle.before ) |
| 576 | if "Traceback" in response: |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 577 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 578 | main.log.error( "Error in sending command: " + response ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 579 | return main.FALSE |
| 580 | if str( ipVersion ) is '4': |
| 581 | self.handle.sendline( "packet = ether/ip/sctp" ) |
| 582 | elif str( ipVersion ) is '6': |
| 583 | self.handle.sendline( "packet = ether/ipv6/sctp" ) |
| 584 | else: |
| 585 | main.log.error( "Unrecognized option for ipVersion, given " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 586 | repr( ipVersion ) ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 587 | return main.FALSE |
| 588 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 589 | response = self.cleanOutput( self.handle.before ) |
| 590 | if "Traceback" in response: |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 591 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 592 | main.log.error( "Error in sending command: " + response ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 593 | return main.FALSE |
| 594 | return main.TRUE |
| 595 | except pexpect.TIMEOUT: |
| 596 | main.log.exception( self.name + ": Command timed out" ) |
| 597 | return main.FALSE |
| 598 | except pexpect.EOF: |
| 599 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 600 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 601 | except Exception: |
| 602 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 603 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 604 | |
| 605 | def buildARP( self, **kwargs ): |
| 606 | """ |
| 607 | Build an ARP frame |
| 608 | |
| 609 | Will create a frame class with the given options. If a field is |
| 610 | left blank it will default to the below value unless it is |
| 611 | overwritten by the next frame. |
| 612 | |
| 613 | NOTE: Some arguments require quotes around them. It's up to you to |
| 614 | know which ones and to add them yourself. Arguments with an asterisk |
| 615 | do not need quotes. |
| 616 | |
| 617 | Default frame: |
| 618 | ###[ ARP ]### |
| 619 | hwtype : XShortField = (1) |
| 620 | ptype : XShortEnumField = (2048) |
| 621 | hwlen : ByteField = (6) |
| 622 | plen : ByteField = (4) |
| 623 | op : ShortEnumField = (1) |
| 624 | hwsrc : ARPSourceMACField = (None) |
| 625 | psrc : SourceIPField = (None) |
| 626 | hwdst : MACField = ('00:00:00:00:00:00') |
| 627 | pdst : IPField = ('0.0.0.0') |
| 628 | |
| 629 | Returns main.TRUE or main.FALSE on error |
| 630 | """ |
| 631 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 632 | main.log.debug( self.name + ": Building ARP Frame" ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 633 | # Set the ARP frame |
| 634 | cmd = 'arp = ARP( ' |
| 635 | options = [] |
| 636 | for key, value in kwargs.iteritems( ): |
| 637 | if isinstance( value, str ): |
| 638 | value = '"' + value + '"' |
| 639 | options.append( str( key ) + "=" + str( value ) ) |
| 640 | cmd += ", ".join( options ) |
| 641 | cmd += ' )' |
| 642 | self.handle.sendline( cmd ) |
| 643 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 644 | response = self.cleanOutput( self.handle.before ) |
| 645 | if "Traceback" in response: |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 646 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 647 | main.log.error( "Error in sending command: " + response ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 648 | return main.FALSE |
| 649 | self.handle.sendline( "packet = ether/arp" ) |
| 650 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 651 | response = self.cleanOutput( self.handle.before ) |
| 652 | if "Traceback" in response: |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 653 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 654 | main.log.error( "Error in sending command: " + response ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 655 | return main.FALSE |
| 656 | return main.TRUE |
| 657 | except pexpect.TIMEOUT: |
| 658 | main.log.exception( self.name + ": Command timed out" ) |
| 659 | return main.FALSE |
| 660 | except pexpect.EOF: |
| 661 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 662 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 663 | except Exception: |
| 664 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 665 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 666 | |
| 667 | def buildICMP( self, ipVersion=4, **kwargs ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 668 | """ |
| 669 | Build an ICMP frame |
| 670 | |
| 671 | Will create a frame class with the given options. If a field is |
| 672 | left blank it will default to the below value unless it is |
| 673 | overwritten by the next frame. |
| 674 | Default frame: |
| 675 | ###[ ICMP ]### |
| 676 | type= echo-request |
| 677 | code= 0 |
| 678 | chksum= None |
| 679 | id= 0x0 |
| 680 | seq= 0x0 |
| 681 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 682 | Options: |
| 683 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 684 | frame to use to encapsulate into |
| 685 | |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 686 | Returns main.TRUE or main.FALSE on error |
| 687 | """ |
| 688 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 689 | main.log.debug( self.name + ": Building ICMP Frame" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 690 | # Set the ICMP frame |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 691 | if str( ipVersion ) is '4': |
| 692 | cmd = 'icmp = ICMP( ' |
| 693 | elif str( ipVersion ) is '6': |
| 694 | cmd = 'icmp6 = ICMPv6EchoReply( ' |
| 695 | else: |
| 696 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 697 | repr( ipVersion ) ) |
| 698 | return main.FALSE |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 699 | options = [] |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 700 | for key, value in kwargs.iteritems( ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 701 | if isinstance( value, str ): |
| 702 | value = '"' + value + '"' |
| 703 | options.append( str( key ) + "=" + str( value ) ) |
| 704 | cmd += ", ".join( options ) |
| 705 | cmd += ' )' |
| 706 | self.handle.sendline( cmd ) |
| 707 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 708 | response = self.cleanOutput( self.handle.before ) |
| 709 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 710 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 711 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 712 | return main.FALSE |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 713 | |
| 714 | if str( ipVersion ) is '4': |
| 715 | self.handle.sendline( "packet = ether/ip/icmp" ) |
| 716 | elif str( ipVersion ) is '6': |
| 717 | self.handle.sendline( "packet = ether/ipv6/icmp6" ) |
| 718 | else: |
| 719 | main.log.error( "Unrecognized option for ipVersion, given " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 720 | repr( ipVersion ) ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 721 | return main.FALSE |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 722 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 723 | response = self.cleanOutput( self.handle.before ) |
| 724 | if "Traceback" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 725 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 726 | main.log.error( "Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 727 | return main.FALSE |
| 728 | return main.TRUE |
| 729 | except pexpect.TIMEOUT: |
| 730 | main.log.exception( self.name + ": Command timed out" ) |
| 731 | return main.FALSE |
| 732 | except pexpect.EOF: |
| 733 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 734 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 735 | except Exception: |
| 736 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 737 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 738 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 739 | def clearBuffer( self, debug=False ): |
| 740 | """ |
| 741 | Keep reading from buffer until its empty |
| 742 | Everything seems to be printed twice in newer versions of |
| 743 | scapy, even when turning off fancy output |
| 744 | """ |
| 745 | i = 0 |
| 746 | response = '' |
| 747 | while True: |
| 748 | try: |
| 749 | i += 1 |
| 750 | # clear buffer |
| 751 | if debug: |
| 752 | main.log.warn( "%s expect loop iteration" % i ) |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 753 | self.handle.expect( self.scapyPrompt, timeout=5 ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 754 | response += self.cleanOutput( self.handle.before, debug ) |
| 755 | except pexpect.TIMEOUT: |
| 756 | return response |
| 757 | |
| 758 | def sendPacket( self, iface=None, packet=None, timeout=1, debug=True ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 759 | """ |
| 760 | Send a packet with either the given scapy packet command, or use the |
| 761 | packet saved in the variable 'packet'. |
| 762 | |
| 763 | Examples of a valid string for packet: |
| 764 | |
| 765 | Simple IP packet |
| 766 | packet='Ether(dst="a6:d9:26:df:1d:4b")/IP(dst="10.0.0.2")' |
| 767 | |
| 768 | A Ping with two vlan tags |
| 769 | packet='Ether(dst='ff:ff:ff:ff:ff:ff')/Dot1Q(vlan=1)/Dot1Q(vlan=10)/ |
| 770 | IP(dst='255.255.255.255', src='192.168.0.1')/ICMP()' |
| 771 | |
| 772 | Returns main.TRUE or main.FALSE on error |
| 773 | """ |
| 774 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 775 | main.log.debug( self.name + ": Sending Packet" ) |
| 776 | if debug: |
| 777 | self.handle.sendline( "packet.summary()" ) |
| 778 | self.handle.expect( self.scapyPrompt ) |
| 779 | self.clearBuffer() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 780 | # TODO: add all params, or use kwargs |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 781 | sendCmd = 'sendp( ' |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 782 | if packet: |
| 783 | sendCmd += packet |
| 784 | else: |
| 785 | sendCmd += "packet" |
| 786 | if iface: |
| 787 | sendCmd += ", iface='{}'".format( iface ) |
| 788 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 789 | if debug: |
| 790 | sendCmd += ', return_packets=True).summary()' # show packet(s) sent |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 791 | self.handle.sendline( sendCmd ) |
| 792 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 793 | response = self.cleanOutput( self.handle.before ) |
| 794 | main.log.debug( self.name + ": Send packet response: {}".format( response ) ) |
Jon Hall | 50a0001 | 2021-03-08 11:06:11 -0800 | [diff] [blame] | 795 | if "Traceback" in response or "Errno" in response or "Error" in response: |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 796 | # KeyError, SyntaxError, ... |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 797 | main.log.error( self.name + ": Error in sending command: " + response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 798 | return main.FALSE |
| 799 | # TODO: Check # of packets sent? |
| 800 | return main.TRUE |
| 801 | except pexpect.TIMEOUT: |
| 802 | main.log.exception( self.name + ": Command timed out" ) |
| 803 | return main.FALSE |
| 804 | except pexpect.EOF: |
| 805 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 806 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 807 | except Exception: |
| 808 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 809 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 810 | |
| 811 | def startFilter( self, ifaceName=None, sniffCount=1, pktFilter="ip" ): |
| 812 | """ |
| 813 | Listen for packets using the given filters |
| 814 | |
| 815 | Options: |
| 816 | ifaceName - the name of the interface to listen on. If none is given, |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 817 | defaults to self.ifaceName which is <host name>-eth0 |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 818 | pktFilter - A string in Berkeley Packet Filter (BPF) format which |
| 819 | specifies which packets to sniff |
| 820 | sniffCount - The number of matching packets to capture before returning |
| 821 | |
| 822 | Returns main.TRUE or main.FALSE on error |
| 823 | """ |
| 824 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 825 | main.log.info( self.name + ": Starting filter on interface %s" % ifaceName ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 826 | # TODO: add all params, or use kwargs |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 827 | ifaceName = str( ifaceName ) if ifaceName else self.ifaceName |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 828 | # Set interface |
You Wang | bd848ed | 2018-03-23 13:57:42 -0700 | [diff] [blame] | 829 | self.handle.sendline( 'conf.iface = "' + ifaceName + '"' ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 830 | self.handle.expect( ifaceName ) |
| 831 | self.cleanOutput( self.handle.before + self.handle.after ) |
| 832 | self.cleanOutput( self.handle.before ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 833 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 834 | response = self.handle.before + self.handle.after |
| 835 | self.cleanOutput( response ) |
| 836 | cmd = 'pkts = sniff(count = %s, filter = "%s", prn=lambda p: p.summary() )' % ( sniffCount, pktFilter ) |
| 837 | main.log.info( self.name + ": Starting filter on " + self.name + ' > ' + cmd ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 838 | self.handle.sendline( cmd ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 839 | response = self.clearBuffer() |
| 840 | |
You Wang | 3a5f74c | 2018-08-03 14:58:15 -0700 | [diff] [blame] | 841 | # Make sure the sniff function didn't exit due to failures |
| 842 | i = self.handle.expect( [ self.scapyPrompt, pexpect.TIMEOUT ], timeout=3 ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 843 | response = self.cleanOutput( self.handle.before + str( self.handle.after ) ) |
You Wang | 3a5f74c | 2018-08-03 14:58:15 -0700 | [diff] [blame] | 844 | if i == 0: |
| 845 | # sniff exited |
| 846 | main.log.error( self.name + ": sniff function exited" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 847 | main.log.error( self.name + ": " + response ) |
You Wang | 3a5f74c | 2018-08-03 14:58:15 -0700 | [diff] [blame] | 848 | return main.FALSE |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 849 | return main.TRUE |
| 850 | except pexpect.TIMEOUT: |
| 851 | main.log.exception( self.name + ": Command timed out" ) |
| 852 | return main.FALSE |
| 853 | except pexpect.EOF: |
| 854 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 855 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 856 | except Exception: |
| 857 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 858 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 859 | |
| 860 | def checkFilter( self, timeout=10 ): |
| 861 | """ |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 862 | Check if a filter is still running. |
| 863 | Returns: |
| 864 | main.TRUE if the filter stopped |
| 865 | main.FALSE if the filter is still running |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 866 | """ |
| 867 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 868 | main.log.debug( self.name + ": Checking Filter" ) |
| 869 | self.handle.sendline( "" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 870 | i = self.handle.expect( [ self.scapyPrompt, pexpect.TIMEOUT ], timeout=timeout ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 871 | response = self.cleanOutput( self.handle.before + str( self.handle.after ), debug=True ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 872 | if i == 0: |
| 873 | return main.TRUE |
| 874 | else: |
| 875 | return main.FALSE |
| 876 | except pexpect.EOF: |
| 877 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 878 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 879 | except Exception: |
| 880 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 881 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 882 | |
| 883 | def killFilter( self ): |
| 884 | """ |
| 885 | Kill a scapy filter |
| 886 | """ |
| 887 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 888 | main.log.debug( self.name + ": Killing scapy filter" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 889 | self.handle.send( "\x03" ) # Send a ctrl-c to kill the filter |
| 890 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 891 | output = self.cleanOutput( self.handle.before, debug=True ) |
| 892 | return output |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 893 | except pexpect.TIMEOUT: |
| 894 | main.log.exception( self.name + ": Command timed out" ) |
| 895 | return None |
| 896 | except pexpect.EOF: |
| 897 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 898 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 899 | except Exception: |
| 900 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 901 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 902 | |
You Wang | 548db38 | 2020-08-12 09:17:13 -0700 | [diff] [blame] | 903 | def readPackets( self, detailed=False ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 904 | """ |
| 905 | Read all the packets captured by the previous filter |
| 906 | """ |
| 907 | try: |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 908 | main.log.debug( self.name + ": Reading Packets" ) |
| 909 | main.log.debug( self.name + ": Begin clear buffer" ) |
| 910 | self.clearBuffer() |
| 911 | main.log.debug( self.name + ": end clear buffer" ) |
| 912 | |
You Wang | 548db38 | 2020-08-12 09:17:13 -0700 | [diff] [blame] | 913 | if detailed: |
| 914 | self.handle.sendline( "[p for p in pkts]") |
| 915 | else: |
| 916 | self.handle.sendline( "pkts.summary()") |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 917 | output = self.clearBuffer() |
Jon Hall | 50a0001 | 2021-03-08 11:06:11 -0800 | [diff] [blame] | 918 | if "Traceback" in output or "Errno" in output or "Error" in output: |
| 919 | # KeyError, SyntaxError, IOError, NameError, ... |
| 920 | main.log.error( self.name + ": Error in sending command: " + output ) |
| 921 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 922 | except pexpect.TIMEOUT: |
| 923 | main.log.exception( self.name + ": Command timed out" ) |
| 924 | return None |
| 925 | except pexpect.EOF: |
| 926 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 927 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 928 | except Exception: |
| 929 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 930 | main.cleanAndExit() |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 931 | return output |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 932 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 933 | def updateSelf( self, IPv6=False ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 934 | """ |
| 935 | Updates local MAC and IP fields |
| 936 | """ |
| 937 | self.hostMac = self.getMac() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 938 | if IPv6: |
| 939 | self.hostIp = self.getIp( IPv6=True ) |
| 940 | else: |
| 941 | self.hostIp = self.getIp() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 942 | |
| 943 | def getMac( self, ifaceName=None ): |
| 944 | """ |
| 945 | Save host's MAC address |
| 946 | """ |
| 947 | try: |
You Wang | dafb6e2 | 2018-01-22 17:01:00 -0800 | [diff] [blame] | 948 | ifaceName = str( ifaceName ) if ifaceName else self.ifaceName |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 949 | cmd = 'get_if_hwaddr("' + str( ifaceName ) + '")' |
| 950 | self.handle.sendline( cmd ) |
| 951 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 952 | response = self.cleanOutput( self.handle.before ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 953 | pattern = r'(([0-9a-f]{2}[:-]){5}([0-9a-f]{2}))' |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 954 | match = re.search( pattern, response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 955 | if match: |
| 956 | return match.group() |
| 957 | else: |
| 958 | # the command will have an exception if iface doesn't exist |
| 959 | return None |
| 960 | except pexpect.TIMEOUT: |
| 961 | main.log.exception( self.name + ": Command timed out" ) |
| 962 | return None |
| 963 | except pexpect.EOF: |
| 964 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 965 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 966 | except Exception: |
| 967 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 968 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 969 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 970 | def getIp( self, ifaceName=None, IPv6=False ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 971 | """ |
| 972 | Save host's IP address |
| 973 | |
| 974 | Returns the IP of the first interface that is not a loopback device. |
| 975 | If no IP could be found then it will return 0.0.0.0. |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 976 | |
| 977 | If IPv6 is equal to True, returns IPv6 of the first interface that is not a loopback device. |
| 978 | If no IPv6 could be found then it will return :: . |
| 979 | |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 980 | """ |
| 981 | def getIPofInterface( ifaceName ): |
| 982 | cmd = 'get_if_addr("' + str( ifaceName ) + '")' |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 983 | if IPv6: |
| 984 | cmd = 'get_if_raw_addr6("' + str( ifaceName ) + '")' |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 985 | self.handle.sendline( cmd ) |
| 986 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 987 | response = self.cleanOutput( self.handle.before ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 988 | |
| 989 | pattern = r'(((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9]))' |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 990 | if IPv6: |
| 991 | pattern = r'(\\x([0-9]|[a-f]|[A-F])([0-9]|[a-f]|[A-F])){16}' |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 992 | match = re.search( pattern, response ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 993 | if match: |
| 994 | # NOTE: The command will return 0.0.0.0 if the iface doesn't exist |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 995 | if IPv6 is not True: |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 996 | if match.group() == '0.0.0.0': |
| 997 | main.log.warn( 'iface {0} has no IPv4 address'.format( ifaceName ) ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 998 | return match.group() |
| 999 | else: |
| 1000 | return None |
| 1001 | try: |
| 1002 | if not ifaceName: |
| 1003 | # Get list of interfaces |
| 1004 | ifList = self.getIfList() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 1005 | if IPv6: |
| 1006 | for ifaceName in ifList: |
| 1007 | if ifaceName == "lo": |
| 1008 | continue |
| 1009 | ip = getIPofInterface( ifaceName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1010 | if ip is not None: |
| 1011 | newip = ip |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 1012 | tmp = newip.split( "\\x" ) |
| 1013 | ip = "" |
| 1014 | counter = 0 |
| 1015 | for i in tmp: |
| 1016 | if i != "": |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1017 | counter = counter + 1 |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 1018 | if counter % 2 == 0 and counter < 16: |
| 1019 | ip = ip + i + ":" |
| 1020 | else: |
| 1021 | ip = ip + i |
| 1022 | return ip |
| 1023 | return "::" |
| 1024 | else: |
| 1025 | for ifaceName in ifList: |
| 1026 | if ifaceName == "lo": |
| 1027 | continue |
| 1028 | ip = getIPofInterface( ifaceName ) |
| 1029 | if ip != "0.0.0.0": |
| 1030 | return ip |
| 1031 | return "0.0.0.0" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1032 | else: |
| 1033 | return getIPofInterface( ifaceName ) |
| 1034 | |
| 1035 | except pexpect.TIMEOUT: |
| 1036 | main.log.exception( self.name + ": Command timed out" ) |
| 1037 | return None |
| 1038 | except pexpect.EOF: |
| 1039 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1040 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1041 | except Exception: |
| 1042 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1043 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1044 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1045 | def addRoute( self, network, gateway, interface=None ): |
| 1046 | """ |
| 1047 | Add a route to the current scapy session |
| 1048 | """ |
| 1049 | main.log.info( self.name + ": Adding route to scapy session; %s via %s out of interface %s" % ( network, gateway, interface ) ) |
| 1050 | if gateway is None: |
| 1051 | main.log.error( self.name + ": Gateway is None, cannot set route" ) |
| 1052 | return main.FALSE |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1053 | if network is None or "None" in network: |
| 1054 | main.log.error( self.name + ": Network is None, cannot set route" ) |
| 1055 | return main.FALSE |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1056 | try: |
| 1057 | cmdStr = 'conf.route.add( net="%s", gw="%s"' % ( network, gateway ) |
| 1058 | if interface: |
| 1059 | cmdStr += ', dev="%s"' % interface |
| 1060 | cmdStr += ')' |
| 1061 | self.handle.sendline( cmdStr ) |
| 1062 | self.handle.expect( self.scapyPrompt ) |
| 1063 | response = self.cleanOutput( self.handle.before ) |
| 1064 | if "Traceback" in response: |
| 1065 | main.log.error( self.name + ": Error in adding route to scappy session" ) |
| 1066 | main.log.debug( response ) |
| 1067 | return main.FALSE |
| 1068 | return main.TRUE |
| 1069 | except pexpect.TIMEOUT: |
| 1070 | main.log.exception( self.name + ": Command timed out" ) |
| 1071 | return None |
| 1072 | except pexpect.EOF: |
| 1073 | main.log.exception( self.name + ": connection closed." ) |
| 1074 | main.cleanAndExit() |
| 1075 | except Exception: |
| 1076 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1077 | main.cleanAndExit() |
| 1078 | |
| 1079 | def addRoutes( self ): |
| 1080 | """ |
| 1081 | Add any routes configured for the host |
| 1082 | """ |
| 1083 | returnValues = [] |
| 1084 | for route in self.routes: |
| 1085 | gw = route.get( 'gw' ) |
| 1086 | iface = route.get( 'interface' ) |
| 1087 | returnValues .append( self.addRoute( "%s/%s" % ( route.get( 'network' ), route.get( 'netmask' ) ), |
Jon Hall | 50a0001 | 2021-03-08 11:06:11 -0800 | [diff] [blame] | 1088 | gw if gw else main.Cluster.active(0).address, |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1089 | interface=iface if iface else self.interfaces[ 0 ].get( 'name' ) ) ) |
| 1090 | return returnValues |
| 1091 | |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1092 | def getIfList( self ): |
| 1093 | """ |
| 1094 | Return List of Interfaces |
| 1095 | """ |
| 1096 | try: |
| 1097 | self.handle.sendline( 'get_if_list()' ) |
| 1098 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1099 | response = self.cleanOutput( self.handle.before ) |
| 1100 | ifList = response.split( '\r\n' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1101 | ifList = ifList[ 1 ].replace( "'", "" )[ 1:-1 ].split( ', ' ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1102 | return ifList |
| 1103 | |
| 1104 | except pexpect.TIMEOUT: |
| 1105 | main.log.exception( self.name + ": Command timed out" ) |
| 1106 | return None |
| 1107 | except pexpect.EOF: |
| 1108 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1109 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1110 | except Exception: |
| 1111 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1112 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1113 | |
| 1114 | if __name__ != "__main__": |
| 1115 | sys.modules[ __name__ ] = ScapyCliDriver() |