很久有寫過一個廣工圖書館主頁一個類爬蟲的demo(因為沒接口,只能扒取靜態網頁),實現一些圖書館系統的一些功能。但最近發現圖書館系統在html頁面上做了手腳,一頁html頁面中嵌入了幾千行的注釋,并有了自己的App,應該是為了增加扒取的流量成本來防止別人去扒取網頁,不過加注釋這手段就不敢恭維了,內網訪問速度還行,但外網訪問的話體驗很差的。
主頁上的APP,必然是用了圖書館的后臺接口和服務器交互的,從而想試試用反編譯的手段來看看APP使用了什么接口。(另外更簡單可以通過tcpdump來給Android手機抓包分析其接口,再用Wireshark來分析tcp包,不過你想要知道全部接口的話,可能需要一個個接口去調用,會比較麻煩,采用反編譯,可能集中在一個類中找到這些接口)。
首先要準備的工具:(了解更多反編譯工具可以去看雪論壇下載或者學習-Link)
APKTool是GOOGLE提供的APK編譯工具,需要JAVA運行環境??梢詫PK進行反編譯,使用它可以將其反編譯成非常接近打包前的原始格式。逆向AndroidManifest.xml、資源文件 resources.arsc以及將dex文件反編譯成可以調試的smali文件。修改后,可以將其編譯回apk文件。APKTool也可以用來漢化Android軟件然后重新打包發布。
官方:http://code.google.com/p/android-apktool/
解壓縮APKTool,并把要反編譯的APK放入目錄中
反編譯:
通過CMD進入上面的目錄,執行命令: apktool decode ZhaoBenShu.apk outdir
稍等片刻完成反編譯,反編譯后的文件會在outdir目錄下。
---outdir目錄結構
res :資源文件,跟adnroid工程目錄下的res基本一樣,各種UI圖片 XML布局文件 values xml文件(多了一個public.xml,有各個資源的id號(R.java中的id))
smail:這個是重點文件夾,里面都是smail格式文件,是Dalvik虛擬機執行的操作碼(Dalvik opcodes),這些操作嗎有自己的語法,如果有學過JNI的話, 這些語法還是比較容易看懂和理解的。AndroidManifest.xml:Android工程下的AndroidManifest.xml
apktool.yml:用于重打包。
smail語法:(全部語法請link)
smail中的數據類型簽名跟java中的是一樣的,如下。
B---byteC---charD---doubleF---floatI---intJ---longS---shortV---voidZ---boolean[XXX---arrayLxxx/yyy---object
smail代碼例子:
初看smail文件,可能會覺得有一些凌亂。不過只要把幾種語法弄懂了,就可以很好地閱讀smail文件。
smail比較常用語法 ( 非全部)分為: 賦值,取值,函數調用,if語句,返回值等。
賦值取值:
例子: iget-object v6, p0, Lcom/zbsh/code/clas/ClassSystem$9;->val$vBarCodes:Ljava/util/ArrayList;
分析:
iget個取值操作,i=instance,是用來instance filed(實例變量),object是類的意思。 v6是本地寄存器,p0在這里是代表this(在非static函數正代表this,在static函數中代表第一個參數)。Lcom/zbsh/code/clas/ClassSystem是表示包路徑為 Lcom/zbsh/code/clas下的ClassSystem類,->相當于C/C++的箭頭操作符,后面是類中的變量或者方法vBarCodes是ClassSystem中的一個變量,Ljava/util/ArrayList是vBarCodes這個變量的類型 (是java中類的簽名)
作用:
把ClassSystem中vBarCodes的值存放在寄存器v6中,vBarCodes的類型必須是對象,且是實例變量非靜態變量。
其中object可以是替換成基本數據類型:iget-boolean iget-byte iget-char iget-short等等。
同樣的:
sget- [type]用來獲取static變量。(少了一個p0,因為靜態變量是沒有this的)
aget-[type]用來獲取array類型。
[x]get vx, vy,把寄存器vy中的值賦給vx。
賦值:
同樣都有以下幾種:
iput-[type]
sput-[type]
aput-[type]
也支持寄存器和寄存器之間的賦值,寄存器和變量之間的賦值。
函數調用:
invoke-direct 調用private函數
invoke-super 調用父類函數
invoke-static 調用靜態函數
invoke-virtual 用于調用protected或public函數(相當于C++的虛函數,java的重載函數,只有protect和public能夠重載)
還有一種比較特殊的:invoke-xxxxx/range:參數多于5個的時候,要加/rang
例子:
invoke-virtual {v4, v1}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z
v4是this,代表 Ljava/lang/String的一個實例,v1是函數的第一個參數,在這里是調用放在V4寄存器中類型為Ljava/lang/String的實例的equal ()方法,并傳入參數v1,返回的結果是Z類型,也就是boolean類型。
如果是invoke-static{v4, v1}, 不同遇在于invoke-virtual {v4, v1}的是v4不是this,而是第一個參數。v1是第二個參數,所調用的方法需要兩個參數。
返回值:
獲取返回值:
move-result vx :把上一個方法返回的值,存在寄存器 vx中。
返回返回值:
return-void 沒返回。
return vx 返回寄存器中vx的值 。
if語句:
if-eq vx,vy,target:eq:equal 如果vx==xy 跳轉到target目標代碼,否則執行順序執行下一句代碼
if-ne vx,vy,target:nq :not equal 如果vx!=xy 跳轉到target目標代碼,否則執行順序執行下一句代碼
if-eqz vx,target:eqz : equal zero 如果vx==0 跳轉到target目標代碼,否則執行順序執行下一句代碼
if-nez vx,target:nez :not equal zero 如果vx!=0 跳轉到target目標代碼,否則執行順序執行下一句代碼
讀smail,找接口:
以搜索接口為例子:
根據文件命名找到GropZbshFind.smali這個文件,應該就是搜索Activity。
在其中有一段代碼:
# virtual methods
.method public onCreate(Landroid/os/Bundle;)V
.locals 3
.parameter "savedInstanceState"</font></p><p><font face="Courier New"> .prologue
.line 13
invoke-super {p0, p1}, Lcom/zbsh/code/thrd/GroupActivity;->onCreate(Landroid/os/Bundle;)V</font></p><p><font face="Courier New"> .line 14
const-class v0, Lcom/zbsh/code/ZbshFindMain;</font></p><p><font face="Courier New"> invoke-virtual {v0}, Ljava/lang/Class;->getName()Ljava/lang/String;</font></p><p><font face="Courier New"> move-result-object v0</font></p><p><font face="Courier New"> new-instance v1, Landroid/content/Intent;</font></p><p><font face="Courier New"> const-class v2, Lcom/zbsh/code/ZbshFindMain;</font></p><p><font face="Courier New"> invoke-direct {v1, p0, v2}, Landroid/content/Intent;->(Landroid/content/Context;Ljava/lang/Class;)V</font></p><p><font face="Courier New"> invoke-virtual {p0, v0, v1}, Lcom/zbsh/code/GropZbshFind;->startChildActivity(Ljava/lang/String;Landroid/content/Intent;)V</font></p><p><font face="Courier New"> .line 15
return-void
.end method
很明顯是通過startActivity來啟動ZbshFindMain這個Activity,
在ZbshFindMain中找到Onclick方法。
復制代碼代碼如下:
# virtual methods
.method public onClick(Landroid/view/View;)V
.........省略一坨代碼...........
iget-object v0, v5, Lcom/zbsh/code/clas/ClassSystem;->ipAddress:Ljava/lang/String;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> .line 199
.local v0, ipAddress:Ljava/lang/String;
new-instance v5, Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-static {v0}, Ljava/lang/String;->valueOf(Ljava/lang/Object;)Ljava/lang/String;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v6</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-direct {v5, v6}, Ljava/lang/StringBuilder;->(Ljava/lang/String;)V</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> const-string v6, "Find/GetBookList.aspx?a="</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> const-string v6, "gdut"</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> const-string v6, "&b="</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v6</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v5, p0, Lcom/zbsh/code/ZbshFindMain$4;->this$0:Lcom/zbsh/code/ZbshFindMain;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5}, Lcom/zbsh/code/ZbshFindMain;->getApplication()Landroid/app/Application;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> check-cast v5, Lcom/zbsh/code/clas/ApplZbsh;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v5, v5, Lcom/zbsh/code/clas/ApplZbsh;->iSystem:Lcom/zbsh/code/clas/ClassSystem;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v5, v5, Lcom/zbsh/code/clas/ClassSystem;->searchType:Ljava/lang/String;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v6, v5}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> const-string v6, "&c="</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v6</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v5, p0, Lcom/zbsh/code/ZbshFindMain$4;->this$0:Lcom/zbsh/code/ZbshFindMain;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5}, Lcom/zbsh/code/ZbshFindMain;->getApplication()Landroid/app/Application;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> check-cast v5, Lcom/zbsh/code/clas/ApplZbsh;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v5, v5, Lcom/zbsh/code/clas/ApplZbsh;->iSystem:Lcom/zbsh/code/clas/ClassSystem;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v5, v5, Lcom/zbsh/code/clas/ClassSystem;->inputKeywords:Ljava/lang/String;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v6, v5}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> const-string v6, "&d="</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> sget v6, Lcom/zbsh/code/clas/ClassDataParameter;->count:I</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> const-string v6, "&e="</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> sget v6, Lcom/zbsh/code/clas/ClassDataParameter;->page:I</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v3</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> .line 201
.local v3, urlPath:Ljava/lang/String;
iget-object v5, p0, Lcom/zbsh/code/ZbshFindMain$4;->this$0:Lcom/zbsh/code/ZbshFindMain;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5}, Lcom/zbsh/code/ZbshFindMain;->getApplication()Landroid/app/Application;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> move-result-object v5</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> check-cast v5, Lcom/zbsh/code/clas/ApplZbsh;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v5, v5, Lcom/zbsh/code/clas/ApplZbsh;->iSystem:Lcom/zbsh/code/clas/ClassSystem;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v6, p0, Lcom/zbsh/code/ZbshFindMain$4;->this$0:Lcom/zbsh/code/ZbshFindMain;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> iget-object v6, v6, Lcom/zbsh/code/ZbshFindMain;->mUIHandler:Landroid/os/Handler;</font></pre><pre class="brush: php; highlight: [5, 15]; html-script: true"><font face=""> invoke-virtual {v5, v0, v3, v6}, Lcom/zbsh/code/clas/ClassSystem;->GetFindOnThread(Ljava/lang/String;Ljava/lang/String;Landroid/os/Handler;)V
上面這段代碼,實現的是通過StringBuilder,通過append方法,拼成一個地址出來,再調用ClassSystem;->GetFindOnThread這個方法,傳入參數,進行一個異步圖書搜索的任務。
再從ClassDataParameter.smali中找到一些定義host地址常量。
我們可以拼出圖書搜索的接口是:http://222.200.98.173:7778/Find/GetBookList.aspx?a=&b=1&c=java&d=40&e=100
返回的是Json數據格式化下:
其次:
還可以通過反編譯更強大的用處是用來修改smali代碼,再重打包apk,來破解一些收費軟件,去除廣告之類,或者了解一些優秀軟件的實現邏輯。
新聞熱點
疑難解答