使用hexdump sed和xxd替换文件中的二进制文件(来自Java keytool csr)? [英] Replace binary in a file (from java keytool csr) using hexdump sed and xxd?

查看:553
本文介绍了使用hexdump sed和xxd替换文件中的二进制文件(来自Java keytool csr)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循此回复,并使用hexdump,xxd和sed.

根据该响应,将keytool生成的CSR(碰巧是base-64 PEM格式)转换为DER之后,我应该能够进行直接字节替换,将0x13替换为0x0c. /p>

这是我尝试过的:

#convert csr pem to der
openssl req -in openfire.csr -outform der -out openfire_csr.der
cat openfire_csr.der | grep -aP '\x13' | md5sum
#e61387f5c1xxxxeb832df102524220d81  - #it has some length
#perform replacement of hex bytes:
sed 's/\x13/\x0c/g' openfire_csr.der
#convert csr der to csr pem:
openssl req -in openfire_csr.der -outform pem -out openfire_utf8.csr
#unable to load X509 request
#3078055660:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:698:Expecting: CERTIFICATE REQUEST

我怀疑我错过了一些转换,但是我不知道在哪里.

如何使用可用工具(例如sedxxd和/或hexdump)执行字节替换?

解决方案

来自

当然有此方法的注意事项.最重要的是,您不能用比原始字符串更长的字符串来替换字符串.简短一点是可以的.另一个问题是字符串必须以null终止,但这几乎总是这种情况.您还必须创建一个以空终止符存在的空终止字符串的ASCII HEX表示形式.此外,必须将其填充为与<pattern>相同的长度.

请参阅此示例

I am attempting to follow this reply and change a few hex bytes in a file by using hexdump, xxd, and sed.

According to that response, after converting the CSR generated with keytool (which happens to be base-64 PEM format) into DER, I should be able to do a straight bytes replacement, replacing 0x13 with 0x0c.

Here is what I have attempted:

#convert csr pem to der
openssl req -in openfire.csr -outform der -out openfire_csr.der
cat openfire_csr.der | grep -aP '\x13' | md5sum
#e61387f5c1xxxxeb832df102524220d81  - #it has some length
#perform replacement of hex bytes:
sed 's/\x13/\x0c/g' openfire_csr.der
#convert csr der to csr pem:
openssl req -in openfire_csr.der -outform pem -out openfire_utf8.csr
#unable to load X509 request
#3078055660:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:698:Expecting: CERTIFICATE REQUEST

I suspect I'm missing some conversion, but I do not know where.

How do I perform byte replacement using available tools (like sed, xxd, and/or hexdump)?

解决方案

From http://everydaywithlinux.blogspot.ca/2012/11/patch-strings-in-binary-files-with-sed.html:

So, you have a binary file that you need to patch. Perhaps it is a pre compiled proprietary program or dynamic library that contains hard coded paths (text strings) that you need to change.

If the file had been a text file, then sed would probably come to your rescue. For binary files there are hex editors available, but they require manual handling and can't be scripted. Other binary patch programs are out there as well but might not be packaged in your favorite distribution and compiling things from source is boring. You could also have the need to do the patching in a packaging stage when building say an RPM.

So, how can you use sed then?

Well, it's quite simple. Just convert the binary file to ASCII HEX with hexdump, patch it with sed and the convert it back to binary with xxd:

hexdump -ve '1/1 "%.2X"' file.bin | \
sed "s/<pattern>/<replacement>/g" | \
xxd -r -p > file.bin.patched

Of course there are caveats to this approach. The most significant one is that you can't replace a string with a string that is longer then the original one. Shorter is OK though. Another one is that the strings must be null terminated, but this is almost always the case. You also have to create and yourself as the ASCII HEX representations of the null terminated strings with their null terminator present. Further, must be padded to the same length as <pattern>.

Refer to this example

这篇关于使用hexdump sed和xxd替换文件中的二进制文件(来自Java keytool csr)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