[iOS]混淆-类名混淆

使用runtime混淆类名

objc_runtime_name属性可以在编译时,将Class或Protocol指定为另一个名字,并且新名字不受命名规范制约,可以以数字开头。
脚本如下:
#encoding:utf-8 # 支持中文输入
import re
import os
import string
import random
s = os.sep
 
##获取当前工作目录
root = os.path.abspath('.')
#需要忽略的文件后缀
ignoredTrail = [".py",".c"]
#需要忽略的文件夹
ignoredDirPath   = ["lib",".git"]
#需要匹配的文件后缀
containTrail   = [".h",".m"]
 
 
#获取文件名和后缀
def getFileNameAndExt(filename):
 (filepath,tempfilename) = os.path.split(filename);
 (shotname,extension) = os.path.splitext(tempfilename);
 return shotname,extension
 
#是否是需要根据后缀忽略的文件
def isIgnoredByTrail(filePath):
  file = getFileNameAndExt(filePath)
  for trail in ignoredTrail:
    if file[1] == trail:
      # print "忽略文件:" + filePath
      return bool(1)
  return bool(0)
 
#是否是需要根据后缀匹配的文件
def isContainByTrail(filePath):
  file = getFileNameAndExt(filePath)
  for trail in containTrail:
    if file[1] == trail:
      # print "忽略文件:" + filePath
      return bool(1)
  return bool(0)
  
#是否根据文件夹路径葫芦
def isIgnoredByDirPath(dirpath):
  for path in ignoredDirPath:
    if string.find(dirpath,path) != -1:
      # print "忽略目录:" + dirpath
      return bool(1)
    else:
      # print "匹配目录:" + dirpath
      continue
  return bool(0)
 
#获取所有文件名
def getAllFiles(path):
 allfile=[]
 for dirpath,dirnames,filenames in os.walk(path):
  # print 'dirpath: ' + dirpath 
  if isIgnoredByDirPath(dirpath):
    continue
     
  for name in filenames:
    if isIgnoredByTrail(name):
      continue
    elif isContainByTrail(name):
      allfile.append(os.path.join(dirpath, name))
    
 return allfile
 
 
#获取所有XIB文件名
def getAllFilesXIB(path):
 allfile=[]
 for dirpath,dirnames,filenames in os.walk(path):
  # print 'dirpath: ' + dirpath 
  if isIgnoredByDirPath(dirpath):
    continue
     
  for name in filenames:
    if name.endswith('.xib'):
      allfile.append(os.path.join(dirpath, name))
    
 return allfile 
 
def randomString():
 
  return string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).replace(" ","") + (''.join(random.sample(string.ascii_letters + string.digits, 8)))
 
 
 
endVC = ['VC','vc','viewController','ViewController', 'VIEWCONTROLLER']
 
endData = ['Data', 'data', 'Model','MODEL','MO']
 
endView = ['VIEW', 'View', 'view']
 
endEngine = ['Engine','engine','Manager','MA','manager','Center','center']
 
endCell = ['Cell','cell','CELL']
 
endDelegate = ['Delegate','delegate','del','gate','Protocol','PROTOCOL','procotol']
 
endTool = ['Tool', 'tool', 'Util','util','helper','Helper','Browser','browser','Photo','photo','LIB','lib', 'Image','image','Button']
 
endArray = [endVC,endData,endView,endEngine,endCell,endDelegate,endTool]
 
 
ranDomName1 = ['ZYH','ZYH','ZYH','ZYH','ZYH','ZYH','SYSTEM','COA','TOP','LEFT','MON','CON','ZZZ','KKK','OPP','AAA','BBB','LOM','CIRCLE','PLUS','MEE','NES','MY','YMD','ALIBABA']
 
ranDomName2 = ['The','Fog','TR','Haode','Xitong','Lunbotu','Shitu','Picture','Photo','Xieyi','Text','Rich','Line','Sys','Turn','Rever', 'Tiaozhuan','Control','Noname','Name','Waker','Date','Weak','XML','Hob','File','Function','Fanfa','Hob','COO','Program','Net','Device','SheBei','Didian','People','Thing','Shijian','Shang','Shangbao','Other','Yanse','Color','Object','Animate','Dongzuo','Word','Act','Different','Plus','Sub', 'Age','Newer','Login','Register','Huanxiao','Length','Count','Jihuoma','Fly','Loop','Star','Get','Set','Get','Set','Get','Set','Get','Set','Shenhe','Read','Write','Laker','Shanghai','Some','Get','Set','In','This','That','String','Number','AAA','Main','Work','Enable','Disable','Check','Ment']
 
