def __iter__(self): def read_chunk_header(ammo_file): chunk_header = '' while chunk_header is '': line = ammo_file.readline() if line.startswith('['): self.headers.update(_parse_header(line.strip('\r\n[]\t '))) elif line is '': return line else: chunk_header = line.strip('\r\n') return chunk_header opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length # if we got StopIteration here, the file is empty chunk_header = read_chunk_header(ammo_file) while chunk_header: if chunk_header is not '': try: fields = chunk_header.split() chunk_size = int(fields[0]) if chunk_size == 0: self.log.debug( 'Zero-sized chunk in ammo file at %s. Starting over.' % ammo_file.tell()) ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) continue uri = fields[1] marker = fields[2] if len(fields) > 2 else None missile = ammo_file.read(chunk_size) if len(missile) < chunk_size: raise AmmoFileError( "Unexpected end of file: read %s bytes instead of %s" % (len(missile), chunk_size)) yield (HttpAmmo( uri=uri, headers=[ ': '.join(header) for header in self.headers.items() ], method='POST', body=missile, http_ver=self.http_ver, ).to_s(), marker) except (IndexError, ValueError) as e: raise AmmoFileError( "Error while reading ammo file. Position: %s, header: '%s', original exception: %s" % (ammo_file.tell(), chunk_header, e)) chunk_header = read_chunk_header(ammo_file) if chunk_header == '': self.log.debug( 'Reached the end of ammo file. Starting over.') ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) info.status.af_position = ammo_file.tell()
def get_google_authtoken(appname, email_address, password): """ Make secure connection to Google Accounts and retrieve an authorisation token for the stated appname. The token can be send to the login front-end at appengine using get_gae_cookie(), which will return a cookie to use for the user session. """ opener = get_opener() # get an AuthToken from Google accounts auth_uri = 'https://www.google.com/accounts/ClientLogin' authreq_data = urllib.urlencode({ "Email": email_address, "Passwd": password, "service": "ah", "source": appname, "accountType": "HOSTED_OR_GOOGLE" }) req = urllib2.Request(auth_uri, data=authreq_data) try: response = opener.open(req) response_body = response.read() response_dict = dict(x.split("=") for x in response_body.split("\n") if x) return response_dict["Auth"] except urllib2.HTTPError, e: if e.code == 403: body = e.read() response_dict = dict(x.split("=", 1) for x in body.split("\n") if x) raise AuthError(req.get_full_url(), e.code, e.msg, e.headers, response_dict) else: raise
def __iter__(self): opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length while True: for line in ammo_file: info.status.af_position = ammo_file.tell() try: request = line.split('"')[1] method, uri, proto = request.split() http_ver = proto.split('/')[1] if method == "GET": yield ( HttpAmmo( uri, headers=self.headers, http_ver=http_ver, ).to_s(), None) else: self.warn( "Skipped line: %s (unsupported method)" % line) except (ValueError, IndexError), e: self.warn("Skipped line: %s (%s)" % (line, e)) ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length while True: for line in ammo_file: info.status.af_position = ammo_file.tell() try: request = line.split('"')[1] method, uri, proto = request.split() http_ver = proto.split('/')[1] if method == "GET": yield ( HttpAmmo( uri, headers=self.headers, http_ver=http_ver, ).to_s(), None) else: self.warn("Skipped line: %s (unsupported method)" % line) except (ValueError, IndexError), e: self.warn("Skipped line: %s (%s)" % (line, e)) ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length while True: for line in ammo_file: info.status.af_position = ammo_file.tell() if line.startswith('['): self.headers.add(line.strip('\r\n[]\t ')) elif len(line.rstrip('\r\n')): fields = line.split() uri = fields[0] if len(fields) > 1: marker = fields[1] else: marker = None yield ( HttpAmmo( uri, headers=self.headers, http_ver=self.http_ver, ).to_s(), marker) if info.status.ammo_count == 0: self.log.error("No ammo in uri-style file") raise AmmoFileError("No ammo! Cover me!") ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length while True: for line in ammo_file: info.status.af_position = ammo_file.tell() if line.startswith('['): self.headers.update( _parse_header(line.strip('\r\n[]\t '))) elif len(line.rstrip('\r\n')): fields = line.split() uri = fields[0] if len(fields) > 1: marker = fields[1] else: marker = None yield ( HttpAmmo( uri, headers=[ ': '.join(header) for header in self.headers.items()], http_ver=self.http_ver, ).to_s(), marker) if info.status.ammo_count == 0: self.log.error("No ammo in uri-style file") raise AmmoFileError("No ammo! Cover me!") ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): with get_opener(self.filename)(self.filename, 'rb') as ammo_file: while True: for line in ammo_file: info.status.af_position = ammo_file.tell() request = line.split('"')[1] method, uri, proto = request.split() http_ver = proto.split('/')[1] if method == "GET": yield (HttpAmmo( uri, headers=self.headers, http_ver=http_ver, ).to_s(), None) else: if not self.warned: self.warned = True self.log.warning( "There are some skipped lines. See full log for details." ) self.log.debug( "Skipped line: %s (unsupported method)" % line) ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __get_stpd_filename(self): ''' Choose the name for stepped data file ''' if self.use_caching: sep = "|" hasher = hashlib.md5() hashed_str = "cache version 5" + sep + \ ';'.join(self.instances_schedule) + sep + str(self.loop_limit) hashed_str += sep + str(self.ammo_limit) + sep + ';'.join( self.rps_schedule) + sep + str(self.autocases) hashed_str += sep + \ ";".join(self.uris) + sep + ";".join( self.headers) + sep + self.http_ver + sep + ";".join(self.chosen_cases) hashed_str += sep + str(self.enum_ammo) if self.instances_schedule: hashed_str += sep + str(self.instances) if self.ammo_file: opener = get_opener(self.ammo_file) hashed_str += sep + opener.hash else: if not self.uris: raise RuntimeError( "Neither ammofile nor uris specified") hashed_str += sep + \ ';'.join(self.uris) + sep + ';'.join(self.headers) self.log.debug("stpd-hash source: %s", hashed_str) hasher.update(hashed_str) if not os.path.exists(self.cache_dir): os.makedirs(self.cache_dir) stpd = self.cache_dir + '/' + \ os.path.basename(self.ammo_file) + \ "_" + hasher.hexdigest() + ".stpd" else: stpd = os.path.realpath("ammo.stpd") self.log.debug("Generated cache file name: %s", stpd) return stpd
def __get_stpd_filename(self): ''' Choose the name for stepped data file ''' if self.use_caching: sep = "|" hasher = hashlib.md5() hashed_str = "cache version 5" + sep + \ ';'.join(self.instances_schedule) + sep + str(self.loop_limit) hashed_str += sep + str(self.ammo_limit) + sep + ';'.join( self.rps_schedule) + sep + str(self.autocases) hashed_str += sep + \ ";".join(self.uris) + sep + ";".join( self.headers) + sep + self.http_ver + sep + ";".join(self.chosen_cases) hashed_str += sep + str(self.enum_ammo) if self.instances_schedule: hashed_str += sep + str(self.instances) if self.ammo_file: opener = get_opener(self.ammo_file) hashed_str += sep + opener.hash else: if not self.uris: raise RuntimeError("Neither ammofile nor uris specified") hashed_str += sep + \ ';'.join(self.uris) + sep + ';'.join(self.headers) self.log.debug("stpd-hash source: %s", hashed_str) hasher.update(hashed_str) if not os.path.exists(self.cache_dir): os.makedirs(self.cache_dir) stpd = self.cache_dir + '/' + \ os.path.basename(self.ammo_file) + \ "_" + hasher.hexdigest() + ".stpd" else: stpd = os.path.realpath("ammo.stpd") self.log.debug("Generated cache file name: %s", stpd) return stpd
def __iter__(self): with get_opener(self.filename)(self.filename, 'rb') as ammo_file: while True: for line in ammo_file: info.status.af_position = ammo_file.tell() if line.startswith('['): self.headers.add(line.strip('\r\n[]\t ')) elif len(line.rstrip('\r\n')): fields = line.split() uri = fields[0] if len(fields) > 1: marker = fields[1] else: marker = None yield ( HttpAmmo( uri, headers=self.headers, http_ver=self.http_ver, ).to_s(), marker) if info.status.ammo_count == 0: self.log.error("No ammo in uri-style file") raise AmmoFileError("No ammo! Cover me!") ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): def read_chunk_header(ammo_file): chunk_header = '' while chunk_header is '': line = ammo_file.readline() if line.startswith('['): self.headers.update( _parse_header(line.strip('\r\n[]\t '))) elif line is '': return line else: chunk_header = line.strip('\r\n') return chunk_header opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length # if we got StopIteration here, the file is empty chunk_header = read_chunk_header(ammo_file) while chunk_header: if chunk_header is not '': try: fields = chunk_header.split() chunk_size = int(fields[0]) if chunk_size == 0: self.log.debug( 'Zero-sized chunk in ammo file at %s. Starting over.' % ammo_file.tell()) ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) continue uri = fields[1] marker = fields[2] if len(fields) > 2 else None missile = ammo_file.read(chunk_size) if len(missile) < chunk_size: raise AmmoFileError( "Unexpected end of file: read %s bytes instead of %s" % (len(missile), chunk_size)) yield ( HttpAmmo( uri=uri, headers=[ ': '.join(header) for header in self.headers.items()], method='POST', body=missile, http_ver=self.http_ver, ).to_s(), marker ) except (IndexError, ValueError) as e: raise AmmoFileError( "Error while reading ammo file. Position: %s, header: '%s', original exception: %s" % (ammo_file.tell(), chunk_header, e)) chunk_header = read_chunk_header(ammo_file) if chunk_header == '': self.log.debug( 'Reached the end of ammo file. Starting over.') ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) info.status.af_position = ammo_file.tell()
def __iter__(self): with get_opener(self.filename)(self.filename, 'rb') as ammo_file: while True: for line in ammo_file: info.status.af_position = ammo_file.tell() yield (line.rstrip('\r\n'), None) ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): opener = get_opener(self.filename) with opener(self.filename, "rb") as ammo_file: info.status.af_size = opener.data_length while True: for line in ammo_file: info.status.af_position = ammo_file.tell() yield (line.rstrip("\r\n"), None) ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): def read_chunk_header(ammo_file): chunk_header = "" while chunk_header is "": line = ammo_file.readline() if line.startswith("["): self.headers.add(line.strip("\r\n[]\t ")) elif line is "": return line else: chunk_header = line.strip("\r\n") return chunk_header opener = get_opener(self.filename) with opener(self.filename, "rb") as ammo_file: info.status.af_size = opener.data_length chunk_header = read_chunk_header(ammo_file) # if we got StopIteration here, the file is empty while chunk_header: if chunk_header is not "": try: fields = chunk_header.split() chunk_size = int(fields[0]) if chunk_size == 0: self.log.debug("Zero-sized chunk in ammo file at %s. Starting over." % ammo_file.tell()) ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) continue uri = fields[1] marker = fields[2] if len(fields) > 2 else None missile = ammo_file.read(chunk_size) if len(missile) < chunk_size: raise AmmoFileError( "Unexpected end of file: read %s bytes instead of %s" % (len(missile), chunk_size) ) yield ( HttpAmmo( uri=uri, headers=self.headers, method="POST", body=missile, http_ver=self.http_ver ).to_s(), marker, ) except (IndexError, ValueError) as e: raise AmmoFileError( "Error while reading ammo file. Position: %s, header: '%s', original exception: %s" % (ammo_file.tell(), chunk_header, e) ) chunk_header = read_chunk_header(ammo_file) if chunk_header == "": self.log.debug("Reached the end of ammo file. Starting over.") ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) info.status.af_position = ammo_file.tell()
def get_ammo_generator(self): """ return ammo generator """ af_readers = { 'phantom': missile.AmmoFileReader, 'slowlog': missile.SlowLogReader, 'line': missile.LineReader, 'uri': missile.UriReader, 'uripost': missile.UriPostReader, 'access': missile.AccessLogReader, } if self.uris and self.ammo_file: raise StepperConfigurationError( 'Both uris and ammo file specified. You must specify only one of them' ) elif self.uris: ammo_gen = missile.UriStyleGenerator(self.uris, self.headers, http_ver=self.http_ver) elif self.ammo_file: if self.ammo_type in af_readers: if self.ammo_type is 'phantom': with get_opener(self.ammo_file)(self.ammo_file, 'rb') as ammo: try: if not ammo.next()[0].isdigit(): self.ammo_type = 'uri' self.log.info( "Setting ammo_type 'uri' because ammo is not started with digit and you did not specify ammo format" ) else: self.log.info( "Default ammo type ('phantom') used, use 'phantom.ammo_type' option to override it" ) except StopIteration, e: self.log( "Couldn't read first line of ammo file: %s" % e) raise AmmoFileError( "Couldn't read first line of ammo file") else: raise NotImplementedError( 'No such ammo type implemented: "%s"' % self.ammo_type) ammo_gen = af_readers[self.ammo_type](self.ammo_file, headers=self.headers, http_ver=self.http_ver)
def __iter__(self): opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length request = "" while True: for line in ammo_file: info.status.af_position = ammo_file.tell() if line.startswith('#'): if request != "": yield (request, None) request = "" else: request += line ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): def read_chunk_header(ammo_file): chunk_header = '' while chunk_header is '': line = ammo_file.readline() if line is '': return line chunk_header = line.strip('\r\n') return chunk_header opener = get_opener(self.filename) with opener(self.filename, 'rb') as ammo_file: info.status.af_size = opener.data_length # if we got StopIteration here, the file is empty chunk_header = read_chunk_header(ammo_file) while chunk_header: if chunk_header is not '': try: fields = chunk_header.split() chunk_size = int(fields[0]) if chunk_size == 0: if info.status.loop_count == 0: self.log.info( 'Zero-sized chunk in ammo file at %s. Starting over.' % ammo_file.tell()) ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) continue marker = fields[1] if len(fields) > 1 else None missile = ammo_file.read(chunk_size) if len(missile) < chunk_size: raise AmmoFileError( "Unexpected end of file: read %s bytes instead of %s" % (len(missile), chunk_size)) yield (missile, marker) except (IndexError, ValueError) as e: raise AmmoFileError( "Error while reading ammo file. Position: %s, header: '%s', original exception: %s" % (ammo_file.tell(), chunk_header, e)) chunk_header = read_chunk_header(ammo_file) if chunk_header == '': ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) info.status.af_position = ammo_file.tell()
def get_ammo_generator(self): """ return ammo generator """ af_readers = { 'phantom': missile.AmmoFileReader, 'slowlog': missile.SlowLogReader, 'line': missile.LineReader, 'uri': missile.UriReader, 'uripost': missile.UriPostReader, 'access': missile.AccessLogReader, } if self.uris and self.ammo_file: raise StepperConfigurationError( 'Both uris and ammo file specified. You must specify only one of them') elif self.uris: ammo_gen = missile.UriStyleGenerator( self.uris, self.headers, http_ver=self.http_ver ) elif self.ammo_file: if self.ammo_type in af_readers: if self.ammo_type is 'phantom': with get_opener(self.ammo_file)(self.ammo_file, 'rb') as ammo: try: if not ammo.next()[0].isdigit(): self.ammo_type = 'uri' self.log.info( "Setting ammo_type 'uri' because ammo is not started with digit and you did not specify ammo format") else: self.log.info( "Default ammo type ('phantom') used, use 'phantom.ammo_type' option to override it") except StopIteration, e: self.log.exception( "Couldn't read first line of ammo file") raise AmmoFileError( "Couldn't read first line of ammo file") else: raise NotImplementedError( 'No such ammo type implemented: "%s"' % self.ammo_type) ammo_gen = af_readers[self.ammo_type]( self.ammo_file, headers=self.headers, http_ver=self.http_ver )
def __iter__(self): opener = get_opener(self.filename) with opener(self.filename, "rb") as ammo_file: info.status.af_size = opener.data_length request = "" while True: for line in ammo_file: info.status.af_position = ammo_file.tell() if line.startswith("#"): if request != "": yield (request, None) request = "" else: request += line ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def get_ammo_generator(self): """ return ammo generator """ af_readers = { 'phantom': missile.AmmoFileReader, 'slowlog': missile.SlowLogReader, 'line': missile.LineReader, 'uri': missile.UriReader, 'uripost': missile.UriPostReader, 'access': missile.AccessLogReader, } if self.uris and self.ammo_file: raise StepperConfigurationError( 'Both uris and ammo file specified. You must specify only one of them' ) elif self.uris: ammo_gen = missile.UriStyleGenerator(self.uris, self.headers, http_ver=self.http_ver) elif self.ammo_file: if self.ammo_type in af_readers: if self.ammo_type is 'phantom': with get_opener(self.ammo_file)(self.ammo_file, 'rb') as ammo: if not ammo.next()[0].isdigit(): self.ammo_type = 'uri' self.log.info( "Setting ammo_type 'uri' because ammo is not started with digit and you did non specify ammo format." ) else: self.log.info( "I believe ammo_type is 'phantom' cause you did not specify it." ) else: raise NotImplementedError( 'No such ammo type implemented: "%s"' % self.ammo_type) ammo_gen = af_readers[self.ammo_type](self.ammo_file, headers=self.headers, http_ver=self.http_ver) else: raise StepperConfigurationError( 'Ammo not found. Specify uris or ammo file') return ammo_gen
def get_ammo_generator(self): """ return ammo generator """ af_readers = { 'phantom': missile.AmmoFileReader, 'slowlog': missile.SlowLogReader, 'line': missile.LineReader, 'uri': missile.UriReader, 'uripost': missile.UriPostReader, 'access': missile.AccessLogReader, } if self.uris and self.ammo_file: raise StepperConfigurationError( 'Both uris and ammo file specified. You must specify only one of them') elif self.uris: ammo_gen = missile.UriStyleGenerator( self.uris, self.headers, http_ver=self.http_ver ) elif self.ammo_file: if self.ammo_type in af_readers: if self.ammo_type is 'phantom': with get_opener(self.ammo_file)(self.ammo_file, 'rb') as ammo: if not ammo.next()[0].isdigit(): self.ammo_type = 'uri' self.log.info( "Setting ammo_type 'uri' because ammo is not started with digit and you did non specify ammo format.") else: self.log.info( "I believe ammo_type is 'phantom' cause you did not specify it.") else: raise NotImplementedError( 'No such ammo type implemented: "%s"' % self.ammo_type) ammo_gen = af_readers[self.ammo_type]( self.ammo_file, headers=self.headers, http_ver=self.http_ver ) else: raise StepperConfigurationError( 'Ammo not found. Specify uris or ammo file') return ammo_gen
def __iter__(self): def read_chunk_header(ammo_file): chunk_header = "" while chunk_header is "": line = ammo_file.readline() if line is "": return line chunk_header = line.strip("\r\n") return chunk_header opener = get_opener(self.filename) with opener(self.filename, "rb") as ammo_file: info.status.af_size = opener.data_length chunk_header = read_chunk_header(ammo_file) # if we got StopIteration here, the file is empty while chunk_header: if chunk_header is not "": try: fields = chunk_header.split() chunk_size = int(fields[0]) if chunk_size == 0: if info.status.loop_count == 0: self.log.info("Zero-sized chunk in ammo file at %s. Starting over." % ammo_file.tell()) ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) continue marker = fields[1] if len(fields) > 1 else None missile = ammo_file.read(chunk_size) if len(missile) < chunk_size: raise AmmoFileError( "Unexpected end of file: read %s bytes instead of %s" % (len(missile), chunk_size) ) yield (missile, marker) except (IndexError, ValueError) as e: raise AmmoFileError( "Error while reading ammo file. Position: %s, header: '%s', original exception: %s" % (ammo_file.tell(), chunk_header, e) ) chunk_header = read_chunk_header(ammo_file) if chunk_header == "": ammo_file.seek(0) info.status.inc_loop_count() chunk_header = read_chunk_header(ammo_file) info.status.af_position = ammo_file.tell()
def get_gae_cookie(appname, auth_token): """ Send a token to the App Engine login, again stating the name of the application to gain authentication for. Returned is a cookie that may be used to authenticate HTTP traffic to the application at App Engine. """ continue_location = "http://localhost/" continue_location = "http://%s.appspot.com/" % appname args = {"continue": continue_location, "auth": auth_token} host = "%s.appspot.com" % appname url = "http://%s/_ah/login?%s" % (host, urllib.urlencode(args)) opener = get_opener() # no redirect handler! req = urllib2.Request(url) try: response = opener.open(req) except urllib2.HTTPError, e: response = e
def __iter__(self): with get_opener(self.filename)(self.filename, 'rb') as ammo_file: while True: for line in ammo_file: info.status.af_position = ammo_file.tell() if line.startswith('['): self.headers.add(line.strip('\r\n[]\t ')) elif len(line.rstrip('\r\n')): fields = line.split() uri = fields[0] if len(fields) > 1: marker = fields[1] else: marker = None yield (HttpAmmo( uri, headers=self.headers, http_ver=self.http_ver, ).to_s(), marker) ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()
def __iter__(self): with get_opener(self.filename)(self.filename, 'rb') as ammo_file: while True: for line in ammo_file: info.status.af_position = ammo_file.tell() request = line.split('"')[1] method, uri, proto = request.split() http_ver = proto.split('/')[1] if method == "GET": yield ( HttpAmmo( uri, headers=self.headers, http_ver=http_ver, ).to_s(), None) else: if not self.warned: self.warned = True self.log.warning("There are some skipped lines. See full log for details.") self.log.debug("Skipped line: %s (unsupported method)" % line) ammo_file.seek(0) info.status.af_position = 0 info.status.inc_loop_count()