简体   繁体   English

如何在python中遍历字节的每个可能值?

[英]How do I loop through every possible value of a byte in python?

I am conducting a Padding Oracle Attack for my Information Security course. 我正在为我的信息安全课程进行Padding Oracle Attack。 Without getting into the details of the attack, I need to have a for loop that loops through all possible 1 byte hex values. 在不深入探讨攻击细节的情况下,我需要有一个for循环,该循环遍历所有可能的1字节十六进制值。

Pseudo-code of what I need to do: 我需要做的伪代码:

for x in range('\x00', '\xFF'):
    replace last byte of ciphertext with byte
    perform padding check

I cannot figure out how to accomplish this. 我不知道该怎么做。 Any ideas? 有任何想法吗?

Bytes are really just integers in the range 0-255 (inclusive), or in hex, 0 through to FF. 字节实际上只是0-255(含)范围内的整数,或十六进制的0到FF。 Generate the integer, then create the byte value from that. 生成整数,然后从中创建字节值。 The bytes() type takes a list of integers, so create a list of length 1: bytes()类型采用整数列表,因此创建一个长度为1的列表:

for i in range(0xff):
    b = bytes([i])

If you store the ciphertext in a bytearray() object , you could even trivially replace that last byte by using the integer directly: 如果将密文存储在bytearray()对象中 ,则甚至可以直接使用整数来简单地替换最后一个字节:

ciphertext_mutable = bytearray(ciphertext)
for i in range(0xff):
    ciphertext_mutable[-1] = i  # replace last byte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python遍历第4行中的第4个像素? - How do I loop through every 4th pixel in every 4th row, using Python? 如何循环一个数字,以便数字迭代每个可能的值? - How to loop a number so that the number iterates through every possible value? 在Python中,如何遍历此列表并将每个项目调用到同一函数? - In Python, how do I loop through this list and call every item to the same function? 如何迭代和循环遍历python中的每个数组值 - How to iterate and loop through every array value in python 插入排序问题——如何遍历每个数字? - Insertion Sort problem -- How do I loop through every number? 如何在 Python 中循环遍历 **kwargs? - How do I loop through **kwargs in Python? 如何循环遍历在每个元素处停止的 python 列表 - How can I loop through a python list stopping at every element 每次我在python中循环时如何创建节点 - How to create a nodes every time i go through the loop in python 在Python中,如何在通过循环创建的python列表中获取条目的值 - In Python, how do I grab the value of an entry in a python list created through a loop 摩尔斯电码Python 3:如何为每个可能输入的单词使用多个if语句,我如何为此做一个循环? - Morse Code Python 3: Instead of using multiple if statements for every possible word user can type in, how do I make a loop for this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM