2016-06-14 54 views
-1

我目前正在尋找Python字典的fuzzer。我已經意識到有些模糊工具,如:Fuzzer for Python字典

然而,他們似乎什麼我期待的有點寬。實際上,我的目標是爲給定的工具提供一個Python字典,並獲得一個與輸入字典非常相似的新字典,但一些值已更改。

例如,提供

{k1: "aaa", k2: "bbb", k3: "ccc"} 

我打算獲得以下新字典:

{k1: "aaj", k2: "bbb", k3: "ccc"} 
{k1: "aaa", k2: "bbr", k3: "ccc"} 
{k1: "aaa", k2: "bbb", k3: "ccp"} 
... 

你知道這樣的工具?任何建議都會受到歡迎。

在最好的情況下,我希望這是一個開源工具。

EDIT1: 我後我tryed到時刻代碼:

def change_randomly(self, v): 
    from random import randint 
    import string 

    new_v = list(v) 
    pos_value = randint(0, len(v)-1) 
    random_char = string.letters[randint(0, len(string.letters)-1)] 

    new_v[pos_value] = str(random_char) 
    return ''.join(new_v) 

可以肯定的,它可以提高,所以我期待着瞭解有關它的任何想法。

謝謝!

+0

什麼是你的字典的大小,你是什麼意思的「一些價值」?爲什麼不只是隨機改變一些值呢? –

+0

請注意,對工具/庫的請求明確偏離主題。 – jonrsharpe

+0

請求幫助來完成一段代碼來完成這個簡單的任務,通常在這裏是受歡迎的;-)這樣可以嗎,你畫了一些代碼,而其他人幫助你在哪裏卡住?正如@AbdulFatir建議/問:是否有某種原因,爲什麼在樣本中只有值字符串的第三個字符被改變,而且只是唯一的? – Dilettant

回答

0

根據意見的問題,何不乾脆寫一個固定長度的基於模板的模糊器是這樣的:

#! /usr/bin/env python 
"""Minimal template based dict string value fuzzer.""" 
from __future__ import print_function 

import random 
import string 


def random_string(rng, length, chars=string.printable): 
    """A random string with given length.""" 
    return ''.join(rng.choice(chars) for _ in range(length)) 


def dict_string_template_fuzz_gen(rng, dict_in): 
    """Given a random number generator rng, and starting from 
    template dict_in expected to have only strings as values, 
    this generator function yields derived dicts with random 
    variations in the string values keeping the length of 
    those identical.""" 

    while True: 
     yield dict((k, random_string(rng, len(v))) for k, v in dict_in.items()) 


def main(): 
    """Drive a test run of minimal template fuzz.""" 

    k1, k2, k3 = 'ka', 'kb', 'kc' 
    template = {k1: "aaa", k2: "bbb", k3: "ccc"} 

    print("# Input(template):") 
    print(template) 

    rng = random.SystemRandom() 
    print("# Output(fuzz):") 
    for n, fuzz in enumerate(dict_string_template_fuzz_gen(rng, 
          template), start=0): 
     print(fuzz) 
     if n > 3: 
      break 

if __name__ == '__main__': 
    main() 

在用例輸入可能產生這樣的:

# Input(template): 
{'kc': 'ccc', 'kb': 'bbb', 'ka': 'aaa'} 
# Output(fuzz): 
{'kc': '6HZ', 'kb': 'zoD', 'ka': '5>b'} 
{'kc': '%<\r', 'kb': 'g>v', 'ka': 'Mo0'} 
{'kc': 'Y $', 'kb': '4z.', 'ka': '0".'} 
{'kc': '^M.', 'kb': 'QY1', 'ka': 'P0)'} 
{'kc': 'FK4', 'kb': 'oZW', 'ka': 'G1q'} 

所以這應該給OP一些啓動,因爲它可能是一個引導問題,在那裏Python的知識才剛剛開始......

我剛剛砍了它--PEP8兼容 - 無論是Python v2還是v3,它都應該可以工作。

許多開放的目標......但應該得到一個去評估,如果一個庫或一些簡單的增強編碼可能就足夠了。只有OP會知道但歡迎對這個答案建議發表評論或更新問題。提示:我幾乎總是使用SystemRandom,因此您可以更穩健地進行並行處理。可能有更快的方法,但是在規範中我看不到性能。這些印刷品當然是被捲入的,因爲這是最好的教育。 HTH

更新: 讀了OP的評論只改變琴絃的一部分,保留一定的相似性,人們可以通過例如交換上述模糊器功能:

def dict_string_template_fuzz_len_gen(rng, dict_in, f_len=1): 
    """Given a random number generator rng, and starting from 
    template dict_in expected to have only strings as values, 
    this generator function yields derived dicts with random 
    variations in the string values keeping the length of 
    those identical. 
    Added as hack the f_len parameter that counts the 
    characters open to be fuzzed from the end of the string.""" 

    r_s = random_string # shorten for line readability below 
    while True: 
     yield dict(
      (k, v[:f_len + 1] + r_s(rng, f_len)) for k, v in dict_in.items()) 

然後具有如輸出樣本:

# Input(template): 
{'kc': 'ccc', 'kb': 'bbb', 'ka': 'aaa'} 
# Output(fuzz): 
{'kc': 'cc\t', 'kb': 'bbd', 'ka': 'aa\\'} 
{'kc': 'cc&', 'kb': 'bbt', 'ka': 'aa\\'} 
{'kc': 'ccg', 'kb': 'bb_', 'ka': 'aaJ'} 
{'kc': 'ccc', 'kb': 'bbv', 'ka': 'aau'} 
{'kc': 'ccw', 'kb': 'bbs', 'ka': "aa'"} 

當調用此函數,而不是其它。