ranDomName3 = ['Welcome','In' ,'Order','To', 'Get','First','1','2','3','4','5','6', 'Request', 'Some', 'Basic', 'Information','One','Comes','Three' 'Pieces', 'The','Suite', 'Head','LLV','MMO','RPG','Game','Random','Test','Length','FPS','Shumu','Tree','Heap','Value','Number','Ana','Bit','Use','For','Date','Shangbao','Other','Yanse','Color','Object','Button','View','Image','View','Load','Creat']
 
ranDomName4 = ['Welcome','In' ,'Order','To', 'Get','First','1','2','3','4','5','6', 'Object','Figure','Cele','Solider','Holiday','Calcute','Mean','Wee','Probably','Gate','World','Car','Rp','Age','Giving','Doing','Play','Add','Vce','Change','JSON','Standar','Adavance','Country','Check','SSS','Hos','Vic','Ter','Style','UI','UI','Style','CSS','Script','Test','Text','Tion','Animate','Button','View','Image','Image','Section','View','Section','Load','Mon']
 
 
ranDomName = [ranDomName1, ranDomName2,ranDomName3, ranDomName4]
 
dicOriginName = {}
dic = {}
def rechangeStr(matched):
  value = matched.group(2)
 
  randomNum = random.randint(2,4)
 
  if randomNum == 2:
    if random.random() < 0.38:
      randomNum = 3
 
  rname = ''
 
  for index in range(randomNum):
    rnameArr = ranDomName[index]
 
    rname += rnameArr[random.randint(0,len(rnameArr)-1)]
  
  endStr = ''
 
  for ends in endArray:
    find = False
    for endValue in ends:
      if value.endswith(endValue):
        endStr = endValue
        find = True
        break
 
    if find:
      break
 
 
 
  rname += endStr
 
  if dic.has_key(rname):
    rname = randomString()
 
  # jsonmodel用protocol与model同名来作处理的,所以要同名替换
  if dicOriginName.has_key(value):
    rname = dicOriginName[value]
 
  dic[rname] = 1
 
  dicOriginName[value] = rname   
 
  return '\n__attribute__((objc_runtime_name("' + rname + '"))) ' + matched.group(1)  
 
 
 
#重命名
def rename(oldName,newName,filePath):
  a = open(filePath,'r') #打开所有文件
  str = a.read()
 
  # searchObj = re.search(r'\n[ \t]*(@interface[ \t]+([a-zA-Z0-9-_]+)[ \t]*\:[ \t]*[a-zA-Z0-9-_]+[ \t]*\n)', str, re.M|re.I)
 
  # if searchObj:
  #   print searchObj.group(0)
  #   print rechangeStr(searchObj)
 
  # searchObj2 = re.search(r'\n[ \t]*(@protocol[ \t]+([a-zA-Z0-9-_]+)[ \t]*(<[a-zA-Z0-9-_]+>)*\n)', str, re.M|re.I)
 
  # if searchObj2:
  #   print searchObj2.group(0)
  #   print rechangeStr(searchObj2)
 
 
  str = re.sub(r'\n[ \t]*(@interface[ \t]+([a-zA-Z0-9-_]+)[ \t]*\:[ \t]*[a-zA-Z0-9-_]+[ \t]*(<[a-zA-Z0-9-_, \t]+>)*[ \t]*\n)', rechangeStr, str)
 
  str = re.sub(r'\n[ \t]*(@protocol[ \t]+([a-zA-Z0-9-_]+)[ \t]*(<[a-zA-Z0-9-_]+>)*\n)', rechangeStr, str)
 
 
  b = open(filePath,'w')
  b.write(str) #再写入
  b.close() #关闭文件
  print "============" + filePath + " finished"
 
 
 
def rechangeXIBStr(matched):
  value = matched.group(1)
 
  rname = value
 
  if dicOriginName.has_key(value):
    rname = dicOriginName[value]
 
  return 'customClass="' + rname + '"'
 
#重命名XIB里的类
def renameXIBWithHistory(filePath):
  a = open(filePath,'r') #打开所有文件
  str = a.read()
 
  str = re.sub(r'customClass="([a-zA-Z0-9-_]+)"', rechangeXIBStr, str, re.M|re.I)
 
 
  b = open(filePath,'w')
  b.write(str) #再写入
  b.close() #关闭文件
  print "============" + filePath + " finished"
 
 
 
 
def canChange(filePath):
  file = getFileNameAndExt(filePath)
  if file[1] != '.h':
    return bool(0)
 
  return bool(1)
 
 
 
fileList = getAllFiles(root)
 
print len(fileList)
 
for file in fileList:
  print file
  rename('','',file)
 
fileListXIB = getAllFilesXIB(root)
for filexib in fileListXIB:
  renameXIBWithHistory(filexib)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,992评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,212评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,535评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,197评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,310评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,383评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,409评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,191评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,621评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,910评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,084评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,763评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,403评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,083评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,318评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,946评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,967评论 2 351