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 |
| 30 | import types |
| 31 | import os |
| 32 | from drivers.common.cli.emulatordriver import Emulator |
| 33 | |
| 34 | |
| 35 | class ScapyCliDriver( Emulator ): |
| 36 | |
| 37 | """ |
| 38 | ScapyCliDriver is the basic driver which will handle |
| 39 | the Scapy functions""" |
| 40 | def __init__( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 41 | super( ScapyCliDriver, self ).__init__() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 42 | self.handle = self |
| 43 | self.name = None |
| 44 | self.home = None |
| 45 | self.wrapped = sys.modules[ __name__ ] |
| 46 | self.flag = 0 |
| 47 | # TODO: Refactor driver to use these everywhere |
| 48 | self.hostPrompt = "~#" |
| 49 | self.bashPrompt = "\$" |
| 50 | self.scapyPrompt = ">>>" |
| 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 ] |
| 59 | self.home = "~/mininet" |
| 60 | self.name = self.options[ 'name' ] |
| 61 | for key in self.options: |
| 62 | if key == "home": |
| 63 | self.home = self.options[ 'home' ] |
| 64 | break |
| 65 | if self.home is None or self.home == "": |
| 66 | self.home = "~/mininet" |
| 67 | |
| 68 | try: |
| 69 | if os.getenv( str( self.ip_address ) ) is not None: |
| 70 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 71 | else: |
| 72 | main.log.info( self.name + |
| 73 | ": Trying to connect to " + |
| 74 | self.ip_address ) |
| 75 | |
| 76 | except KeyError: |
| 77 | main.log.info( "Invalid host name," + |
| 78 | " connecting to local host instead" ) |
| 79 | self.ip_address = 'localhost' |
| 80 | except Exception as inst: |
| 81 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 82 | |
| 83 | self.handle = super( |
| 84 | ScapyCliDriver, |
| 85 | self ).connect( |
| 86 | user_name=self.user_name, |
| 87 | ip_address=self.ip_address, |
| 88 | port=None, |
| 89 | pwd=self.pwd ) |
| 90 | |
| 91 | if self.handle: |
| 92 | main.log.info( "Connection successful to the host " + |
| 93 | self.user_name + |
| 94 | "@" + |
| 95 | self.ip_address ) |
| 96 | return main.TRUE |
| 97 | else: |
| 98 | main.log.error( "Connection failed to the host " + |
| 99 | self.user_name + |
| 100 | "@" + |
| 101 | self.ip_address ) |
| 102 | main.log.error( "Failed to connect to the Mininet Host" ) |
| 103 | return main.FALSE |
| 104 | except pexpect.EOF: |
| 105 | main.log.error( self.name + ": EOF exception found" ) |
| 106 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 107 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 108 | except Exception: |
| 109 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 110 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 111 | |
| 112 | def disconnect( self ): |
| 113 | """ |
| 114 | Called at the end of the test to stop the scapy component and |
| 115 | disconnect the handle. |
| 116 | """ |
| 117 | self.handle.sendline( '' ) |
| 118 | i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ], |
| 119 | timeout=2 ) |
| 120 | response = main.TRUE |
| 121 | if i == 0: |
| 122 | response = self.stopNet() |
| 123 | elif i == 1: |
| 124 | return main.TRUE |
| 125 | # print "Disconnecting Mininet" |
| 126 | if self.handle: |
| 127 | self.handle.sendline( "exit" ) |
| 128 | self.handle.expect( "exit" ) |
| 129 | self.handle.expect( "(.*)" ) |
| 130 | else: |
| 131 | main.log.error( "Connection failed to the host" ) |
| 132 | return response |
| 133 | |
| 134 | def stopNet( self, fileName="", timeout=5 ): |
| 135 | """ |
| 136 | Stops mininet. |
| 137 | Returns main.TRUE if the mininet successfully stops and |
| 138 | main.FALSE if the pexpect handle does not exist. |
| 139 | |
| 140 | Will cleanup and exit the test if scapy fails to stop |
| 141 | """ |
| 142 | main.log.info( self.name + ": Stopping scapy..." ) |
| 143 | response = '' |
| 144 | if self.handle: |
| 145 | try: |
| 146 | self.handle.sendline( "" ) |
| 147 | i = self.handle.expect( [ '>>>', |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 148 | self.prompt, |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 149 | pexpect.EOF, |
| 150 | pexpect.TIMEOUT ], |
| 151 | timeout ) |
| 152 | if i == 0: |
| 153 | main.log.info( "Exiting scapy..." ) |
| 154 | response = self.execute( |
| 155 | cmd="exit", |
| 156 | prompt="(.*)", |
| 157 | timeout=120 ) |
| 158 | main.log.info( self.name + ": Stopped" ) |
| 159 | response = main.TRUE |
| 160 | |
| 161 | if i == 1: |
| 162 | main.log.info( " Mininet trying to exit while not " + |
| 163 | "in the mininet prompt" ) |
| 164 | elif i == 2: |
| 165 | main.log.error( "Something went wrong exiting mininet" ) |
| 166 | elif i == 3: # timeout |
| 167 | main.log.error( "Something went wrong exiting mininet " + |
| 168 | "TIMEOUT" ) |
| 169 | |
| 170 | if fileName: |
| 171 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 172 | self.handle.expect( self.prompt ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 173 | self.handle.sendline( |
| 174 | "sudo kill -9 \`ps -ef | grep \"" + |
| 175 | fileName + |
| 176 | "\" | grep -v grep | awk '{print $2}'\`" ) |
| 177 | except pexpect.EOF: |
| 178 | main.log.error( self.name + ": EOF exception found" ) |
| 179 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 180 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 181 | else: |
| 182 | main.log.error( self.name + ": Connection failed to the host" ) |
| 183 | response = main.FALSE |
| 184 | return response |
| 185 | |
| 186 | def createHostComponent( self, name ): |
| 187 | """ |
| 188 | Creates a new mininet cli component with the same parameters as self. |
| 189 | This new component is intended to be used to login to the hosts created |
| 190 | by mininet. |
| 191 | |
| 192 | Arguments: |
| 193 | name - The string of the name of this component. The new component |
| 194 | will be assigned to main.<name> . |
| 195 | In addition, main.<name>.name = str( name ) |
| 196 | """ |
| 197 | try: |
| 198 | # look to see if this component already exists |
| 199 | getattr( main, name ) |
| 200 | except AttributeError: |
| 201 | # namespace is clear, creating component |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 202 | main.componentDictionary[ name ] = main.componentDictionary[ self.name ].copy() |
| 203 | main.componentDictionary[ name ][ 'connect_order' ] = str( int( main.componentDictionary[ name ][ 'connect_order' ] ) + 1 ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 204 | main.componentInit( name ) |
| 205 | except Exception: |
| 206 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 207 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 208 | else: |
| 209 | # namespace is not clear! |
| 210 | main.log.error( name + " component already exists!" ) |
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 | |
| 213 | def removeHostComponent( self, name ): |
| 214 | """ |
| 215 | Remove host component |
| 216 | Arguments: |
| 217 | name - The string of the name of the component to delete. |
| 218 | """ |
| 219 | try: |
| 220 | # Get host component |
| 221 | component = getattr( main, name ) |
| 222 | except AttributeError: |
| 223 | main.log.error( "Component " + name + " does not exist." ) |
| 224 | return main.FALSE |
| 225 | try: |
| 226 | # Disconnect from component |
| 227 | component.disconnect() |
| 228 | # Delete component |
| 229 | delattr( main, name ) |
| 230 | # Delete component from ComponentDictionary |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 231 | del( main.componentDictionary[ name ] ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 232 | return main.TRUE |
| 233 | except Exception: |
| 234 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 235 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 236 | |
| 237 | def startHostCli( self, host=None ): |
| 238 | """ |
| 239 | Use the mininet m utility to connect to the host's cli |
| 240 | """ |
| 241 | # These are fields that can be used by scapy packets. Initialized to None |
| 242 | self.hostIp = None |
| 243 | self.hostMac = None |
| 244 | try: |
| 245 | if not host: |
| 246 | host = self.name |
| 247 | self.handle.sendline( self.home + "/util/m " + host ) |
Jeremy Ronquillo | 0f2008a | 2017-06-23 15:32:51 -0700 | [diff] [blame] | 248 | self.handle.sendline( "cd" ) |
| 249 | self.handle.expect( self.hostPrompt ) |
| 250 | self.handle.sendline( "" ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 251 | self.handle.expect( self.hostPrompt ) |
| 252 | return main.TRUE |
| 253 | except pexpect.TIMEOUT: |
| 254 | main.log.exception( self.name + ": Command timed out" ) |
| 255 | return main.FALSE |
| 256 | except pexpect.EOF: |
| 257 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 258 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 259 | except Exception: |
| 260 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 261 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 262 | |
| 263 | def startScapy( self, mplsPath="" ): |
| 264 | """ |
| 265 | Start the Scapy cli |
| 266 | optional: |
| 267 | mplsPath - The path where the MPLS class is located |
| 268 | NOTE: This can be a relative path from the user's home dir |
| 269 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 270 | mplsLines = [ 'import imp', |
| 271 | 'imp.load_source( "mplsClass", "{}mplsClass.py" )'.format( mplsPath ), |
| 272 | 'from mplsClass import MPLS', |
| 273 | 'bind_layers(Ether, MPLS, type = 0x8847)', |
| 274 | 'bind_layers(MPLS, MPLS, bottom_of_label_stack = 0)', |
| 275 | 'bind_layers(MPLS, IP)' ] |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 276 | |
| 277 | try: |
| 278 | self.handle.sendline( "scapy" ) |
| 279 | self.handle.expect( self.scapyPrompt ) |
| 280 | self.handle.sendline( "conf.color_theme = NoTheme()" ) |
| 281 | self.handle.expect( self.scapyPrompt ) |
| 282 | if mplsPath: |
| 283 | main.log.info( "Adding MPLS class" ) |
| 284 | main.log.info( "MPLS class path: " + mplsPath ) |
| 285 | for line in mplsLines: |
| 286 | main.log.info( "sending line: " + line ) |
| 287 | self.handle.sendline( line ) |
| 288 | self.handle.expect( self.scapyPrompt ) |
| 289 | return main.TRUE |
| 290 | except pexpect.TIMEOUT: |
| 291 | main.log.exception( self.name + ": Command timed out" ) |
| 292 | return main.FALSE |
| 293 | except pexpect.EOF: |
| 294 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 295 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 296 | except Exception: |
| 297 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 298 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 299 | |
| 300 | def stopScapy( self ): |
| 301 | """ |
| 302 | Exit the Scapy cli |
| 303 | """ |
| 304 | try: |
| 305 | self.handle.sendline( "exit()" ) |
| 306 | self.handle.expect( self.hostPrompt ) |
| 307 | return main.TRUE |
| 308 | except pexpect.TIMEOUT: |
| 309 | main.log.exception( self.name + ": Command timed out" ) |
| 310 | return main.FALSE |
| 311 | except pexpect.EOF: |
| 312 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 313 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 314 | except Exception: |
| 315 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 316 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 317 | |
| 318 | def buildEther( self, **kwargs ): |
| 319 | """ |
| 320 | Build an Ethernet frame |
| 321 | |
| 322 | Will create a frame class with the given options. If a field is |
| 323 | left blank it will default to the below value unless it is |
| 324 | overwritten by the next frame. |
| 325 | Default frame: |
| 326 | ###[ Ethernet ]### |
| 327 | dst= ff:ff:ff:ff:ff:ff |
| 328 | src= 00:00:00:00:00:00 |
| 329 | type= 0x800 |
| 330 | |
| 331 | Returns main.TRUE or main.FALSE on error |
| 332 | """ |
| 333 | try: |
| 334 | # Set the Ethernet frame |
| 335 | cmd = 'ether = Ether( ' |
| 336 | options = [] |
| 337 | for key, value in kwargs.iteritems(): |
| 338 | if isinstance( value, str ): |
| 339 | value = '"' + value + '"' |
| 340 | options.append( str( key ) + "=" + str( value ) ) |
| 341 | cmd += ", ".join( options ) |
| 342 | cmd += ' )' |
| 343 | self.handle.sendline( cmd ) |
| 344 | self.handle.expect( self.scapyPrompt ) |
| 345 | if "Traceback" in self.handle.before: |
| 346 | # KeyError, SyntaxError, ... |
| 347 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 348 | return main.FALSE |
| 349 | self.handle.sendline( "packet = ether" ) |
| 350 | self.handle.expect( self.scapyPrompt ) |
| 351 | if "Traceback" in self.handle.before: |
| 352 | # KeyError, SyntaxError, ... |
| 353 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 354 | return main.FALSE |
| 355 | return main.TRUE |
| 356 | except pexpect.TIMEOUT: |
| 357 | main.log.exception( self.name + ": Command timed out" ) |
| 358 | return main.FALSE |
| 359 | except pexpect.EOF: |
| 360 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 361 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 362 | except Exception: |
| 363 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 364 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 365 | |
| 366 | def buildIP( self, **kwargs ): |
| 367 | """ |
| 368 | Build an IP frame |
| 369 | |
| 370 | Will create a frame class with the given options. If a field is |
| 371 | left blank it will default to the below value unless it is |
| 372 | overwritten by the next frame. |
| 373 | Default frame: |
| 374 | ###[ IP ]### |
| 375 | version= 4 |
| 376 | ihl= None |
| 377 | tos= 0x0 |
| 378 | len= None |
| 379 | id= 1 |
| 380 | flags= |
| 381 | frag= 0 |
| 382 | ttl= 64 |
| 383 | proto= hopopt |
| 384 | chksum= None |
| 385 | src= 127.0.0.1 |
| 386 | dst= 127.0.0.1 |
| 387 | \options\ |
| 388 | |
| 389 | Returns main.TRUE or main.FALSE on error |
| 390 | """ |
| 391 | try: |
| 392 | # Set the IP frame |
| 393 | cmd = 'ip = IP( ' |
| 394 | options = [] |
| 395 | for key, value in kwargs.iteritems(): |
| 396 | if isinstance( value, str ): |
| 397 | value = '"' + value + '"' |
| 398 | options.append( str( key ) + "=" + str( value ) ) |
| 399 | cmd += ", ".join( options ) |
| 400 | cmd += ' )' |
| 401 | self.handle.sendline( cmd ) |
| 402 | self.handle.expect( self.scapyPrompt ) |
| 403 | if "Traceback" in self.handle.before: |
| 404 | # KeyError, SyntaxError, ... |
| 405 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 406 | return main.FALSE |
| 407 | self.handle.sendline( "packet = ether/ip" ) |
| 408 | self.handle.expect( self.scapyPrompt ) |
| 409 | if "Traceback" in self.handle.before: |
| 410 | # KeyError, SyntaxError, ... |
| 411 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 412 | return main.FALSE |
| 413 | return main.TRUE |
| 414 | except pexpect.TIMEOUT: |
| 415 | main.log.exception( self.name + ": Command timed out" ) |
| 416 | return main.FALSE |
| 417 | except pexpect.EOF: |
| 418 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 419 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 420 | except Exception: |
| 421 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 422 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 423 | |
| 424 | def buildIPv6( self, **kwargs ): |
| 425 | """ |
| 426 | Build an IPv6 frame |
| 427 | |
| 428 | Will create a frame class with the given options. If a field is |
| 429 | left blank it will default to the below value unless it is |
| 430 | overwritten by the next frame. |
| 431 | Default frame: |
| 432 | ###[ IPv6 ]### |
| 433 | version= 6 |
| 434 | tc= 0 |
| 435 | fl= 0 |
| 436 | plen= None |
| 437 | nh= No Next Header |
| 438 | hlim= 64 |
| 439 | src= ::1 |
| 440 | dst= ::1 |
| 441 | |
| 442 | Returns main.TRUE or main.FALSE on error |
| 443 | """ |
| 444 | try: |
| 445 | # Set the IPv6 frame |
| 446 | cmd = 'ipv6 = IPv6( ' |
| 447 | options = [] |
| 448 | for key, value in kwargs.iteritems(): |
| 449 | if isinstance( value, str ): |
| 450 | value = '"' + value + '"' |
| 451 | options.append( str( key ) + "=" + str( value ) ) |
| 452 | cmd += ", ".join( options ) |
| 453 | cmd += ' )' |
| 454 | self.handle.sendline( cmd ) |
| 455 | self.handle.expect( self.scapyPrompt ) |
| 456 | if "Traceback" in self.handle.before: |
| 457 | # KeyError, SyntaxError, ... |
| 458 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 459 | return main.FALSE |
| 460 | self.handle.sendline( "packet = ether/ipv6" ) |
| 461 | self.handle.expect( self.scapyPrompt ) |
| 462 | if "Traceback" in self.handle.before: |
| 463 | # KeyError, SyntaxError, ... |
| 464 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 465 | return main.FALSE |
| 466 | return main.TRUE |
| 467 | except pexpect.TIMEOUT: |
| 468 | main.log.exception( self.name + ": Command timed out" ) |
| 469 | return main.FALSE |
| 470 | except pexpect.EOF: |
| 471 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 472 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 473 | except Exception: |
| 474 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 475 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 476 | |
| 477 | def buildTCP( self, ipVersion=4, **kwargs ): |
| 478 | """ |
| 479 | Build an TCP frame |
| 480 | |
| 481 | Will create a frame class with the given options. If a field is |
| 482 | left blank it will default to the below value unless it is |
| 483 | overwritten by the next frame. |
| 484 | |
| 485 | NOTE: Some arguments require quotes around them. It's up to you to |
| 486 | know which ones and to add them yourself. Arguments with an asterisk |
| 487 | do not need quotes. |
| 488 | |
| 489 | Options: |
| 490 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 491 | frame to use to encapsulate into |
| 492 | Default frame: |
| 493 | ###[ TCP ]### |
| 494 | sport= ftp_data * |
| 495 | dport= http * |
| 496 | seq= 0 |
| 497 | ack= 0 |
| 498 | dataofs= None |
| 499 | reserved= 0 |
| 500 | flags= S |
| 501 | window= 8192 |
| 502 | chksum= None |
| 503 | urgptr= 0 |
| 504 | options= {} |
| 505 | |
| 506 | Returns main.TRUE or main.FALSE on error |
| 507 | """ |
| 508 | try: |
| 509 | # Set the TCP frame |
| 510 | cmd = 'tcp = TCP( ' |
| 511 | options = [] |
| 512 | for key, value in kwargs.iteritems(): |
| 513 | options.append( str( key ) + "=" + str( value ) ) |
| 514 | cmd += ", ".join( options ) |
| 515 | cmd += ' )' |
| 516 | self.handle.sendline( cmd ) |
| 517 | self.handle.expect( self.scapyPrompt ) |
| 518 | if "Traceback" in self.handle.before: |
| 519 | # KeyError, SyntaxError, ... |
| 520 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 521 | return main.FALSE |
| 522 | if str( ipVersion ) is '4': |
| 523 | self.handle.sendline( "packet = ether/ip/tcp" ) |
| 524 | elif str( ipVersion ) is '6': |
| 525 | self.handle.sendline( "packet = ether/ipv6/tcp" ) |
| 526 | else: |
| 527 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 528 | repr( ipVersion ) ) |
| 529 | return main.FALSE |
| 530 | self.handle.expect( self.scapyPrompt ) |
| 531 | if "Traceback" in self.handle.before: |
| 532 | # KeyError, SyntaxError, ... |
| 533 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 534 | return main.FALSE |
| 535 | return main.TRUE |
| 536 | except pexpect.TIMEOUT: |
| 537 | main.log.exception( self.name + ": Command timed out" ) |
| 538 | return main.FALSE |
| 539 | except pexpect.EOF: |
| 540 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 541 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 542 | except Exception: |
| 543 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 544 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 545 | |
| 546 | def buildUDP( self, ipVersion=4, **kwargs ): |
| 547 | """ |
| 548 | Build an UDP frame |
| 549 | |
| 550 | Will create a frame class with the given options. If a field is |
| 551 | left blank it will default to the below value unless it is |
| 552 | overwritten by the next frame. |
| 553 | |
| 554 | NOTE: Some arguments require quotes around them. It's up to you to |
| 555 | know which ones and to add them yourself. Arguments with an asterisk |
| 556 | do not need quotes. |
| 557 | |
| 558 | Options: |
| 559 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 560 | frame to use to encapsulate into |
| 561 | Default frame: |
| 562 | ###[ UDP ]### |
| 563 | sport= domain * |
| 564 | dport= domain * |
| 565 | len= None |
| 566 | chksum= None |
| 567 | |
| 568 | Returns main.TRUE or main.FALSE on error |
| 569 | """ |
| 570 | try: |
| 571 | # Set the UDP frame |
| 572 | cmd = 'udp = UDP( ' |
| 573 | options = [] |
| 574 | for key, value in kwargs.iteritems(): |
| 575 | options.append( str( key ) + "=" + str( value ) ) |
| 576 | cmd += ", ".join( options ) |
| 577 | cmd += ' )' |
| 578 | self.handle.sendline( cmd ) |
| 579 | self.handle.expect( self.scapyPrompt ) |
| 580 | if "Traceback" in self.handle.before: |
| 581 | # KeyError, SyntaxError, ... |
| 582 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 583 | return main.FALSE |
| 584 | if str( ipVersion ) is '4': |
| 585 | self.handle.sendline( "packet = ether/ip/udp" ) |
| 586 | elif str( ipVersion ) is '6': |
| 587 | self.handle.sendline( "packet = ether/ipv6/udp" ) |
| 588 | else: |
| 589 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 590 | repr( ipVersion ) ) |
| 591 | return main.FALSE |
| 592 | self.handle.expect( self.scapyPrompt ) |
| 593 | if "Traceback" in self.handle.before: |
| 594 | # KeyError, SyntaxError, ... |
| 595 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 596 | return main.FALSE |
| 597 | return main.TRUE |
| 598 | except pexpect.TIMEOUT: |
| 599 | main.log.exception( self.name + ": Command timed out" ) |
| 600 | return main.FALSE |
| 601 | except pexpect.EOF: |
| 602 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 603 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 604 | except Exception: |
| 605 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 606 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 607 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 608 | def buildSCTP( self, ipVersion=4, **kwargs ): |
| 609 | """ |
| 610 | Build an SCTP frame |
| 611 | |
| 612 | Will create a frame class with the given options. If a field is |
| 613 | left blank it will default to the below value unless it is |
| 614 | overwritten by the next frame. |
| 615 | |
| 616 | NOTE: Some arguments require quotes around them. It's up to you to |
| 617 | know which ones and to add them yourself. Arguments with an asterisk |
| 618 | do not need quotes. |
| 619 | |
| 620 | Options: |
| 621 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 622 | frame to use to encapsulate into |
| 623 | Default frame: |
| 624 | ###[ SCTP ]### |
| 625 | sport= domain * |
| 626 | dport= domain * |
| 627 | tag = None |
| 628 | chksum = None |
| 629 | |
| 630 | Returns main.TRUE or main.FALSE on error |
| 631 | """ |
| 632 | try: |
| 633 | # Set the SCTP frame |
| 634 | cmd = 'sctp = SCTP( ' |
| 635 | options = [ ] |
| 636 | for key, value in kwargs.iteritems( ): |
| 637 | options.append( str( key ) + "=" + str( value ) ) |
| 638 | cmd += ", ".join( options ) |
| 639 | cmd += ' )' |
| 640 | self.handle.sendline( cmd ) |
| 641 | self.handle.expect( self.scapyPrompt ) |
| 642 | if "Traceback" in self.handle.before: |
| 643 | # KeyError, SyntaxError, ... |
| 644 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 645 | return main.FALSE |
| 646 | if str( ipVersion ) is '4': |
| 647 | self.handle.sendline( "packet = ether/ip/sctp" ) |
| 648 | elif str( ipVersion ) is '6': |
| 649 | self.handle.sendline( "packet = ether/ipv6/sctp" ) |
| 650 | else: |
| 651 | main.log.error( "Unrecognized option for ipVersion, given " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 652 | repr( ipVersion ) ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 653 | return main.FALSE |
| 654 | self.handle.expect( self.scapyPrompt ) |
| 655 | if "Traceback" in self.handle.before: |
| 656 | # KeyError, SyntaxError, ... |
| 657 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 658 | return main.FALSE |
| 659 | return main.TRUE |
| 660 | except pexpect.TIMEOUT: |
| 661 | main.log.exception( self.name + ": Command timed out" ) |
| 662 | return main.FALSE |
| 663 | except pexpect.EOF: |
| 664 | main.log.exception( self.name + ": connection closed." ) |
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 | except Exception: |
| 667 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 668 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 669 | |
| 670 | def buildARP( self, **kwargs ): |
| 671 | """ |
| 672 | Build an ARP frame |
| 673 | |
| 674 | Will create a frame class with the given options. If a field is |
| 675 | left blank it will default to the below value unless it is |
| 676 | overwritten by the next frame. |
| 677 | |
| 678 | NOTE: Some arguments require quotes around them. It's up to you to |
| 679 | know which ones and to add them yourself. Arguments with an asterisk |
| 680 | do not need quotes. |
| 681 | |
| 682 | Default frame: |
| 683 | ###[ ARP ]### |
| 684 | hwtype : XShortField = (1) |
| 685 | ptype : XShortEnumField = (2048) |
| 686 | hwlen : ByteField = (6) |
| 687 | plen : ByteField = (4) |
| 688 | op : ShortEnumField = (1) |
| 689 | hwsrc : ARPSourceMACField = (None) |
| 690 | psrc : SourceIPField = (None) |
| 691 | hwdst : MACField = ('00:00:00:00:00:00') |
| 692 | pdst : IPField = ('0.0.0.0') |
| 693 | |
| 694 | Returns main.TRUE or main.FALSE on error |
| 695 | """ |
| 696 | try: |
| 697 | # Set the ARP frame |
| 698 | cmd = 'arp = ARP( ' |
| 699 | options = [] |
| 700 | for key, value in kwargs.iteritems( ): |
| 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 ) |
| 708 | if "Traceback" in self.handle.before: |
| 709 | # KeyError, SyntaxError, ... |
| 710 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 711 | return main.FALSE |
| 712 | self.handle.sendline( "packet = ether/arp" ) |
| 713 | self.handle.expect( self.scapyPrompt ) |
| 714 | if "Traceback" in self.handle.before: |
| 715 | # KeyError, SyntaxError, ... |
| 716 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 717 | return main.FALSE |
| 718 | return main.TRUE |
| 719 | except pexpect.TIMEOUT: |
| 720 | main.log.exception( self.name + ": Command timed out" ) |
| 721 | return main.FALSE |
| 722 | except pexpect.EOF: |
| 723 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 724 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 725 | except Exception: |
| 726 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 727 | main.cleanAndExit() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 728 | |
| 729 | def buildICMP( self, ipVersion=4, **kwargs ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 730 | """ |
| 731 | Build an ICMP frame |
| 732 | |
| 733 | Will create a frame class with the given options. If a field is |
| 734 | left blank it will default to the below value unless it is |
| 735 | overwritten by the next frame. |
| 736 | Default frame: |
| 737 | ###[ ICMP ]### |
| 738 | type= echo-request |
| 739 | code= 0 |
| 740 | chksum= None |
| 741 | id= 0x0 |
| 742 | seq= 0x0 |
| 743 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 744 | Options: |
| 745 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 746 | frame to use to encapsulate into |
| 747 | |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 748 | Returns main.TRUE or main.FALSE on error |
| 749 | """ |
| 750 | try: |
| 751 | # Set the ICMP frame |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 752 | if str( ipVersion ) is '4': |
| 753 | cmd = 'icmp = ICMP( ' |
| 754 | elif str( ipVersion ) is '6': |
| 755 | cmd = 'icmp6 = ICMPv6EchoReply( ' |
| 756 | else: |
| 757 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 758 | repr( ipVersion ) ) |
| 759 | return main.FALSE |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 760 | options = [] |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 761 | for key, value in kwargs.iteritems( ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 762 | if isinstance( value, str ): |
| 763 | value = '"' + value + '"' |
| 764 | options.append( str( key ) + "=" + str( value ) ) |
| 765 | cmd += ", ".join( options ) |
| 766 | cmd += ' )' |
| 767 | self.handle.sendline( cmd ) |
| 768 | self.handle.expect( self.scapyPrompt ) |
| 769 | if "Traceback" in self.handle.before: |
| 770 | # KeyError, SyntaxError, ... |
| 771 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 772 | return main.FALSE |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 773 | |
| 774 | if str( ipVersion ) is '4': |
| 775 | self.handle.sendline( "packet = ether/ip/icmp" ) |
| 776 | elif str( ipVersion ) is '6': |
| 777 | self.handle.sendline( "packet = ether/ipv6/icmp6" ) |
| 778 | else: |
| 779 | main.log.error( "Unrecognized option for ipVersion, given " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 780 | repr( ipVersion ) ) |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 781 | return main.FALSE |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 782 | self.handle.expect( self.scapyPrompt ) |
| 783 | if "Traceback" in self.handle.before: |
| 784 | # KeyError, SyntaxError, ... |
| 785 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 786 | return main.FALSE |
| 787 | return main.TRUE |
| 788 | except pexpect.TIMEOUT: |
| 789 | main.log.exception( self.name + ": Command timed out" ) |
| 790 | return main.FALSE |
| 791 | except pexpect.EOF: |
| 792 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 793 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 794 | except Exception: |
| 795 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 796 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 797 | |
| 798 | def sendPacket( self, iface=None, packet=None, timeout=1 ): |
| 799 | """ |
| 800 | Send a packet with either the given scapy packet command, or use the |
| 801 | packet saved in the variable 'packet'. |
| 802 | |
| 803 | Examples of a valid string for packet: |
| 804 | |
| 805 | Simple IP packet |
| 806 | packet='Ether(dst="a6:d9:26:df:1d:4b")/IP(dst="10.0.0.2")' |
| 807 | |
| 808 | A Ping with two vlan tags |
| 809 | packet='Ether(dst='ff:ff:ff:ff:ff:ff')/Dot1Q(vlan=1)/Dot1Q(vlan=10)/ |
| 810 | IP(dst='255.255.255.255', src='192.168.0.1')/ICMP()' |
| 811 | |
| 812 | Returns main.TRUE or main.FALSE on error |
| 813 | """ |
| 814 | try: |
| 815 | # TODO: add all params, or use kwargs |
| 816 | sendCmd = 'srp( ' |
| 817 | if packet: |
| 818 | sendCmd += packet |
| 819 | else: |
| 820 | sendCmd += "packet" |
| 821 | if iface: |
| 822 | sendCmd += ", iface='{}'".format( iface ) |
| 823 | |
| 824 | sendCmd += ', timeout=' + str( timeout ) + ')' |
| 825 | self.handle.sendline( sendCmd ) |
| 826 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | a510a8a | 2016-05-04 15:09:28 -0700 | [diff] [blame] | 827 | # main.log.warn( "Send packet response: {}".format( self.handle.before ) ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 828 | if "Traceback" in self.handle.before: |
| 829 | # KeyError, SyntaxError, ... |
| 830 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 831 | return main.FALSE |
| 832 | # TODO: Check # of packets sent? |
| 833 | return main.TRUE |
| 834 | except pexpect.TIMEOUT: |
| 835 | main.log.exception( self.name + ": Command timed out" ) |
| 836 | return main.FALSE |
| 837 | except pexpect.EOF: |
| 838 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 839 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 840 | except Exception: |
| 841 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 842 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 843 | |
| 844 | def startFilter( self, ifaceName=None, sniffCount=1, pktFilter="ip" ): |
| 845 | """ |
| 846 | Listen for packets using the given filters |
| 847 | |
| 848 | Options: |
| 849 | ifaceName - the name of the interface to listen on. If none is given, |
| 850 | defaults to <host name>-eth0 |
| 851 | pktFilter - A string in Berkeley Packet Filter (BPF) format which |
| 852 | specifies which packets to sniff |
| 853 | sniffCount - The number of matching packets to capture before returning |
| 854 | |
| 855 | Returns main.TRUE or main.FALSE on error |
| 856 | """ |
| 857 | try: |
| 858 | # TODO: add all params, or use kwargs |
| 859 | ifaceName = str( ifaceName ) if ifaceName else self.name + "-eth0" |
| 860 | # Set interface |
| 861 | self.handle.sendline( ' conf.iface = "' + ifaceName + '"' ) |
| 862 | self.handle.expect( self.scapyPrompt ) |
| 863 | cmd = 'pkt = sniff(count = ' + str( sniffCount ) +\ |
| 864 | ', filter = "' + str( pktFilter ) + '")' |
Jon Hall | a510a8a | 2016-05-04 15:09:28 -0700 | [diff] [blame] | 865 | main.log.info( "Filter on " + self.name + ' > ' + cmd ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 866 | self.handle.sendline( cmd ) |
| 867 | self.handle.expect( '"\)\r\n' ) |
| 868 | # TODO: parse this? |
| 869 | return main.TRUE |
| 870 | except pexpect.TIMEOUT: |
| 871 | main.log.exception( self.name + ": Command timed out" ) |
| 872 | return main.FALSE |
| 873 | except pexpect.EOF: |
| 874 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 875 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 876 | except Exception: |
| 877 | main.log.exception( self.name + ": Uncaught exception!" ) |
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 | |
| 880 | def checkFilter( self, timeout=10 ): |
| 881 | """ |
| 882 | Check that a filter returned and returns the reponse |
| 883 | """ |
| 884 | try: |
| 885 | i = self.handle.expect( [ self.scapyPrompt, pexpect.TIMEOUT ], timeout=timeout ) |
| 886 | if i == 0: |
| 887 | return main.TRUE |
| 888 | else: |
| 889 | return main.FALSE |
| 890 | except pexpect.EOF: |
| 891 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 892 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 893 | except Exception: |
| 894 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 895 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 896 | |
| 897 | def killFilter( self ): |
| 898 | """ |
| 899 | Kill a scapy filter |
| 900 | """ |
| 901 | try: |
| 902 | self.handle.send( "\x03" ) # Send a ctrl-c to kill the filter |
| 903 | self.handle.expect( self.scapyPrompt ) |
| 904 | return self.handle.before |
| 905 | except pexpect.TIMEOUT: |
| 906 | main.log.exception( self.name + ": Command timed out" ) |
| 907 | return None |
| 908 | except pexpect.EOF: |
| 909 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 910 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 911 | except Exception: |
| 912 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 913 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 914 | |
| 915 | def readPackets( self ): |
| 916 | """ |
| 917 | Read all the packets captured by the previous filter |
| 918 | """ |
| 919 | try: |
| 920 | self.handle.sendline( "for p in pkt: p \n") |
| 921 | self.handle.expect( "for p in pkt: p \r\n... \r\n" ) |
| 922 | self.handle.expect( self.scapyPrompt ) |
| 923 | except pexpect.TIMEOUT: |
| 924 | main.log.exception( self.name + ": Command timed out" ) |
| 925 | return None |
| 926 | except pexpect.EOF: |
| 927 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 928 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 929 | except Exception: |
| 930 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 931 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 932 | return self.handle.before |
| 933 | |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 934 | def updateSelf( self, IPv6=False ): |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 935 | """ |
| 936 | Updates local MAC and IP fields |
| 937 | """ |
| 938 | self.hostMac = self.getMac() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 939 | if IPv6: |
| 940 | self.hostIp = self.getIp( IPv6=True ) |
| 941 | else: |
| 942 | self.hostIp = self.getIp() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 943 | |
| 944 | def getMac( self, ifaceName=None ): |
| 945 | """ |
| 946 | Save host's MAC address |
| 947 | """ |
| 948 | try: |
| 949 | ifaceName = str( ifaceName ) if ifaceName else self.name + "-eth0" |
| 950 | cmd = 'get_if_hwaddr("' + str( ifaceName ) + '")' |
| 951 | self.handle.sendline( cmd ) |
| 952 | self.handle.expect( self.scapyPrompt ) |
| 953 | pattern = r'(([0-9a-f]{2}[:-]){5}([0-9a-f]{2}))' |
| 954 | match = re.search( pattern, self.handle.before ) |
| 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 ) |
| 987 | |
| 988 | 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] | 989 | if IPv6: |
| 990 | pattern = r'(\\x([0-9]|[a-f]|[A-F])([0-9]|[a-f]|[A-F])){16}' |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 991 | match = re.search( pattern, self.handle.before ) |
| 992 | if match: |
| 993 | # 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] | 994 | if IPv6 is not True: |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 995 | if match.group() == '0.0.0.0': |
| 996 | main.log.warn( 'iface {0} has no IPv4 address'.format( ifaceName ) ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 997 | return match.group() |
| 998 | else: |
| 999 | return None |
| 1000 | try: |
| 1001 | if not ifaceName: |
| 1002 | # Get list of interfaces |
| 1003 | ifList = self.getIfList() |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 1004 | if IPv6: |
| 1005 | for ifaceName in ifList: |
| 1006 | if ifaceName == "lo": |
| 1007 | continue |
| 1008 | ip = getIPofInterface( ifaceName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1009 | if ip is not None: |
| 1010 | newip = ip |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 1011 | tmp = newip.split( "\\x" ) |
| 1012 | ip = "" |
| 1013 | counter = 0 |
| 1014 | for i in tmp: |
| 1015 | if i != "": |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1016 | counter = counter + 1 |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 1017 | if counter % 2 == 0 and counter < 16: |
| 1018 | ip = ip + i + ":" |
| 1019 | else: |
| 1020 | ip = ip + i |
| 1021 | return ip |
| 1022 | return "::" |
| 1023 | else: |
| 1024 | for ifaceName in ifList: |
| 1025 | if ifaceName == "lo": |
| 1026 | continue |
| 1027 | ip = getIPofInterface( ifaceName ) |
| 1028 | if ip != "0.0.0.0": |
| 1029 | return ip |
| 1030 | return "0.0.0.0" |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1031 | else: |
| 1032 | return getIPofInterface( ifaceName ) |
| 1033 | |
| 1034 | except pexpect.TIMEOUT: |
| 1035 | main.log.exception( self.name + ": Command timed out" ) |
| 1036 | return None |
| 1037 | except pexpect.EOF: |
| 1038 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1039 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1040 | except Exception: |
| 1041 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1042 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1043 | |
| 1044 | def getIfList( self ): |
| 1045 | """ |
| 1046 | Return List of Interfaces |
| 1047 | """ |
| 1048 | try: |
| 1049 | self.handle.sendline( 'get_if_list()' ) |
| 1050 | self.handle.expect( self.scapyPrompt ) |
| 1051 | ifList = self.handle.before.split( '\r\n' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1052 | ifList = ifList[ 1 ].replace( "'", "" )[ 1:-1 ].split( ', ' ) |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1053 | return ifList |
| 1054 | |
| 1055 | except pexpect.TIMEOUT: |
| 1056 | main.log.exception( self.name + ": Command timed out" ) |
| 1057 | return None |
| 1058 | except pexpect.EOF: |
| 1059 | main.log.exception( self.name + ": connection closed." ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1060 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1061 | except Exception: |
| 1062 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1063 | main.cleanAndExit() |
Jeremy Songster | 1f39bf0 | 2016-01-20 17:17:25 -0800 | [diff] [blame] | 1064 | |
| 1065 | if __name__ != "__main__": |
| 1066 | sys.modules[ __name__ ] = ScapyCliDriver() |