简体   繁体   English

使用hexdump sed和xxd替换文件中的二进制文件(来自java keytool csr)?

[英]Replace binary in a file (from java keytool csr) using hexdump sed and xxd?

I am attempting to follow this reply and change a few hex bytes in a file by using hexdump, xxd, and sed. 我试图通过使用hexdump,xxd和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 . 根据该响应,在将使用keytool生成的CSR(恰好是base-64 PEM格式)转换为DER之后,我应该能够进行直接字节替换,将0x13替换为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 )? 如何使用可用工具(如sedxxd和/或hexdump )执行字节替换?

From http://everydaywithlinux.blogspot.ca/2012/11/patch-strings-in-binary-files-with-sed.html : 来自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. 如果文件是文本文件,那么sed可能会帮你解决。 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. 在构建RPM时,您还可能需要在打包阶段进行修补。

So, how can you use sed then? 那么,你怎么能用sed呢?

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将二进制文件转换为ASCII HEX,使用sed修补它,然后使用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. 您还必须创建自己作为空终止字符串的ASCII HEX表示,并且它们的空终止符存在。 Further, must be padded to the same length as <pattern> . 此外,必须填充到与<pattern>相同的长度。

Refer to this example 请参阅此示例

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM