创建UNIX二进制文件 [英] Create Binary files in UNIX
问题描述
这个问题是出有一段时间了,我想我应该提供一些奖励积分,如果我能得到它的工作。
This question was out there for a while and I thought I should offer some bonus points if I can get it to work.
近日在工作中,我写了一个解析器,将转换为二进制文件以可读格式。二进制文件与 10101010
字符的ASCII文件。它一直连接在二进制codeD。所以,如果我做了猫
上的文件,我得到以下 -
Recently at work, I wrote a parser that would convert a binary file in a readable format. Binary file isn't an Ascii file with 10101010
characters. It has been encoded in binary. So if I do a cat
on the file, I get the following -
[jaypal~/Temp/GTP]$ cat T20111017153052.NEW
==?sGTP?ղ?N????W????&Xx1?T?&Xx1?;
?d@#e?
?0H????????|?X?@@(?ղ??VtPOC01
cceE??k@9??W傇??R?K?i2??d@#e???&Xx1&Xx??!?
blackberrynet?/??!
??!
??#ripassword??W傅?W傆??0H??
#R??@Vtc@@(?ղ??n?POC01
所以我用 hexdump都
工具,使文件显示以下内容并将其重定向到一个文件。现在我这是包含十六进制值的文本文件我的输出文件。
So I used hexdump
utility to make the file display following content and redirected it to a file. Now I had my output file which was a text file containing Hex values.
[jaypal~/Temp/GTP]$ hexdump -C T20111017153052.NEW
00000000 3d 3d 01 f8 73 47 54 50 02 f1 d5 b2 be 4e e4 d7 |==..sGTP.....N..|
00000010 00 01 01 00 01 80 00 cc 57 e5 82 00 00 00 00 00 |........W.......|
00000020 00 00 00 00 00 00 00 00 87 d3 f5 13 00 00 00 00 |................|
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 10 |................|
00000040 01 01 0f 00 00 00 00 00 26 58 78 31 00 b3 54 c5 |........&Xx1..T.|
00000050 26 58 78 31 00 b4 3b 0a 00 00 ad 64 13 40 01 03 |&Xx1..;....d.@..|
00000060 23 16 65 f3 01 01 0b 91 30 19 48 99 f2 ff ff ff |#.e.....0.H.....|
00000070 ff ff ff 02 00 7c 00 dc 01 58 00 a0 40 40 28 02 |.....|...X..@@(.|
00000080 f1 d5 b2 b8 ca 56 74 50 4f 43 30 31 00 00 00 00 |.....VtPOC01....|
00000090 00 04 0a 63 63 07 00 00 00 00 00 00 00 00 00 00 |...cc...........|
000000a0 00 00 00 65 45 00 00 b4 fb 6b 40 00 39 11 16 cd |...eE....k@.9...|
000000b0 cc 57 e5 82 87 d3 f5 52 85 a1 08 4b 00 a0 69 02 |.W.....R...K..i.|
000000c0 32 10 00 90 00 00 00 00 ad 64 00 00 02 13 40 01 |2........d....@.|
在吨 AWK
, SED
和削减
,该脚本转换十六进制值成可读的文本。要做到这一点,我用了偏移定位这将标志着转换每个参数的开始和结束位置。所有转换后生成的文件看起来像这样
After tons of awk
, sed
and cut
, the script converted hex values into readable text. To do so, I used the offset positioning which would mark start and end position of each parameter converted. The resulting file after all conversion looks like this
[jaypal:~/Temp/GTP] cat textfile.txt
Beginning of DB Package Identifier: ==
Total Package Length: 508
Offset to Data Record Count field: 115
Data Source: GTP
Timestamp: 2011-10-25
Matching Site Processor ID: 1
DB Package format version: 1
DB Package Resolution Type: 0
DB Package Resolution Value: 1
DB Package Resolution Cause Value: 128
Transport Protocol: 0
SGSN IP Address: 220.206.129.47
GGSN IP Address: 202.4.210.51
为什么我这样做
我是一名测试工程师和手动验证二进制文件是一个重大的痛苦。我不得不通过偏移手动解析和使用计算器,将它们转换并验证它针对的Wireshark和GUI。
Why did I do it
I am a test engineer and to manually validate binary files was a major pain. I had to manually parse through the offsets and use a calculator to convert them and validate it against Wireshark and GUI.
我希望做什么我做的相反。这是我的计划 -
I wish to do the reverse of what I did. This was my plan -
- 有一个易于阅读这将对
参数输入文本文件:值
。 - 用户可以简单地把价值旁边(如日期将是一个参数,用户可以给他们想要的数据文件有日期)。
- 该脚本将切出从输入文本文件中的所有培训相关信息(用户提供的信息),并将其转换为十六进制值。
- 一旦文件中的十六进制值已经转换,我想带code将其回二进制。
前三个步骤完成
在我的脚本输入文本文件转换到与十六进制值的文本文件,我得到一个文件中像如下(的通知我可以做猫
就可以了)。
Once my script converts the Input text file in to a text file with hex values, I get a file like follows (notice I can do cat
on it).
[visdba@hw-diam-test01 ParserDump]$ cat temp_file | sed 's/.\{32\}/&\n/g' | sed 's/../& /g'
3d 3d 01 fc 73 47 54 50 02 f1 d6 55 3c 9f 49 9c
00 01 01 00 01 80 00 dc ce 81 2f 00 00 00 00 00
00 00 00 00 00 00 00 00 ca 04 d2 33 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10
01 01 0f 00 00 07 04 ea 00 00 ff ff 00 00 14 b7
00 00 ff ff 00 00 83 ec 00 00 83 62 54 14 59 00
60 38 34 f5 01 01 0b 58 62 70 11 60 f6 ff ff ff
ff ff ff 02 00 7c 00 d0 01 4c 00 b0 40 40 28 02
f1 d6 55 38 cb 2b 23 50 4f 43 30 31 00 00 00 00
00 04 0a 63 63 07 00 00 00 00 00 00 00 00 00 00
我的内涵是为连接codeD 此转换的文件到一个二这样,当我做猫
上的文件,我得到一堆乱七八糟的值。
My intension is to encoded this converted file in to a binary so that when I do cat
on the file, I get bunch of garbage values.
[jaypal~/Temp/GTP]$ cat temp.file
==?sGTP?ղ?N????W????&Xx1?T?&Xx1?;
?d@#e?
?0H????????|?X?@@(?ղ??VtPOC01
cceE??k@9??W傇??R?K?i2??d@#e???&Xx1&Xx??!?
blackberrynet?/??!
??!
所以,问题是这样的。 如何做我带code将其以这种形式?
我们没有很多的GTP(GPRS隧道协议)生产的消息。我想如果我逆向工程这一点,我可以有效地创建一个数据发生器,使我自己的数据。
We don't have a lot of GTP (GPRS Tunnelling Protocol) messages on production. I thought if I reverse engineer this, I could effectively create a data generator and make my own data.
有可能是复杂的工具在那里,但我不想花太多时间学习他们。它已经约2个月,我已经开始了* nix的平台上工作,刚开始手握住它的力量工具,如 SED
和 AWK
。
There may be sophisticated tools out there, but I don't want to spend too much time learning them. It's been around 2 months, I have started working on the *nix platform and just getting hand around it's power tools like sed
and awk
.
我确实希望一些帮助和指导,以实现这一目标。
What I do want is some help and guidance to make this happen.
再次感谢您的阅读! 200点等待的人谁可以指导我在正确的方向。 :)
Thanks again for reading! 200 points awaits for someone who can guide me in the right direction. :)
下面是原二进制文件的样本
下面是输入文本文件的样本将允许用户在值冲
Here is a sample of Input Text File that would allow the User to punch in values
下面是文件我的脚本创建毕竟从输入文本文件转换完成。
Here is a sample of File that my script creates after all the conversion from the Input Text File is complete.
的如何更改的编码文件3
到文件1
?的
推荐答案
您可以使用 XXD 转换和从二进制文件/ hexdumps很简单。
You can use xxd to convert to and from binary files / hexdumps quite simply.
数据为十六进制
echo Hello | xxd -p
48656c6c6f0a
十六进制数据
echo 48656c6c6f0a | xxd -r -p
Hello
或
echo 48 65 6c 6c 6f 0a | xxd -r -p
Hello
的 -p
后记是模式,允许更自由输入
The -p
is postscript mode which allows for a more freeform input
这是从输出 XXD -r -p文本
其中文字是你给上述
This is the output from xxd -r -p text
where text is the data you give above
==▒sGTP▒▒U<▒I▒▒▒/▒▒3▒▒▒▒▒▒▒▒▒bTY`84▒
Xbp`▒▒▒▒▒▒▒|▒L▒@@(▒▒U8▒+#POC01
:▒ިv▒b▒▒▒▒TY`84Ud▒▒▒▒>▒▒▒▒▒▒▒!▒
blackberrynet▒/▒▒!
M
▒▒!
N
▒▒#Oripassword▒▒/▒▒/▒▒Xbp`▒@@(▒▒U8▒IvPOC01
:qU▒b▒▒▒▒▒▒TY`84U▒▒▒*:▒▒!
▒k▒▒▒#O Welcmme!
▒!
M
这篇关于创建UNIX二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!