Quartett!的二进制脚本分析

我前两天在NetOA方面确实是有点懈怠了。不为别的,正是为了这篇将提到的脚本的分析。虽然没把分 析做彻底,不过我觉得现在已经足够使用,顺便拿出来说说。

上个周末,汉公突然跟我提起FFDSystem的话题,然后有人联系我做Quartett!的汉化。自从跟汉公和 明大合作参与汉化以来,我基本上就是做脚本处理的相关工作比较多;汉公解决破解的棘手问题,而明大 主要完成打包问题,也兼做脚本编辑器,视具体分工而定。这次也不例外,汉公主攻了资源文件的破解和 资源抽取,资源的打包还没做,脚本这块就暂时交给了我。一般,如果脚本是没经过处理的文本,那也就 没我什么事了;这次遇到的果然还是经过处理了的二进制脚本。

一拿到已经从Script.dat中提取出来的脚本文件,我吓了一跳:文件名居然都是MD5……汉公那边果然 还没把资源破解完善。不过没关系,只要文件内容是对的就能开工。可以确认的是,脚本(准确说是给到 我手上的脚本)的后缀名是tkn。

打开其中的第一个文件,0a69b4afebd6d64527a21e3f1aa993f9.tkn。内容如下:

Java代码

Offset      0  1  2  3  4  5  6  7   8  9  A  B  C  D  E  F00000000   54 4F 4B 45 4E 53 45 54  64 00 00 00 76 08 00 00   TOKENSETd...v...00000010   0C 00 00 00 85 23 00 0C  00 00 00 81 62 61 73 65   ....?.....|ase00000020   5F 70 61 74 68 00 0C 00  00 00 83 2E 2E 2F 00 16   _path.....?./..00000030   00 00 00 85 23 00 16 00  00 00 81 69 6E 63 6C 75   ...?.....(nclu00000040   64 65 00 16 00 00 00 83  53 63 72 69 70 74 2F 42   de.....ゴcript/B00000050   61 73 65 49 6E 73 74 72  75 63 74 69 6F 6E 2E 74   aseInstruction.t00000060   78 74 00 20 00 00 00 81  6D 6F 74 69 6F 6E 00 20   xt. ...[otion. 00000070   00 00 00 81 4D 61 69 6E  00 20 00 00 00 85 28 00   ...`ain. ...?.

读起来似乎很郁闷(?),其实看到有那么多ASCII字符我已经很开心了。可以辨认出最开头的 TOKENSET(但此时还无法判断那个d是什么)、ase_path、nclude等等。进一步观察可以发现那些看似被 剪掉了的字符都在,前面的base_path、include就是如此。编辑器里显示不出来只是因为大于0x7F的字节 被解释成双字节字符编码(DBCS)中一个双字节字符的首字节,也就是例如说0x81把base_path中的b (0x62)给“吃”了。

在上述截图范围内,我总共识别出了这些:base_path、include、Script/BaseInstruction.txt、 motion、Main等字串。观察它们前后的规律:这些字串总是以0结尾,是标准的C string;这些字串的前 面总是有一个大于0x7F的字节(留意到0x81和0x83),而在那个字节之前似乎总是有3个00字节,前面又 是一个非00的字节。

为了方便分析,我写了一个小程序来抽取出我感兴趣的信息,辅助分析。

对应上面内容而提出出来的内容:

(格式是:字符串起始地址 一个奇怪的数字 字符串之前的那个字节 字符串内容)

Java代码

0x1C 0xC 0x81 base_path0x2B 0xC 0x83 ../0x3B 0x16 0x81 include0x48 0x16 0x83 Script/BaseInstruction.txt0x68 0x20 0x81 motion0x74 0x20 0x81 Main

经观察,发现字符串之前的那个字节似乎是某种操作码或者类型,而再前面的那个似乎是个什么奇怪 的数字,会连续有好几个相同的,然后又增大一点。

接下来,突然发觉原来0x85也是个重要的数值;也有以这个数值打头的字符串,但一般都是长度为一 的符号,所以先前被忽略了。想了想,干脆把0x80开始到0x88开头的,其之前是三个00的东西全部都扫描 一遍。于是在之前的程序上修改了一下判断条件,得到下面代码:

opcode_analysis.cs:

C#代码

1.using System;2.using System.Collections.Generic;3.using System.IO;4.using System.Text;5.6.namespace FFDSystemAnalysis7.{8. sealed class Analyzer9. {10. private static readonly byte[ ] SIGNATURE = {11. ( byte )0x54, ( byte )0x4F, ( byte )0x4B, ( byte )0x45,12. ( byte )0x4E, ( byte )0x53, ( byte )0x45, ( byte )0x54,13. ( byte )0x64, ( byte )0x0, ( byte )0x0, ( byte )0x014. };15.16. static void Main( string[ ] args ) {17. if ( !args[ 0 ].EndsWith( ".tkn" ) ) return;18. if ( !File.Exists( args[ 0 ] ) ) return;19.20. string infile = args[ 0 ];21. string utfile = infile + ".txt";22.23. Encoding utf16le = new UnicodeEncoding( false, true );24. Encoding jis = Encoding.GetEncoding( 932 );25.26. using ( BinaryReader reader = new BinaryReader( File.OpenRead( infile ), jis ) ) {27. using ( BinaryWriter writer = new BinaryWriter( File.Create( outfile ), utf16le ) ) {28. byte[ ] sig = reader.ReadBytes( SIGNATURE.Length );29. if ( !Equals( sig, SIGNATURE ) ) {30. Console.WriteLine( "Wrong signature" );31. return;32. } 33.34. // write UTF-16LE BOM35. writer.Write( ( ushort ) 0xFEFF );36.37. Queue queue = new Queue( 3 );38. queue.Enqueue( reader.ReadByte( ) );39. queue.Enqueue( reader.ReadByte( ) );40. queue.Enqueue( reader.ReadByte( ) );41.42. byte lastOpcode = 0;43. while ( reader.BaseStream.Position < reader.BaseStream.Length ) {44. byte currentByte = reader.ReadByte( );45. if ( currentByte == 0x08046. || currentByte == 0x08147. || currentByte == 0x08248. || currentByte == 0x08349. || currentByte == 0x08450. || currentByte == 0x08551. || currentByte == 0x08652. || currentByte == 0x08753. || currentByte == 0x088 ) {54. if ( MatchQueueData( queue ) ) {55. long position = reader.BaseStream.Position;56. string line = ReadCString( reader );57. Entry e = new Entry( ) {58. position = position,59. pcode = currentByte,60. lastOpcode = lastOpcode,61. line = line62. };63. writer.Write(64. utf16le.GetBytes(65. string.Format( "{0}{1}",66. e.ToString( ),67. Environment.NewLine ) 68. )69. );70. } // if71. } // if72.73. // re-initialize74. lastOpcode = queue.Dequeue( );75. queue.Enqueue( currentByte );76. } // while77. } // using78. } // using79. } // Main80.81. static bool Equals( byte[ ] a, byte[ ] b ) {82. int len = a.Length;83. if ( len != b.Length ) return false;84. for ( int i = 0; i < len; i++ ) {85. if ( a[ i ] != b[ i ] ) return false;86. }87. return true;88. }89.90. static bool MatchQueueData( Queue queue ) {91. byte[ ] array = queue.ToArray( );92. return Equals( zeros, array );93. }94.95. static string ReadCString( BinaryReader reader ) {96. StringBuilder builder = new StringBuilder( );97. char c = '/0';98.99. while ( ( c = reader.ReadChar( ) ) != '/0' ) {100. builder.Append( c );101. }102.103. return builder.ToString( );104. }105.106. static readonly byte[ ] zeros107. = new byte[ ] { 0, 0, 0 };108. }109.110. struct Entry111. {112. public long position;113. public string line;114. public byte opcode;115. public byte lastOpcode;116.117. public override string ToString( ) {118. return string.Format( "0x{0:X} 0x{1:X} 0x{2:X} {3}",119. this.position, this.lastOpcode, this.opcode, this.line );120. }121. } 122.}

这段代码本身没什么稀奇,只有第57行到62行的内容有点诡异:居然把变量赋值给自己了?

不不,再怎么说我也不可能犯这种错误。这其实是C# 3.0里的一个有趣语法,initializer。可以通过 initializer,在使用new关键字构造新实例的时候指定其中一些字段的值;等号左边的是字段名,右边则 是字面量或者变量名(或者表达式)。编译器能够正确识别出看似是同名字的token之间的区别,因而能 够正确赋值。好吧我承认这不是好的编程习惯,大家看到了千万不要学,要引以为戒……

另外,那个if里一大堆对currentByte的判断后来也重构到外面一个单独的MatchOpcode()方法里去了 。像上面这样写实在太恶心……也要引以为戒哦

虽然没什么稀奇,还是说下这个文件里的流程:

1、检查作为参数文件是否存在,并且是否后缀为tkn。检查不通过则退出程序。

2、获取一个Shift-JIS和一个UTF-16LE字符集的Encoding实例,并使用它们创建Shift-JIS的输入流和 UTF-16LE的输出流。

3、校验脚本文件的特征码(signature)。这里假设头12个字节都是特征码。

4、校验成功后,给输出流写出一个字节序标记(BOM,Byte Order Mark)。这本来应该不需要手工做 的,但我一直没弄清楚为什么我明明在创建utf16le时指定要BOM系统却不帮我自动做……

5、创建一个队列来记录最近的三个字节。使用一个变量(lastOpcode)来记录最近的第四个字节。

6、扫描文件直到遇到文件结束。如果遇到了连续的3个00,则读入其后的一个字节,并判断是否在 [0x80, 0x88]的范围内;满足的话则读入一个C string并输出记录。

7、程序结束。

于是我得到了更新版的记录:

(格式与前面相同)

Java代码

0x15 0xC 0x85 #0x1C 0xC 0x81 base_path0x2B 0xC 0x83 ../0x34 0x16 0x85 #0x3B 0x16 0x81 include0x48 0x16 0x83 Script/BaseInstruction.txt0x68 0x20 0x81 motion0x74 0x20 0x81 Main

于是我恍然大悟:那“奇怪的数字”居然是脚本源文件行号!而被认为是操作码或者类型的那个字节 ,则用于指定后面字符串的类型:可以是符号、十进制数字、十六进制数字、标识符、字符串、符号等。

但位于脚本的0xC到0xF的那个数字(上图紫色部分)是什么意思还让我伤了下脑筋。观察了一下,发 现从0a69b4afebd6d64527a21e3f1aa993f9.tkn提取出来的“东西”一共有1237个,而那意义不明的数字是 0x876 = 2166,还差了不少。但总觉得它们应该有关系。突然想起我前面是用了个很糟糕的办法来提取记 录,有连续的3个00字节才满足条件。但假如行号超过了0xFF = 255行的话这个条件就不成立了。赶紧把 程序修改为第三版,按照新的理解去读入“行号”和“类型”两个数据,确认那个数字确实就是文件里总 的token数。

然后我才理解了signature里那TOKENSET的含义……这看似是二进制的脚本其实根本没有编译过的二进 制脚本之魂。

编译的前端至少有两部,scan和parse。Scan阶段处理词法分析,会把源文件切分成一个个token,而 parse阶段处理文法分析,会根据上下文无关文法来尝试“理解”这些token,构造语法树(进而构造抽象 语法树)。但这里我所看到的脚本只对脚本源文件做了scan,然后直接把scan的结果保存成“二进制脚本 ”了。真够OTL的。

简单点说,这个“二进制脚本”完整保留了脚本源文件的文本信息,而且还多加了些行号、类型等信 息进去。缺少的是被去除了的注释。

那就很好办了不是么。于是把所谓的反编译程序写了出来:

ScriptDecompiler.cs

C#代码

1.// ScriptDecompiler.cs, 2007/12/182.// by RednaxelaFX3.4./*5. * Copyright (c) 2007 着作权由RednaxelaFX所有。着作权人保留一切权利。6. *7. * 这份授权 条款,在使用者符合以下三条件的情形下,授予使用者使用及再散播本8. * 软件包装原始码及二进 位可执行形式的权利,无论此包装是否经改作皆然:9. *10. * * 对于本软件源代码的再散播, 必须保留上述的版权宣告、此三条件表列,以11. * 及下述的免责声明。12. * * 对于本套件 二进位可执行形式的再散播,必须连带以文件以及/或者其他附13. * 于散播包装中的媒介方式, 重制上述之版权宣告、此三条件表列,以及下述14. * 的免责声明。15. * * 未获事前取得书 面许可,不得使用RednaxelaFX之名称,16. * 来为本软件之衍生物做任何表示支持、认可或推广 、促销之行为。17. *18. * 免责声明:本软件是由RednaxelaFX以现状("as is") 提供,19. * 本软件包装不负任何明示或默示之担保责任,包括但不限于就适售性以及特定目 20. * 的的适用性为默示性担保。RednaxelaFX无论任何条件、21. * 无论成因或任何责任主义 、无论此责任为因合约关系、无过失责任主义或因非违22. * 约之侵权(包括过失或其他原因等)而 起,对于任何因使用本软件包装所产生的23. * 任何直接性、间接性、偶发性、特殊性、惩罚性或任 何结果的损害(包括但不限24. * 于替代商品或劳务之购用、使用损失、资料损失、利益损失、业务 中断等等),25. * 不负任何责任,即在该种使用已获事前告知可能会造成此类损害的情形下亦然。 26. */27.28.using System;29.using System.Collections.Generic;30.using System.IO;31.using System.Text;32.33.namespace FFDSystemAnalysis34.{35. enum TokenType36. {37. Decimal = 0x080,38. Identifier = 0x081,39. Hexadecimal = 0x082,40. String = 0x083,41. peraTor = 0x08542. }43.44. sealed class ScriptDecompiler45. {46. private static readonly byte[ ] SIGNATURE = {47. ( byte ) 0x54, ( byte )0x4F, ( byte )0x4B, ( byte )0x45,48. ( byte )0x4E, ( byte ) 0x53, ( byte )0x45, ( byte )0x54,49. ( byte )0x64, ( byte )0x0, ( byte )0x0, ( byte )0x050. };51.52. static void Main( string[ ] args ) {53. if ( !args[ 0 ].EndsWith( ".tkn" ) ) return;54. if ( !File.Exists( args[ 0 ] ) ) return;55.56. string infile = args[ 0 ];57. string utfile = Path.GetFileNameWithoutExtension( infile ) + ".txt";58.59. Encoding utf16le = new UnicodeEncoding( false, true );60. Encoding jis = Encoding.GetEncoding( 932 );61.62. using ( BinaryReader reader = new BinaryReader( File.OpenRead( infile ), jis ) ) {63. using ( BinaryWriter writer = new BinaryWriter( File.Create( outfile ), utf16le ) ) {64. byte[ ] sig = reader.ReadBytes( SIGNATURE.Length );65. if ( !Equals( sig, SIGNATURE ) ) {66. Console.WriteLine( "Wrong signature" );67. return;68. }69.70. // write UTF-16LE BOM71. writer.Write( ( ushort ) 0xFEFF );72.73. // process each token74. int lineNum = 1;75. int lastLineNum = 1;76. TokenType tokenType = TokenType.OperaTor;77. TokenType lastTokenType = TokenType.OperaTor;78. int tabCount = 0;79. int tokenCount = reader.ReadInt32( );80. for ( int tokenNum = 0; tokenNum < tokenCount; ++tokenNum ) {81. // deal with line numbers, insert empty new lines if needed82. lineNum = reader.ReadInt32( );83. if ( lastLineNum < lineNum ) { // should write on a newline84. // write empty lines85. for ( int i = lastLineNum; i < lineNum; ++i ) {86. writer.Write( utf16le.GetBytes( Environment.NewLine ) );87. }88. // write tabs as indent89. for ( int tabs = 0; tabs < tabCount; ++tabs ) {90. writer.Write( utf16le.GetBytes( "/t" ) );91. }92. // put a dummy value into tokenType93. lastTokenType = TokenType.OperaTor;94. } 95.96. // get token tokenType97. tokenType = ( TokenType ) ( reader.ReadByte( ) & 0x0FF );98.99. // get token value100. string tokenString = ReadCString( reader );101.102. // deal with different token types103. if ( !( lastTokenType == TokenType.OperaTor104. || lastTokenType == TokenType.String105. || lastTokenType == TokenType.Decimal106. || lastTokenType == TokenType.Hexadecimal ) ) {107. writer.Write( utf16le.GetBytes( " " ) );108. }109. switch ( tokenType ) {110. case TokenType.Decimal:111. case TokenType.Identifier:112. case TokenType.Hexadecimal:113. writer.Write( utf16le.GetBytes( tokenString ) );114. break;115.116. case TokenType.String:117. writer.Write(118. utf16le.GetBytes( string.Format( "/"{0}/"", tokenString ) ) );119. break;120.121. case TokenType.OperaTor:122. switch ( tokenString ) {123. case "#":124. case "%":125. case "-":126. case "@":127. writer.Write( utf16le.GetBytes( tokenString ) );128. break;129. case "{":130. ++tabCount;131. writer.Write( utf16le.GetBytes( tokenString ) );132. break;133. case "}":134. --tabCount;135. writer.BaseStream.Position -= 2; // delete the last tab136. writer.Write( utf16le.GetBytes( tokenString ) );137. break;138. case "(":139. case ",":140. case ";":141. case "=":142. writer.Write(143. utf16le.GetBytes( string.Format( "{0} ", tokenString ) ) );144. break;145. case ")":146. writer.Write(147. utf16le.GetBytes( string.Format( " {0}", tokenString ) ) );148. break;149. } // switch tokenString150. break;151.152. default:153. Console.WriteLine( "Unexpected token type {0} at 0x{1}.",154. tokenType.ToString( "X" ),155. reader.BaseStream.Position.ToString( "X" ) );156. return;157. } // switch tokenType158.159. // re-initialize160. lastLineNum = lineNum;161. lastTokenType = tokenType;162. } // for163. }164. }165. }166.167. static bool Equals( byte[ ] a, byte[ ] b ) {168. int len = a.Length;169. if ( len != b.Length ) return false;170. for ( int i = 0; i < len; i++ ) {171. if ( a[ i ] != b[ i ] ) return false;172. }173. return true;174. }175.176. static string ReadCString( BinaryReader reader ) {177. StringBuilder builder = new StringBuilder( );178. char c = '/0';179.180. while ( ( c = reader.ReadChar( ) ) != '/0' ) {181. builder.Append( c );182. }183.184. return builder.ToString( );185. }186. }187.}

中间有些代码是为了插入缩进的,忽略那部分吧……

得到的脚本看起来像是这样:

Java代码

1.<br />2.<br />3.<br />4.<br />5.<br />6.<br />7.<br />8.<br />9.<br />10.<br />11.#base_path "../"<br />12.<br />13.<br />14.<br />15.<br />16.<br />17.<br />18.<br />19.<br />20.<br />21.#include "Script/BaseInstruction.txt"<br />22.<br />23.<br />24.<br />25.<br />26.<br />27.<br />28.<br />29.<br />30.<br />31.motion Main (  )  

中间是有很多空行没错。那些原本应该是有注释的地方,或者本身就是空行(为了让代码好看)。这 里我只是把原始脚本的状态尽量复原了出来而已。

暂时来说,这样就够用了。这个脚本处理已经让我们能做很多事。要进一步做的话,我可以把文法分 析也做出来,方便对脚本更仔细的分析。但这两天肯定是没时间做那种事情咯……

Until then…

P.S. 上述代码皆以BSD许可证的形式发布。请有兴趣的人在遵循BSD许可证的前提下自由使用这些代码 。

P.S.S. 其实上面代码值得吐槽的地方N多。例如说我完全没使用try-catch语句来处理可能出现的异常 ,又例如我在第一份代码里把一个Queue转变成数组再做相等性比较(极其恶心,本来自己写个循环数组 就解决了)。……这些都是所谓的“原型代码”,目标是尽可能快的写出代码来验证自己的一些设想是否 正确。偷懒不加异常处理、宁可别扭的使用标准库里的容器也不自己封装一个,都是出于同一原因。看倌 们请多多包涵这些地方 XD

P.S.S.S. 唉,不过我偷懒也真是过分了。后一份代码里居然没把BinaryWriter改回用StreamWriter… …

躲在某一地点,想念一个站在来路,

Quartett!的二进制脚本分析

相关文章:

你感兴趣的文章:

标签云: