android 逆向后的smali文件中类名称的修改

1 我们会通过一些如apkdb的工具将apk反编译

2 反编译后发现smali文件就是文本的汇编代码

3 你将smali文件转为class文件后再转为java文件

4 你发现有个包叫做a.a 有个类也叫做a.a,这在eclipse中不允许了

5 你想把类a.a改为a.a1

6 使用字符串替换吧

===========================怎么办,请用字符串替换吧,下面的代码是一个mfc工程可搞定这件事(完整工程在我的资源中)



// smaliRefactoryDlg.cpp : 实现文件
//


#include "stdafx.h"
#include "smaliRefactory.h"
#include "smaliRefactoryDlg.h"
#include "memfile.h"
#include <vector>
#include "viewmem.h"




using namespace std;




#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// 用于应用程序“关于”菜单项的 CAboutDlg 对话框


class CAboutDlg : public CDialog
{
public:
CAboutDlg();


// 对话框数据
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持


// 实现
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()




// CsmaliRefactoryDlg 对话框








CsmaliRefactoryDlg::CsmaliRefactoryDlg(CWnd* pParent /*=NULL*/)
: CDialog(CsmaliRefactoryDlg::IDD, pParent)
, m_strPath(_T(""))
, m_strModifPath(_T(""))
, m_strModifyClass(_T(""))
, m_strModifyToClass(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
plist = 0;
}


void CsmaliRefactoryDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strPath);
DDX_Text(pDX, IDC_EDIT2, m_strModifPath);
DDX_Text(pDX, IDC_EDIT3, m_strModifyClass);
DDX_Text(pDX, IDC_EDIT4, m_strModifyToClass);
}


BEGIN_MESSAGE_MAP(CsmaliRefactoryDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CsmaliRefactoryDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CsmaliRefactoryDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CsmaliRefactoryDlg::OnBnClickedButton3)
ON_WM_SIZE()
ON_BN_CLICKED(IDC_BUTTON4, &CsmaliRefactoryDlg::OnBnClickedButton4)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CsmaliRefactoryDlg::OnNMDblclkList1)
ON_BN_CLICKED(IDC_BUTTON5, &CsmaliRefactoryDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &CsmaliRefactoryDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7, &CsmaliRefactoryDlg::OnBnClickedButton7)
ON_BN_CLICKED(IDC_BUTTON8, &CsmaliRefactoryDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON9, &CsmaliRefactoryDlg::OnBnClickedButton9)
END_MESSAGE_MAP()




// CsmaliRefactoryDlg 消息处理程序


BOOL CsmaliRefactoryDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);


if(pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);


if(!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
{
//初始化
plist = (CListCtrl*)(GetDlgItem(IDC_LIST1));


if(plist)
{
plist->SetExtendedStyle(plist->GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
plist->InsertColumn(0, L"path", LVCFMT_LEFT, 100);
plist->InsertColumn(1, L"file", LVCFMT_LEFT, 100);
plist->InsertColumn(2, L"class", LVCFMT_LEFT, 100);
plist->InsertColumn(3, L"needmodify", LVCFMT_LEFT, 100);
}
}
SetWindowPos(0, 0, 0, 800, 600, SWP_NOZORDER | SWP_NOMOVE);
return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}


void CsmaliRefactoryDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}


// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。


void CsmaliRefactoryDlg::OnPaint()
{
if(IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}


//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CsmaliRefactoryDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}




void CsmaliRefactoryDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog fd(TRUE);
int r = fd.DoModal();


if(r == IDOK)
{
m_strPath = fd.GetPathName();
UpdateData(FALSE);
}
}








void CsmaliRefactoryDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
//LoadFiles
if(plist)
{
plist->DeleteAllItems();
UpdateData(TRUE);
CString strf = m_strPath;


if(strf.IsEmpty())
return;


EnumFile(strf, L"*.*");
}
}


