def generate_input(self, size):
return ''.join(random.choice(string.printable + '\x00') for i in range(size))
python类printable()的实例源码
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)
def to_printable_string(self):
'''
If this buffer is printable, returns a string version of it. Otherwise fails.
'''
assert self.is_printable(), "Buffer %s is not printable" % self._buf
return ''.join(chr(c) for c in self._buf)