Python string 模块,printable() 实例源码
我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用string.printable()。
def strings(in_file):
min = 4
with open(in_file, "rb") as f:
result = ""
for c in f.read():
if c in string.printable:
result += c.strip()
continue
if len(result) >= min:
yield result
result = ""
if len(result) >= min: # catch result at EOF
yield result
# --------------------------------------------------------------------------------
# Function : usage
#
# Purpose : Proivde a mini help on usage of this program
#
# Parameters : None
#
# Returns : None
def __ichr(self):
addr = self.__stck.pop()
# Input Routine
while msvcrt.kbhit():
msvcrt.getwch()
while True:
char = msvcrt.getwch()
if char in '\x00\xE0':
msvcrt.getwch()
elif char in string.printable:
char = char.replace('\r', '\n')
msvcrt.putwch(char)
break
item = ord(char)
# Storing Number
self.__heap.set_(addr, item)
def test_simple_arithmetic_input_generation(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_arithmetic()
self.func_check(self.TEST_INPUT)
mutate_seq_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_four_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_walking_byte_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_four_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_8_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None, set_arith_max=127)
mutate_seq_16_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None, set_arith_max=127)
mutate_seq_32_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None, set_arith_max=127)
for i in range(len(self.TEST_MUTATION_CHECK)):
self.assertTrue(self.TEST_MUTATION_CHECK[i])
def test_seq_arithmetic_input_generation(self):
for i in range(1, self.MAX_ITERATIONS+1):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_arithmetic(i%127)
self.func_check(self.TEST_INPUT)
mutate_seq_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_four_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_walking_byte_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_four_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_8_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None, set_arith_max=(i+1)%127)
mutate_seq_16_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None, set_arith_max=(i+1)%127)
mutate_seq_32_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None, set_arith_max=(i+1)%127)
for i in range(len(self.TEST_MUTATION_CHECK)):
if(not self.TEST_MUTATION_CHECK[i]):
print(str(array('B', self.TEST_INPUT)) + " - " + str(array('B', self.TEST_MUTATIONS[i])))
self.assertTrue(self.TEST_MUTATION_CHECK[i])
def test_simple_interesting32_input_generation(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_interesting32()
self.func_check(self.TEST_INPUT)
mutate_seq_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_four_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_walking_byte_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_four_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_8_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_16_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_32_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_8_bit_interesting_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_16_bit_interesting_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_32_bit_interesting_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
for i in range(len(self.TEST_MUTATION_CHECK)):
self.assertTrue(self.TEST_MUTATION_CHECK[i])
def test_is_not_arithmetic_32(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_arithmetic_32(i%127)
for j in range(0, len(self.TEST_INPUT)-3):
value1 = (ord(self.TEST_INPUT[j]) << 24) + \
(ord(self.TEST_INPUT[j+1]) << 16) + \
(ord(self.TEST_INPUT[j+2]) << 8) + \
ord(self.TEST_INPUT[j+3])
value2 = (ord(self.TEST_MUTATIONS[j][j]) << 24) + \
(ord(self.TEST_MUTATIONS[j][j+1]) << 16) + \
(ord(self.TEST_MUTATIONS[j][j+2]) << 8) + \
ord(self.TEST_MUTATIONS[j][j+3])
swapped_value = swap_32(value2)
v1 = is_not_arithmetic(value1, value2, 4)
v2 = is_not_arithmetic(value1, swapped_value, 4)
self.assertFalse(v1 and v2)
def test_is_not_interesting_32(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_interesting32()
for j in range(len(self.TEST_INPUT)-3):
value1 = (ord(self.TEST_INPUT[j]) << 24) + \
(ord(self.TEST_INPUT[j+1]) << 16) + \
(ord(self.TEST_INPUT[j+2]) << 8) + \
ord(self.TEST_INPUT[j+3])
value2 = (ord(self.TEST_MUTATIONS[j][j]) << 24) + \
(ord(self.TEST_MUTATIONS[j][j+1]) << 16) + \
(ord(self.TEST_MUTATIONS[j][j+2]) << 8) + \
ord(self.TEST_MUTATIONS[j][j+3])
self.assertFalse(is_not_interesting(value1, value2, 4, True))
def print_nodes():
import string
print("\n".join(sorted(nodes.nodes.keys())))
used_chars = sorted(node.char for node in nodes.nodes.values())
used_chars += list("0123456789.()")
printable = string.printable[:-5]
print("".join(char*(char in used_chars)or" " for char in printable))
print(printable)
print("".join(char*(char not in used_chars)or" " for char in printable))
print(" ".join(char for char in used_chars if char not in printable and char != "\n"))
chars = {}
for node in nodes.nodes.values():
if node.char in chars:
if node.char == "":
continue
print("DUPLICATE CHARS: %r and %r"%(chars[node.char], node))
else:
chars[node.char] = node
print(chars[input("Char? ")].__name__)
sys.exit()
def validate_tor_port(tor_port, description):
'''
Validate a single Tor ORPort or DirPort entry, using description as
the port type in any log messages.
tor_port is an ORPort or DirPort config line.
Some can be IPv6 *Ports, which have an IPv6 address and a port.
Others include options, such as NoListen.
'''
# Do some basic validation of the port
# There isn't much we can do here, because port lines vary so much
if len(tor_port) < 1 or len(tor_port) > 200:
logging.warning("Bad %s length %d: %s",
description, len(tor_port), tor_port)
return False
if not all(c in string.printable for c in tor_port):
logging.warning("Bad %s characters: %s", description, tor_port)
return False
return True
def get_sample_info(vt_rep, from_vt):
'''Parse and extract sample information from JSON line
Returns a SampleInfo named tuple: md5, sha1, sha256, label_pairs
'''
label_pairs = []
if from_vt:
try:
scans = vt_rep['scans']
except KeyError:
return None
for av, res in scans.items():
if res['detected']:
label = res['result']
clean_label = filter(lambda x: x in string.printable,
label).strip().encode('utf-8').strip()
label_pairs.append((av, clean_label))
else:
label_pairs = vt_rep['av_labels']
return SampleInfo(vt_rep['md5'], vt_rep['sha1'], vt_rep['sha256'],
label_pairs)
def recognize_format(self):
for line in self.input.split('\n'):
if self.format: break
for format, rex in BytesParser.formats_compiled.items():
line = BytesParser.make_line_printable(line)
out(dbg("Trying format %s on ('%s')" % (format, line)))
if rex.match(line):
out(ok("%s has been recognized as %s formatted." % (self.name, format)))
self.format = format
break
if not self.format:
if not all(c in string.printable for c in self.input):
out(ok("%s has been recognized as RAW bytes." % (self.name)))
self.format = 'raw'
return True
else:
out(err("Could not recognize input bytes format of the %s!" % self.name))
return False
return (len(self.format) > 0)
def Decode_Name(nbname):
#From http://code.google.com/p/dpkt/ with author's permission.
try:
from string import printable
if len(nbname) != 32:
return nbname
l = []
for i in range(0, 32, 2):
l.append(chr(((ord(nbname[i]) - 0x41) << 4) | ((ord(nbname[i+1]) - 0x41) & 0xf)))
return filter(lambda x: x in printable, ''.join(l).split('\x00', 1)[0].replace(' ', ''))
except:
return "Illegal NetBIOS name"
def export_ke20k_train_maui():
'''
just use the validation dataset
:return:
'''
config = keyphrase.config.setup_keyphrase_all() # load settings.
target_dir = '/Users/memray/Project/seq2seq-keyphrase/dataset/keyphrase/baseline-data/maui/ke20k/train/'
import emolga,string
printable = set(string.printable)
validation_records = emolga.dataset.build_dataset.deserialize_from_file(config['path'] + '/dataset/keyphrase/'+config['data_process_name']+'validation_record_'+str(config['validation_size'])+'.pkl')
for r_id, r in enumerate(validation_records):
print(r_id)
r['title'] = filter(lambda x: x in printable, r['title'])
r['abstract'] = filter(lambda x: x in printable, r['abstract'])
r['keyword'] = filter(lambda x: x in printable, r['keyword'])
with open(target_dir+str(r_id)+'.txt', 'w') as textfile:
textfile.write(r['title']+'\n'+r['abstract'])
with open(target_dir + str(r_id) + '.key', 'w') as phrasefile:
for p in r['keyword'].split(';'):
phrasefile.write('%s\t1\n' % p)
def _ValidateVisiblePrintableAsciiNotReserved(value, name):
"""Checks if value is a visible printable ASCII string not starting with '!'.
Whitespace characters are excluded. Printable visible ASCII
strings starting with '!' are reserved for internal use.
Args:
value: The string to validate.
name: The name of this string; used in the exception message.
Returns:
The checked string.
Raises:
ValueError: If the string is not visible printable ASCII, or starts with
'!'.
"""
for char in value:
if char not in _VISIBLE_PRINTABLE_ASCII:
raise ValueError(
'%r must be visible printable ASCII: %r'
% (name, value))
if value.startswith('!'):
raise ValueError('%r must not start with "!": %r' % (name, value))
return value
def _ValidateVisiblePrintableAsciiNotReserved(value, name):
"""Checks if value is a visible printable ASCII string not starting with '!'.
Whitespace characters are excluded. Printable visible ASCII
strings starting with '!' are reserved for internal use.
Args:
value: The string to validate.
name: The name of this string; used in the exception message.
Returns:
The checked string.
Raises:
ValueError: If the string is not visible printable ASCII, or starts with
'!'.
"""
for char in value:
if char not in _VISIBLE_PRINTABLE_ASCII:
raise ValueError(
'%r must be visible printable ASCII: %r'
% (name, value))
if value.startswith('!'):
raise ValueError('%r must not start with "!": %r' % (name, value))
return value
def __init__(self, keys=None, buttonmasks=None, screen_shape=(1024, 728)):
self.keys = []
if keys is None:
keys = [c for c in string.printable] + list(constants.KEYMAP.keys())
for key in (keys or []):
down = vnc_event.KeyEvent.by_name(key, down=True)
up = vnc_event.KeyEvent.by_name(key, down=False)
self.keys.append(down)
self.keys.append(up)
self._key_set = set(self.keys)
self.screen_shape = screen_shape
if self.screen_shape is not None:
self.buttonmasks = []
if buttonmasks is None:
buttonmasks = range(256)
for buttonmask in buttonmasks:
self.buttonmasks.append(buttonmask)
self._buttonmask_set = set(self.buttonmasks)
def getchars(type, need_char=False):
flag = str(type)
chars = []
if type in pystrs.base_dic_type and not need_char:
if flag == pystrs.base_dic_type[0]:
chars = string.digits
elif flag == pystrs.base_dic_type[1]:
chars = string.ascii_lowercase
elif flag == pystrs.base_dic_type[2]:
chars = string.ascii_uppercase
elif flag == pystrs.base_dic_type[3]:
chars = string.printable[:36]
elif flag == pystrs.base_dic_type[4]:
chars = string.digits + string.ascii_uppercase
elif flag == pystrs.base_dic_type[5]:
chars = string.ascii_letters
elif flag == pystrs.base_dic_type[6]:
chars = string.printable[:62]
return chars
elif need_char:
return type
def clean_token(token):
token = token.replace('"', '"')
token = token.replace(''', "'")
token = token.replace(chr(int("85",16)), "...")
token = token.replace(chr(int("91",16)), "'")
token = token.replace(chr(int("92",16)), "'")
token = token.replace(chr(int("93",16)), '"')
token = token.replace(chr(int("94",16)), '"')
token = token.replace(chr(int("96",16)), '-')
if not is_printable(token):
sys.stderr.write('TOKEN NOT PRINTABLE: '+''.join([str(c) for c in token if c in string.printable ]) + '\n')
return "<UNK>"
else:
return token
def data_repr(data):
def hexdump(src, length=0x10):
lines = []
for c in xrange(0, len(src), length):
lines.append("%08x: %-*s |%s|\n" %
(c, length*3,
' '.join('%02x' % ord(x) for x in src[c:c+length]),
''.join(x if 0x20 < ord(x) < 0x7f else '.' for x in src[c:c+length]))
)
return ''.join(lines)
if all(c in string.printable for c in data):
return '\n'+data
else:
return '\n'+hexdump(data)
# STARTTLS interception code based on:
# https://github.com/ipopov/starttls-mitm
def get_dtb_model(filename, min_length=4):
""" Finds the first printable string in a file with length greater
than min_length. Replaces spaces with underscores.
"""
with open(filename, errors="ignore") as f:
result = ""
for c in f.read():
if c in string.printable:
result += c
continue
if len(result) >= min_length:
return result.replace(" ", "_")
result = ""
if len(result) >= min_length: # catch result at EOF
return result.replace(" ", "_")
return None
def strings(s, min_length=4):
strings_result = list()
result = str()
for c in s:
try:
c = chr(c)
except TypeError:
# In Python 2, c is already a chr
pass
if c in string.printable:
result += c
else:
if len(result) >= min_length:
strings_result.append(result)
result = str()
return strings_result
def serialPoller(self):
start = True
def addText(self, text):
tb = self.consoleBody.get_buffer()
tb.insert(tb.get_end_iter(), text)
while True:
if self.serialConnection:
try:
data = self.serialConnection.read()
d2 = ''
for i in data:
if i in string.printable:
d2 += i
gobject.idle_add(addText, self, d2)
except:
pass
else:
try:
self.serialConnection = serial.serial_for_url(self.serialLocation)
self.serialConnection.baudrate = self.baudrate
except:
pass
time.sleep(0.1)
def inlineSerialPoller(self):
start = True
def addText(self, text):
tb = self.serialConsoleBody.get_buffer()
tb.insert(tb.get_end_iter(), text)
while True:
if self.serialConnection:
try:
data = self.serialConnection.read()
d2 = ''
for i in data:
if i in string.printable:
d2 += i
gobject.idle_add(addText, self, d2)
except:
pass
else:
try:
self.serialConnection = serial.serial_for_url(self.serialLocation)
self.serialConnection.baudrate = self.baudrate
except:
pass
time.sleep(0.1)
def _escape(cls, iterable):
try:
while True:
ch = next(iterable)
# if it's a single backslash, then double it.
if ch == '\\':
yield '\\'
yield '\\'
# if it's a known escapable character, then return it
elif ch in cls.cmap:
yield '\\'
yield cls.cmap[ch]
# otherwise, it should be printable and doesn't need to be escaped
elif ch in string.printable:
yield ch
# we don't know, so let python handle it
else:
# FIXME: replace this repr() hack with a proper \x encoding
yield repr(ch)[1:-1]
except StopIteration: pass
def Decode_Name(nbname):
#From http://code.google.com/p/dpkt/ with author's permission.
try:
from string import printable
if len(nbname) != 32:
return nbname
l = []
for i in range(0, 32, 2):
l.append(chr(((ord(nbname[i]) - 0x41) << 4) | ((ord(nbname[i+1]) - 0x41) & 0xf)))
return filter(lambda x: x in printable, ''.join(l).split('\x00', 1)[0].replace(' ', ''))
except:
return "Illegal NetBIOS name"
def check_yara(self, data):
ret = []
if self.rules:
yarahits = self.rules.match(data=data)
if yarahits:
for hit in yarahits:
ret.append("YARA: %s" % hit.rule)
#for key, val in hit.strings.iteritems():
for (key,stringname,val) in hit.strings:
makehex = False
for char in val:
if char not in string.printable:
makehex = True
break
if makehex == True:
ret.append(" %s => %s" % (hex(key), binascii.hexlify(val)))
else:
ret.append(" %s => %s" % (hex(key), val))
return '\n'.join(ret)
def decrypt(keys, cipher_text):
"""
???????
:param keys: list(), ?? [11, 22, 33 ,44]
:param cipher_text: str(), ???? 16 ???, ?? "5b0dcfc68c8d58e9c5680e4c"
:return: str(), ??????, ?? "hello,world!"
"""
result_list = [int() for j in range(len(cipher_text) // 2)]
for i in range(0, len(cipher_text), 6):
temp = int(cipher_text[i:i + 6], 16)
for j in range(3):
temp &= (1 << 24) - 1
temp = (temp << 17) | (temp >> 7)
temp ^= keys[temp & 3] << 8
result_list[i // 2] = (temp & 0xff0000) >> 16
result_list[i // 2 + 1] = (temp & 0xff00) >> 8
result_list[i // 2 + 2] = (temp & 0xff)
# ?????, ??????, ???????????????
for j in range(3):
if chr(result_list[i // 2 + j]) not in string.printable:
return ""
return "".join([chr(each) for each in result_list])
def getCharset(num,addchar):
char = ""
charset = { '1': s.ascii_lowercase,
'2': s.ascii_uppercase,
'3': s.digits,
'4': s.hexdigits,
'5': s.punctuation,
'6': s.printable}
if num is 1:
return charset['1']
else:
num = num.split(',')
for i in num:
if 1 <= int(i) <= 6:
i= '%s' % i
char += charset[i]
else:
print "Number %s out of range." % (i)
return char+''.join(addchar)
def getInfoViaCOTP(device):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # 1 second timeout
sock.connect((device['ip_address'], 102)) ## Will setup TCP/SYN with port 102
cotpconnectresponse = hexlify(send_and_recv(sock, '03000016'+'11e00000000500c1020600c2020600c0010a'))
if not cotpconnectresponse[10:12] == 'd0':
print('COTP Connection Request failed, no route to IP '+device['ip_address']+'?')
return []
data = '720100b131000004ca0000000200000120360000011d00040000000000a1000000d3821f0000a3816900151653657276657253657373696f6e5f3742363743433341a3822100150b313a3a3a362e303a3a3a12a3822800150d4f4d532b204465627567676572a38229001500a3822a001500a3822b00048480808000a3822c001211e1a304a3822d001500a1000000d3817f0000a38169001515537562736372697074696f6e436f6e7461696e6572a2a20000000072010000'
tpktlength = str(hex((len(data)+14)/2))[2:] ## Dynamically find out the data length
cotpdata = send_and_recv(sock, '030000'+tpktlength+'02f080'+data)
## It is sure that the CPU state is NOT in this response
print('Hardware: '+cotpdata.split(';')[2])
print('Firmware: '+filter(lambda x: x in string.printable, cotpdata.split(';')[3].replace('@','.')))
sock.close()
def get_links_from(job, city, page):
'''''
@job:????
@city:???????
@page????????
@urls???????????????
****************?????????**********************
?????????
'''
urls=[]
for i in range(page):
url = "http://sou.zhaopin.com/jobs/searchresult.ashx?jl={}&kw={}&p={}".format(str(city),str(job),i)
url = quote(url, safe=string.printable)
info = get_content(url,headers)
soup = BeautifulSoup(info,"lxml")#??????“lxml”
link_urls = soup.select('td.zwmc a')
for url in link_urls:
urls.append(url.get('href'))
return (urls)
#url = "http://s.yingjiesheng.com/result.jsp?keyword=%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98&city=217&start=0&period=0&sort=score&jobtype=1"
#get_links_from('??','????', 5)
def generate_input(self, size):
return ''.join(random.choice(string.printable + '\x00') for i in range(size))
def generate_test_mutations_arithmetic(self):
self.TEST_MUTATIONS = []
for i in range(len(self.TEST_INPUT)):
value = ""
while True:
value = self.TEST_INPUT[:i] + random.choice(string.printable) + self.TEST_INPUT[i+1:]
if value != self.TEST_INPUT:
break
self.TEST_MUTATIONS.append(value)
self.TEST_MUTATION_CHECK.append(False)
def test_simple_interesting8_input_generation(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_interesting8()
self.func_check(self.TEST_INPUT)
mutate_seq_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_four_walking_bits_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_walking_byte_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_two_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_four_walking_bytes_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None)
mutate_seq_8_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_16_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_32_bit_arithmetic_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
mutate_seq_8_bit_interesting_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
#mutate_seq_16_bit_interesting_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
#mutate_seq_32_bit_interesting_array(array('B', self.TEST_INPUT), self.func_check, effector_map=None, skip_null=None)
for i in range(len(self.TEST_MUTATION_CHECK)):
if(not self.TEST_MUTATION_CHECK[i]):
print(str(array('B', self.TEST_INPUT)) + " - " + str(array('B', self.TEST_MUTATIONS[i])))
self.assertTrue(self.TEST_MUTATION_CHECK[i])
def test_is_not_arithmetic_8(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_arithmetic(i%127)
for j in range(len(self.TEST_INPUT)):
self.assertFalse(is_not_arithmetic(ord(self.TEST_INPUT[j]), ord(self.TEST_MUTATIONS[j][j]), 1, set_arith_max=(i+1)%127))
def test_is_not_arithmetic_16(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_arithmetic_16(i%127)
for j in range(len(self.TEST_INPUT)-1):
value1 = (ord(self.TEST_INPUT[j]) << 8) + ord(self.TEST_INPUT[j+1])
value2 = (ord(self.TEST_MUTATIONS[j][j]) << 8) + ord(self.TEST_MUTATIONS[j][j+1])
swapped_value = swap_16(value2)
v1 = is_not_arithmetic(value1, value2, 2, set_arith_max=(i+1)%127)
v2 = is_not_arithmetic(value1, swapped_value, 2, set_arith_max=(i+1)%127)
self.assertFalse(v1 and v2)
def test_is_not_interesting_8(self):
for i in range(self.MAX_ITERATIONS):
self.TEST_INPUT = ''.join(random.choice(string.printable + '\x00') for i in range(i))
self.TEST_MUTATIONS = []
self.TEST_MUTATION_CHECK = []
self.generate_test_mutations_seq_interesting8()
for j in range(len(self.TEST_INPUT)):
v1 = is_not_interesting(ord(self.TEST_INPUT[j]), ord(self.TEST_MUTATIONS[j][j]), 1, False)
self.assertFalse(v1 and v2)
def insert_char(self, c):
"""Given an integer character, insert that character in the current
line. Stop when the maximum line length is reached.
"""
if c not in string.printable:
return
para_idx, line_idx, char_idx = self.paragraph
line = list(self.line)
line.insert(char_idx, c)
char_idx += 1
self.line = "".join(line)
self._char_index_to_yx(para_idx, char_idx)
def insertAtCursor(self, b):
if b == '\r':
self.x = 0
elif b == '\n' or self.x >= self.width:
self.x = 0
self._scrollDown()
if b in string.printable and b not in '\r\n':
ch = (b, self._currentCharacterAttributes())
if self.modes.get(insults.modes.IRM):
self.lines[self.y][self.x:self.x] = [ch]
self.lines[self.y].pop()
else:
self.lines[self.y][self.x] = ch
self.x += 1
def keystrokeReceived(self, keyID, modifier):
m = self.keyHandlers.get(keyID)
if m is not None:
m()
elif keyID in string.printable:
self.characterReceived(keyID, False)
else:
log.msg("Received unhandled keyID: %r" % (keyID,))
def get_random_string(length):
"""
returns a random string with size length (numerical, alphabetical and some random other signs)
"""
result = ""
for _ in range(0, length):
result += random.choice(string.printable)
return result
def __init__(self, s):
self.s = s
self.printable = set(string.printable)
self.iterable = self._is_iterable()
def _printable(self):
out = filter(lambda x: x in self.printable, self.s)
return out
def _stringify_iter(self):
out = []
for i in range(self.length):
temp = filter(lambda x: x in self.printable, self.s[i])
temp = temp.encode('ascii')
out.append(temp)
return out
def generate_section_signatures(self, pe, name, sig_length=512):
"""Generates signatures for all the sections in a PE file.
If the section contains any data a signature will be created
for it. The signature name will be a combination of the
parameter 'name' and the section number and its name.
"""
section_signatures = list()
for idx, section in enumerate(pe.sections):
if section.SizeOfRawData < sig_length:
continue
#offset = pe.get_offset_from_rva(section.VirtualAddress)
offset = section.PointerToRawData
sig_name = '%s Section(%d/%d,%s)' % (
name, idx + 1, len(pe.sections),
''.join([c for c in section.Name if c in string.printable]))
section_signatures.append(
self.__generate_signature(
pe, offset, sig_name, ep_only=False,
section_start_only=True,
sig_length=sig_length) )
return '\n'.join(section_signatures)+'\n'
def __str__(self):
"""Return the escaped ASCII representation of the string."""
def convert_char(char):
if char in string.printable:
return char
else:
return r'\x%02x' % ord(char)
if self.string:
return ''.join([convert_char(c) for c in self.string])
return ''
def make_line_printable(line):
return ''.join([c if c in string.printable else '.' for c in line])
def __add__(self, other):
if type(other) == list:
return ClemencyBuffer(self._buf + other)
elif type(other) == str:
assert all(c in printable for c in other)
return self + ClemencyBuffer.from_string(other)
elif type(other) == type(self):
return ClemencyBuffer(self._buf + other._buf)
else:
assert False
def is_printable(self):
'''
Returns True if all the characters in the buffer are ASCII printable characters.
'''
return all((0 <= c <= 255) for c in self._buf)