void CsmaliRefactoryDlg::EnumFile(CString strf , CString strExt)
{
CString strF1 = strf;
strF1 += L"\\*.*";
WIN32_FIND_DATA FND_DAT;
HANDLE hFind =::FindFirstFile(strF1, &FND_DAT);


if(hFind != INVALID_HANDLE_VALUE)
{
do
{
if(wcsicmp(FND_DAT.cFileName, L".") == 0 || wcsicmp(FND_DAT.cFileName, L"..") == 0)
continue;


if(FND_DAT.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
CString strF2 = strf;
strF2 += L"\\";
strF2 += FND_DAT.cFileName;
EnumFile(strF2, L"*.*");
}
else
{
CString file = strf;
file += L"\\";
file += FND_DAT.cFileName;
WCHAR szExt[_MAX_EXT] = {0};
WCHAR szFileN[_MAX_FNAME] = {0};
_wsplitpath_s(file, NULL, 0, NULL, 0, szFileN, _MAX_FNAME, szExt, _MAX_EXT);


if(wcsicmp(szExt, L".smali") == 0)
{
#ifdef _DEBUG
CString temp;
#endif


if(plist)
{
int rc = plist->GetItemCount();
plist->InsertItem(rc, file);
CString strFN;
strFN.Format(L"%s%s", szFileN, szExt);
plist->SetItemText(rc, 1, szFileN);
}
}
}
}
while(FindNextFile(hFind, &FND_DAT));


FindClose(hFind);
}
}






class FN_STRING
{
public:
CString m_strName;
CStringA m_strContent;//文件内容
FN_STRING()
{
}


void ReadFile(CString strFNamo)
{
memfile mf;
CStringA strFNameA;
strFNameA = strFNamo;
mf.loadfile(strFNameA);
CStringA aa((char*)mf.getPtr(), mf.getLen());
m_strContent = aa;
}


CString getClassName()
{
CString strRet;
CStringA strLine;
int nfl = -1;


while(strLine.IsEmpty())
{
int nfl2 = m_strContent.Find("\n", nfl + 1);


if(nfl2 >= nfl + 1)
{
strLine = m_strContent.Mid(nfl + 1, nfl2 - nfl - 1);
strLine.Trim();
nfl = nfl2;
}
else
{
strLine = m_strContent;
break;
}
}


//获取类名称
int nf1 = strLine.Find(".class");


if(nf1 >= 0)
{
int nf_public = strLine.Find("public", nf1 + 6);
int nf_interface = strLine.Find("interface", nf1 + 6);
int nf_abstract = strLine.Find("abstract", nf1 + 6);
int nf_final = strLine.Find("final", nf1 + 6);
int nf_enum = strLine.Find("enum", nf1 + 6);
int nf_synthetic = strLine.Find("synthetic", nf1 + 6);
int nf_annotation = strLine.Find("annotation", nf1 + 6);
int nfxj = 6;
int nfx = nf_public;


if(nf_interface > nfx)
{
nfx = nf_interface;
nfxj = 9;
}


if(nf_abstract > nfx)
{
nfx = nf_abstract;
nfxj = 8;
}


if(nf_final > nfx)
{
nfx = nf_final;
nfxj = 5;
}


if(nf_enum > nfx)
{
nfx = nf_enum;
nfxj = 4;
}


if(nf_synthetic > nfx)
{
nfx = nf_synthetic;
nfxj = 9;
}


if(nf_annotation > nfx)
{
nfx = nf_annotation;
nfxj = 10;
}


if(nfx > nf1 + 6)
{
int nfx2 = strLine.Find(";", nfx + nfxj);


if(nfx2 > nfx + nfxj)
{
CStringA sm = strLine.Mid(nfx + nfxj, nfx2 - nfx - nfxj);
strRet = sm;
}
}
else
{
int nfx2 = strLine.Find(";", nf1 + 6);


if(nfx2 >  nf1 + 6)
{
CStringA sm = strLine.Mid(nf1 + 6, nfx2 - nf1 - 6);
strRet = sm;
}
}
}


strRet.Trim();
return strRet;
}


protected:
private:
};


vector<FN_STRING> all_filemem;


void CsmaliRefactoryDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
//readfiles
if(plist)
{
all_filemem.clear();


for(int i = 0; i < plist->GetItemCount(); i++)
{
CString strFp = plist->GetItemText(i, 0);
FN_STRING fnstring;
fnstring.m_strName = plist->GetItemText(i, 0);
fnstring.ReadFile(strFp);
plist->SetItemText(i, 2, fnstring.getClassName());
all_filemem.push_back(fnstring);
}
}
}


void CsmaliRefactoryDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);


if(plist)
{
plist->SetWindowPos(0, 4, 70, cx - 8, cy - 200, SWP_NOZORDER);
}


CStatic* pstatic = NULL;
pstatic = (CStatic*)(GetDlgItem(IDC_STATIC1));


if(pstatic)
{
pstatic->SetWindowPos(0, 16, cy - 100, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}


pstatic = (CStatic*)(GetDlgItem(IDC_STATIC2));


if(pstatic)
{
pstatic->SetWindowPos(0, 16, cy - 100 + 30, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}


pstatic = (CStatic*)(GetDlgItem(IDC_STATIC3));


if(pstatic)
{
pstatic->SetWindowPos(0, 16 + 200 + 300 + 20, cy - 100 + 30 + 7, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}


CEdit* ped = NULL;
ped = (CEdit*)(GetDlgItem(IDC_EDIT2));


if(ped)
{
ped->SetWindowPos(0, 100, cy - 100 , 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}


ped = (CEdit*)(GetDlgItem(IDC_EDIT3));


if(ped)
{
ped->SetWindowPos(0, 100, cy - 100 + 30 , 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}


ped = (CEdit*)(GetDlgItem(IDC_EDIT4));


if(ped)
{
ped->SetWindowPos(0, 100 + 100 + 400, cy - 100 + 30 , 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}


CButton* pb = (CButton*)(GetDlgItem(IDC_BUTTON5));


if(pb)
{
pb->SetWindowPos(0, cx - 80, cy - 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}


// TODO: 在此处添加消息处理程序代码
}


void CsmaliRefactoryDlg::OnBnClickedButton4()
{
//  是否需要改类的名称
if(plist)
{
for(int i = 0; i < plist->GetItemCount(); i++)
{
CString fn2 = plist->GetItemText(i, 1);
CString fn1 = plist->GetItemText(i, 0);
WCHAR szFName[_MAX_FNAME] = {0};
WCHAR szDrive[_MAX_DRIVE] = {0};
WCHAR szDir[_MAX_DIR] = {0};
WCHAR szExt[_MAX_EXT] = {0};
_wsplitpath_s(fn1, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFName, _MAX_FNAME, szExt, _MAX_EXT);
CString strP;
strP.Format(L"%s%s%s", szDrive, szDir, szFName);


if(PathIsDirectory(strP))
{
plist->SetItemText(i, 3, L"need!");
}
else
{
plist->SetItemText(i, 3, L"");
}
}
}
}


void CsmaliRefactoryDlg::OnNMDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);


// TODO: 在此添加控件通知处理程序代码
if(plist)
{
int sl = plist->GetSelectionMark();
int rc = plist->GetItemCount();


if(sl < 0 || sl >= rc)
return;


//
m_strModifPath = plist->GetItemText(sl, 0);
m_strModifyClass = plist->GetItemText(sl, 2);


if(m_strModifyClass.Find(L"$") >= 0)
{
m_strModifyToClass = m_strModifyClass;
m_strModifyToClass.Replace(L"$", L"_");
}
else
{
m_strModifyToClass = m_strModifyClass + L"1";
}


UpdateData(FALSE);
}


*pResult = 0;
}


void CsmaliRefactoryDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知处理程序代码
//替换引用引用
UpdateData(TRUE);
Changeclass(m_strModifPath, m_strModifyClass, m_strModifyToClass);
}


void CsmaliRefactoryDlg::Changeclass(CString m_strModifPath, CString m_strModifyClass,
                                     CString m_strModifyToClass)
{
if(plist)
{
//将一个文件的文件名也改了,所有文件中引用这个类的名字也改了
CStringA classA;
CStringA classATo;
classA = m_strModifyClass;
classATo = m_strModifyToClass;
classA += ";";
classATo += ";";


for(size_t i = 0; i < all_filemem.size(); i++)
{
all_filemem[i].m_strContent.Replace(classA, classATo);
}


//文件名也要换


for(int i = 0; i < plist->GetItemCount(); i++)
{
CString str1 = plist->GetItemText(i, 0);


if(str1.CompareNoCase(m_strModifPath) == 0)
{
//取得短类名
CString shortName;
int rf = m_strModifyToClass.ReverseFind(L'/');


if(rf >= 0)
{
shortName = m_strModifyToClass.Mid(rf + 1);
}
else
{
if(m_strModifyToClass[0] == L'L')
{
shortName = m_strModifyToClass.Mid(1);
}
else
{
shortName = m_strModifyToClass;
}
}


//
WCHAR szFName[_MAX_FNAME] = {0};
WCHAR szDrive[_MAX_DRIVE] = {0};
WCHAR szDir[_MAX_DIR] = {0};
WCHAR szExt[_MAX_EXT] = {0};
_wsplitpath_s(m_strModifPath, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFName, _MAX_FNAME, szExt,
             _MAX_EXT);
CString strNewFile;
strNewFile.Format(L"%s%s%s%s", szDrive, szDir, shortName, szExt);
plist->SetItemText(i, 0, strNewFile);


for(size_t i = 0; i < all_filemem.size(); i++)
{
if(all_filemem[i].m_strName.CompareNoCase(m_strModifPath) == 0)
{
all_filemem[i].m_strName = strNewFile;
}
}
}
}
}
}


void CsmaliRefactoryDlg::OnBnClickedButton6()
{
CString strModifPath;
// TODO: 在此添加控件通知处理程序代码
//看内存


if(plist)
{
int sl = plist->GetSelectionMark();
int rc = plist->GetItemCount();


if(sl < 0 || sl >= rc)
return;


//
strModifPath = plist->GetItemText(sl, 0);


for(size_t i = 0; i < all_filemem.size(); i++)
{
if(all_filemem[i].m_strName.CompareNoCase(strModifPath) == 0)
{
viewmem vm(this);
vm.m_strMm = all_filemem[i].m_strContent;
vm.DoModal();
}
}
}
}


void CsmaliRefactoryDlg::OnBnClickedButton7()
{
// TODO: 在此添加控件通知处理程序代码
//全部baocun
CString strout = m_strPath + L"_out";


for(size_t i = 0; i < all_filemem.size(); i++)
{
CString strPath = all_filemem[i].m_strName;
CStringA strContentA = all_filemem[i].m_strContent;
strPath.Replace(m_strPath, strout);
WriteFileZZ(strPath, strContentA);
}
}


void CsmaliRefactoryDlg::WriteFileZZ(CString strPath, CStringA strContentA)
{
WCHAR szPath[1024] = {0};


for(int i = 0; i < strPath.GetLength(); i++)
{
szPath[i] = strPath[i];


if(strPath[i] == L'\\' || strPath[i] == L'/')
{
CreateDirectory(szPath, NULL);
}
}


CStringA strA;
strA = strPath;
FILE* fp = fopen(strA, "wb+");


if(fp)
{
for(int i = 0; i < strContentA.GetLength(); i++)
fputc(strContentA[i], fp);


fclose(fp);
}
}


void CsmaliRefactoryDlg::OnBnClickedButton8()
{
// TODO: 在此添加控件通知处理程序代码
if(!plist)
return;


for(int i = 0; i < plist->GetItemCount(); i++)
{
CString x3 = plist->GetItemText(i, 3);


if(x3.Find(L"need") >= 0)
{
m_strModifPath = plist->GetItemText(i, 0);
m_strModifyClass = plist->GetItemText(i, 2);


if(m_strModifyClass.Find(L"$") >= 0)
{
m_strModifyToClass = m_strModifyClass;
m_strModifyToClass.Replace(L"$", L"_");
}
else
{
m_strModifyToClass = m_strModifyClass + L"1";
}


UpdateData(FALSE);
OnBnClickedButton5();
}
}


MessageBox(L"OK!");
}


void CsmaliRefactoryDlg::OnBnClickedButton9()
{
if(!plist)
return;


for(int i = 0; i < plist->GetItemCount(); i++)
{
CString x2 = plist->GetItemText(i, 2);


if(x2.Find(L"$") >= 0)
{
m_strModifPath = plist->GetItemText(i, 0);
m_strModifyClass = plist->GetItemText(i, 2);
m_strModifyToClass = m_strModifyClass;
m_strModifyToClass.Replace(L"$", L"_");
UpdateData(FALSE);
OnBnClickedButton5();
}
}


MessageBox(L"OK!");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值