使用模糊测试库 (python)

发布于 2024-09-14 20:48:37 字数 964 浏览 7 评论 0原文

我正在尝试使用这个库: http://pastebin.com/xgPXpGtw (使用示例: http://pastebin.com/fNFAW3Fh) 我有一些问题,因为我不想像他那样将所有字节拆分为数组。

我的测试脚本看起来像这样:

import random
from random import *

def onerand(packet):
    pack  = packet[:]
    byte = str(chr(choice(range(256))))
    pack[choice(range(len(packet)))]= byte
    print "fuzzing rand byte:%s\n" % (byte.encode("hex"))
    return pack

test = "\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63"

while True:
   print onerand(test)

实际上返回:

Traceback (most recent call last):
  File "test.py", line 14, in <module> 
    print onerand(test)
  File "test.py", line 7, in onerand
    pack[choice(range(len(packet)))]= byte
TypeError: 'str' object does not support item assignment

那么我应该做什么才能在测试参数上使用该函数?

谢谢 !

I'm trying to use this library : http://pastebin.com/xgPXpGtw (an example of use: http://pastebin.com/fNFAW3Fh)
I have some issues since I dont want to split in an array all the byte as he does.

My test script looks like this:

import random
from random import *

def onerand(packet):
    pack  = packet[:]
    byte = str(chr(choice(range(256))))
    pack[choice(range(len(packet)))]= byte
    print "fuzzing rand byte:%s\n" % (byte.encode("hex"))
    return pack

test = "\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63"

while True:
   print onerand(test)

And actually returns :

Traceback (most recent call last):
  File "test.py", line 14, in <module> 
    print onerand(test)
  File "test.py", line 7, in onerand
    pack[choice(range(len(packet)))]= byte
TypeError: 'str' object does not support item assignment

So what should i do to be able to use that function on the test parameters ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-09-21 20:48:37

在 Python 中,字符串是不可变的。您向函数 onerand 传递一个字符串,参数名称 packet,复制它并给出本地名称 pack (仍然是一个字符串,因此仍然是不可变的),那么你尝试做的

pack[whatever] = byte

索引并不重要:你正在尝试修改不可变的字符串。这就是错误消息告诉您的内容,在我看来尽可能清楚:您不能这样做。

我不想将所有内容拆分为数组
字节

,那么您肯定不能使用字符串。无论如何,你对数组有什么看法? 导入数组,使用pack = array.array('c', packet)而不是pack = packet[:],永远幸福地生活after -- array.array 非常紧凑且快速,而且也是可变的!

编辑:您可以使用列表来完成此操作,如已接受的答案中所示,但这只是以相对较高的性能成本为代价。例如,考虑一下:

$ py26 -mtimeit -s's="".join([str(x)[0] for x in range(99)]); import array
> ' 'a=array.array("c",s); a[23]="b"; b=a.tostring()'
1000000 loops, best of 3: 1.09 usec per loop
$ py26 -mtimeit -s's="".join([str(x)[0] for x in range(99)]); import array
> ' 'a=list(s); a[23]="b"; b="".join(a)'
100000 loops, best of 3: 7.68 usec per loop

list 是一种比您真正需要的 array.array 更通用的结构,因此选择的速度要慢七倍以上错误的数据结构。 (Python 2.7 中的情况没那么可怕,“仅”速度慢了 4 倍以上——但是,想想你要花多少钱去买一台比你现在的机器快四倍的机器,也许你会同意,即使超速事情“仅仅”增加了 4 倍以上,而不是 7 倍以上,仍然是一个非常值得的副产品;-)。

In Python, strings are immutable. You pass to function onerand a string, argument name packet, copy it giving a local name pack (still a string, still therefore immutable), then you try to do

pack[whatever] = byte

the index doesn't matter: you're trying to modify the immutable string. That's what the error message is telling you, as clearly as possible it seems to me: you can't do that.

I dont want to split in an array all
the byte

Well you surely can't use a string, if you need to assign some of them. What do you have against arrays, anyway? import array, use pack = array.array('c', packet) instead of pack = packet[:], and live happily ever after -- an array.array is very compact and speedy, and mutable too!

Edit: you could do it with a list, as in the accepted answer, but that's only at a truly steep relative cost in performance. Consider, for example:

$ py26 -mtimeit -s's="".join([str(x)[0] for x in range(99)]); import array
> ' 'a=array.array("c",s); a[23]="b"; b=a.tostring()'
1000000 loops, best of 3: 1.09 usec per loop
$ py26 -mtimeit -s's="".join([str(x)[0] for x in range(99)]); import array
> ' 'a=list(s); a[23]="b"; b="".join(a)'
100000 loops, best of 3: 7.68 usec per loop

A list is a much more general structure than the array.array you really need here, whence the more-than-seven-times slowdown in choosing the wrong data structure. (It's less terrible in Python 2.7, "only" a 4-times-plus slowdown -- but, think how much it would cost you to buy a machine four times faster than your current one, and maybe you'll agree that even speeding things up by "just" 4+ times, instead of 7+ times, is still a well-worthwhile byproduct;-).

眼眸印温柔 2024-09-21 20:48:37

使用 pack = list(packet) 而不是 pack = packet[:],然后在结尾。

您无法替换字符串的单个字节,但可以将其转换为字符列表,替换一项,然后再转换回来。

instead of pack = packet[:], use pack = list(packet), and then return ''.join(pack) at the end.

you can't replace a single byte of a string, but you can convert it to a list of characters, replace one item, and then convert back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文