【什么是软件的逆向工程】
很几个朋友留言说这个是逆向工程么,在我看来什么是逆向工程本身是仁者见仁,智者见智的。我参考了一下wikipedia【4】,摘录如下:
The term reverse engineering as applied to software means different things to different people, prompting Chikofsky and Cross to write a paper researching the various uses and defining a taxonomy. From their paper, they state, "Reverse engineering is the process of analyzing a subject system to create representations of the system at a higher level of abstraction."[4] It can also be seen as "going backwards through the development cycle".[5] In this model, the output of the implementation phase (in source code form) is reverse-engineered back to the analysis phase, in an inversion of the traditional waterfall model. Reverse engineering is a process of examination only: the software system under consideration is not modified (which would make it reengineering). Software anti-tamper technology is used to deter both reverse engineering and reengineering of proprietary software and software-powered systems. In practice, two main types of reverse engineering emerge. In the first case, source code is already available for the software, but higher-level aspects of the program, perhaps poorly documented or documented but no longer valid, are discovered. In the second case, there is no source code available for the software, and any efforts towards discovering one possible source code for the software are regarded as reverse engineering. This second usage of the term is the one most people are familiar with. Reverse engineering of software can make use of the clean room design technique to avoid copyright infringement.
On a related note, black box testing in software engineering has a lot in common with reverse engineering. The tester usually has the API, but their goals are to find bugs and undocumented features by bashing the product from outside.
Other purposes of reverse engineering include security auditing, removal of copy protection ("cracking"), circumvention of access restrictions often present in consumer electronics, customization of embedded systems (such as engine management systems), in-house repairs or retrofits, enabling of additional features on low-cost "crippled" hardware (such as some graphics card chipsets), or even mere satisfaction of curiosity.
因为使用的是java,对其它语言只能供参考了,但是基本方法应该是类似的。
美国这里有着很好的开源代码环境,最著名的是老牌的sourceforge,但是现在Google也提供了类似的服;还有很多大学都将自己研究的代码公布出来。很多时候利用或者学习别人的东西在进行开发,“站在巨人的肩膀上,逆向工程便是通向巨人肩膀的一条捷径。”【1】
首先是下载源代码,一般都是直接下载zip的压缩包,也用使用SVN直接下载最新的开发包的。取决于你的要求了,是否要更上最新的开发进度。网站一般都会提供用户手册 和开发手册 ,这些文档尤其的重要,应当认真的阅读。有些时候网站还会提供一些FAQ,Wiki以及一些example或者Demo。
在阅读代码前,一定要认真的阅读FAQ和get start,可以避免很多不必要的错误!
阅读源代码需要一些基本知识,比如设计模式,XML,UML,JUnit等。现在的代码大部分都是社区模式,都应用了很多软件工程的模式。接下来导入代码到开发工具,我使用Eclipse为主,也会用NetBeans。很多时候导入都是很顺利的,如果源代码需要调用其它的类库,就需要设置jar路径,甚至自己去下载那些类库;如果涉及J2EE的,还要配置运行环境,这是题外话了。
我基本上将源代码分为两种,带图形界面(GUI)的或者不带的。带GUI有一些特殊之处,后面单独再说。
一般首先是看package,分析类之间的关系,这个时候UML很有用了。可以参考我之前的一篇文章【2】。接下来就是分析类里面的具体函数了,这个时候很需要分析以下函数调用关系,也叫做call hierarchy,这个一般是树形结构;如果采用图来表示,也叫做call graph。这里就具体说一些Eclipse里面如何进行函数调用关系的分析。
这里有几个快捷键可以记住【3】:
1. Ctrl+左键
这个是大多数人经常用到的,用来查看变量、方法、类的定义
2. Ctrl+O
查看一个类的纲要,列出其方法和成员变量。提示 :再多按一次Ctrl+O ,可以列出该类继承的方法和变量。
助记 :"O"--->"Outline"--->"纲要"
3. Ctrl+T
查看一个类的继承关系树,是自顶向下的,再多按一次Ctrl+T, 会换成自底向上的显示结构。
提示 :选中一个方法名,按Ctrl+T,可以查看到有这个同名方法的父类、子类、接口。
助记 :"T"------->"Tree"----->"层次树"
4.Alt+左右方向键
我们经常会遇到看代码时Ctrl+左键,层层跟踪,然后迷失在代码中的情况,这时只需要按“Alt+左方向键”就可以退回到上次阅读的位置,同理,按“Alt+右方向键”会前进到刚才退回的阅读位置,就像浏览器的前进和后退按钮一样。
5.Ctrl+Alt+H
如果你想知道一个类的方法到底被那些其他的类调用,那么请选中这个方法名,然后按“Ctrl+Alt+H”,Eclipse就会显示出这个方法被哪些方法调用,最终产生一个调用关系树。
5.Alt+Shift+Q, T
这是用来显示,你可以将上面call hierarchy里面的任何一个函数或者拖到这个面板,它会分析类的调用关系,生成一个class hierarchy。
有了宏观的UML再加上微观的函数调用关系,一般就能比较好的理解源代码了。
还有一些其他软件(大部分我也没有用过),列在这里,或许有用:gprof, Ariadne,Slickedit,codeviz,DTrace。
参考:
【1】http://hi.baidu.com/ghd_214/blog/item/87a72dadb6df2f034b36d6ea.html
【2】http://blog.csdn.net/autofei/archive/2009/11/04/4765289.aspx
【3】http://doggou.javaeye.com/blog/211741
【4】http://en.wikipedia.org/wiki/Reverse_engineering#Reverse_engineering_of_software