使用模糊测试库 (python)
我正在尝试使用这个库: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Python 中,字符串是不可变的。您向函数
onerand
传递一个字符串,参数名称packet
,复制它并给出本地名称pack
(仍然是一个字符串,因此仍然是不可变的),那么你尝试做的索引并不重要:你正在尝试修改不可变的字符串。这就是错误消息告诉您的内容,在我看来尽可能清楚:您不能这样做。
,那么您肯定不能使用字符串。无论如何,你对数组有什么看法?
导入数组
,使用pack = array.array('c', packet)
而不是pack = packet[:]
,永远幸福地生活after --array.array
非常紧凑且快速,而且也是可变的!编辑:您可以使用列表来完成此操作,如已接受的答案中所示,但这只是以相对较高的性能成本为代价。例如,考虑一下:
list
是一种比您真正需要的array.array
更通用的结构,因此选择的速度要慢七倍以上错误的数据结构。 (Python 2.7 中的情况没那么可怕,“仅”速度慢了 4 倍以上——但是,想想你要花多少钱去买一台比你现在的机器快四倍的机器,也许你会同意,即使超速事情“仅仅”增加了 4 倍以上,而不是 7 倍以上,仍然是一个非常值得的副产品;-)。In Python, strings are immutable. You pass to function
onerand
a string, argument namepacket
, copy it giving a local namepack
(still a string, still therefore immutable), then you try to dothe 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.
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
, usepack = array.array('c', packet)
instead ofpack = packet[:]
, and live happily ever after -- anarray.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:
A
list
is a much more general structure than thearray.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;-).使用
pack = list(packet)
而不是pack = packet[:]
,然后在结尾。您无法替换字符串的单个字节,但可以将其转换为字符列表,替换一项,然后再转换回来。
instead of
pack = packet[:]
, usepack = list(packet)
, and thenreturn ''.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.