例如给定任意字符串。可能是chars或只是随机的bytes:
string = '\xf0\x9f\xa4\xb1'
Run Code Online (Sandbox Code Playgroud)
我想输出:
b'\xf0\x9f\xa4\xb1'
Run Code Online (Sandbox Code Playgroud)
这看起来很简单,但我无法在任何地方找到答案。当然,只需键入b后面的字符串即可。但我想在运行时执行此操作,或者从包含字节字符串的变量执行此操作。
如果给定的string是AAAA或一些已知的,characters我可以简单地执行string.encode('utf-8'),但我期望字节串只是随机的。对'\xf0\x9f\xa4\xb1'(随机字节)执行此操作会产生意外结果b'\xc3\xb0\xc2\x9f\xc2\xa4\xc2\xb1'。
一定有更简单的方法来做到这一点吗?
编辑:
我想将字符串转换为字节而不使用编码
我正在进行protostar漏斗练习#5.
基本上这是代码,目标是将代码执行重定向到shellcode.我的问题只涉及为什么我的指令没有执行.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char buffer[64];
gets(buffer);
}
Run Code Online (Sandbox Code Playgroud)
这是一个缓冲区溢出问题.我已成功将返回地址重定向到我的nop sled的开头,然后是int3"\ xcc"shellcode,它阻止程序执行(只是检查它是否会执行)
"\x90\x90\x90\x90\xcc\xcc\xcc\xcc"
Run Code Online (Sandbox Code Playgroud)
基本上,当我走过我的nop雪橇时,它只是说
0xbffff7a2 in ?? ()
(gdb)
0xbffff7a3 in ?? ()
(gdb)
0xbffff7a4 in ?? ()
(gdb)
Run Code Online (Sandbox Code Playgroud)
请记住,我已成功将代码重定向到这些地址,并且我将这些地址分别视为"\ x90"和"\ xcc".但没有任何反应.该程序甚至没有收到来自"\ xcc"的中断.
我可能没有提供足够的信息,但基本上它应该执行这些指令,但似乎没有.
我想可能是指令需要字节对齐?但这甚至重要吗?
我想知道为什么即使你指向那个地址也不能执行指令的原因是什么.
This is x86 Architecture machine.
Run Code Online (Sandbox Code Playgroud) 这是我的c ++代码:
#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;
int main() {
clock_t end;
clock_t start = clock();
for(int i = 0; i < 1000; i++) {
cout << "Test" << endl;
}
end = clock() - start;
double duration = end / (double) CLOCKS_PER_SEC;
cout << "Duration: " << duration << endl;
getchar();
return 0;
} // This takes around ~ .9 secs on average
Run Code Online (Sandbox Code Playgroud)
这是我的python代码:
import time
def foo():
start = time.time()
for i in range(1000): …Run Code Online (Sandbox Code Playgroud)