<s> import re <EOL> import urllib <EOL> import ast <EOL> from tornado . httputil import HTTPHeaders <EOL> from framework . http . wafbypasser . core . template_parser import TemplateParser <EOL> class PlaceholderManager : <EOL> def __init__ ( self , fuzzing_signature ) : <EOL> self . sig = fuzzing_signature <EOL> self . lsig = self . sig + "<STR_LIT>" + self . sig <EOL> self . fsig = self . sig + "<STR_LIT>" + self . sig <EOL> self . template_signature_re = self . sig <EOL> self . template_signature_re += "<STR_LIT>" + self . sig + "<STR_LIT>" + self . sig <EOL> def template_signature ( self , string ) : <EOL> ret = re . search ( self . template_signature_re , string ) <EOL> if ret : <EOL> return ret . group ( <NUM_LIT:0> ) <EOL> return False <EOL> @ staticmethod <EOL> def get_placeholder_number ( template_signature_re , string ) : <EOL> ret = re . compile ( template_signature_re ) <EOL> if ret : <EOL> return len ( ret . findall ( string ) ) <EOL> return <NUM_LIT:0> <EOL> def replace_url ( self , url , payload ) : <EOL> if self . fsig in url : <EOL> return url . replace ( self . fsig , urllib . quote_plus ( payload ) ) <EOL> template_sig = self . template_signature ( url ) <EOL> if template_sig : <EOL> tp = TemplateParser ( ) <EOL> tp . set_payload ( payload ) <EOL> new_payload = repr ( <EOL> tp . transform ( self . template_signature ( url ) , <EOL> self . sig ) ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return url . replace ( template_sig , new_payload ) <EOL> return url <EOL> def replace_header ( self , headers , payload ) : <EOL> raw_headers = str ( headers ) <EOL> if self . fsig in raw_headers : <EOL> raw_headers = raw_headers . replace ( self . fsig , payload ) <EOL> return HTTPHeaders ( ast . literal_eval ( raw_headers ) ) <EOL> template_sig = self . template_signature ( raw_headers ) <EOL> if template_sig : <EOL> tp = TemplateParser ( ) <EOL> tp . set_payload ( payload ) <EOL> header_template = self . template_signature ( raw_headers ) <EOL> new_payload = repr ( <EOL> tp . transform ( header_template , <EOL> self . sig ) ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> raw_headers = raw_headers . replace ( header_template , new_payload ) <EOL> new_headers = HTTPHeaders ( ast . literal_eval ( raw_headers ) ) <EOL> return new_headers <EOL> return headers <EOL> def replace_body ( self , body , payload ) : <EOL> if body is None : <EOL> return body <EOL> if self . fsig in body : <EOL> return body . replace ( self . fsig , urllib . quote_plus ( payload ) ) <EOL> template_sig = self . template_signature ( body ) <EOL> if template_sig : <EOL> tp = TemplateParser ( ) <EOL> tp . set_payload ( payload ) <EOL> new_payload = repr ( <EOL> tp . transform ( self . template_signature ( body ) , <EOL> self . sig ) ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return body . replace ( template_sig , new_payload ) <EOL> return body <EOL> def transformed_http_requests ( self , http_helper , methods , url , payloads , <EOL> headers = None , body = None ) : <EOL> """<STR_LIT>""" <EOL> requests = [ ] <EOL> for method in methods : <EOL> for payload in payloads : <EOL> new_url = self . replace_url ( url , payload ) <EOL> new_headers = self . replace_header ( headers , payload ) <EOL> new_body = self . replace_body ( body , payload ) <EOL> request = http_helper . create_http_request ( method , <EOL> new_url , <EOL> new_body , <EOL> new_headers , <EOL> payload ) <EOL> requests . append ( request ) <EOL> return requests </s>
