Hel*_*ded 6 scripting python netcat
我必须从 netcat 获取输出,对其进行解码并返回。
键入后:
nc cs2107.spro.ink 9000
Run Code Online (Sandbox Code Playgroud)
我得到的输出是这样的:
Welcome to the Proof of work challenge!
Rules: i will provide hex encoded byte strings to you.
Your task is to decode it and calculate the md5 hash in hex encoded format and return it back to me. You will need to do this 500 times!
Ready? Go!
cdde140fffda1da2bc3f
MD5:
Run Code Online (Sandbox Code Playgroud)
所以我必须接受那个十六进制编码的字符串,解码它并再次输出它,这假设发生了 500 次。
我想我知道我必须做什么,但我不知道如何在 unix 中对其进行编码。我认为需要一个 .sh 文件?但我不太确定。
从 nc cs2107.spro.ink 9000 开始
从 nc 输出中搜索十六进制字符串
解码它并计算 md5 哈希
最后,送回去
编辑:
我知道这样做可以保存 nc 的输出
nc cs2107.spro.ink 9000 > somefile.txt
Run Code Online (Sandbox Code Playgroud)
然后我如何专门搜索十六进制字符串?
我如何解码十六进制字符串?
最后如何将结果返回到终端?
edit2:所以我可以使用 python 来完成这个任务。一些提示是使用 subprocess 模块或 sockets 模块。在我读到的时候尝试用子进程来做
subprocess.Popen
Run Code Online (Sandbox Code Playgroud)
保持命令在后台运行
我目前坚持这个
import subprocess
p = subprocess.Popen('nc cs2107.spro.ink 9000', shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
a = subprocess.Popen('grep ^[0-9a-f] | xxd -r | md5sum | awk "{print $1}" ', stdin = p.stdout, stdout = subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
现在我被困在试图将 a 重新导入 p
使用调用的 shell 执行此操作nc很困难,因为您需要保持连接打开并从中获取数据nc、处理该数据并将其反馈回同一实例nc。这是可以做到的,但是很难。
如果你能用同一种语言与网络交互并控制数据流,那就容易多了。如果您使用 shell bash、ksh 或 ksh 之一,那么您可以使用它们的网络功能:这些 shell 可以是 TCP 客户端。例如,下面是一个从服务器读取行并逐一回显它们的脚本:
{
while IFS= read -r line <&3; do
echo "$line" >&3
done
} 3<>/dev/tcp/cs2107.spro.ink/9000
Run Code Online (Sandbox Code Playgroud)
要解码十六进制字符串,请查找xxd. 我会让你弄清楚数据处理逻辑。