iOS逆向----增删改查plist文件

Posted 普通网友

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS逆向----增删改查plist文件相关的知识,希望对你有一定的参考价值。

  • plist 是 Mac 种非常普遍的一种文件格式,类似 xml,通过键值对的方式来进行一些配置。
  • PlistBuddy 则是 Mac 自带的专门解析 plist 的工具。

由于Macos并未在环境变量中默认配置PlistBuddy,所以需要通过绝对路径来引用(当然你也可以自己配置一下环境变量):

~ ❯ /usr/libexec/PlistBuddy --help
Command Format:
    Help - Prints this information
    Exit - Exits the program, changes are not saved to the file
    Save - Saves the current changes to the file
    Revert - Reloads the last saved version of the file
    Clear [<Type>] - Clears out all existing entries, and creates root of Type
    Print [<Entry>] - Prints value of Entry.  Otherwise, prints file
    Set <Entry> <Value> - Sets the value at Entry to Value
    Add <Entry> <Type> [<Value>] - Adds Entry to the plist, with value Value
    Copy <EntrySrc> <EntryDst> - Copies the EntrySrc property to EntryDst
    Delete <Entry> - Deletes Entry from the plist
    Merge <file.plist> [<Entry>] - Adds the contents of file.plist to Entry
    Import <Entry> <file> - Creates or sets Entry the contents of file

Entry Format:
    Entries consist of property key names delimited by colons.  Array items
    are specified by a zero-based integer index.  Examples:
        :CFBundleShortVersionString
        :CFBundleDocumentTypes:2:CFBundleTypeExtensions

Types:
    string
    array
    dict
    bool
    real
    integer
    date
    data

Examples:
    Set :CFBundleIdentifier com.apple.plistbuddy
        Sets the CFBundleIdentifier property to com.apple.plistbuddy
    Add :CFBundleGetInfoString string "App version 1.0.1"
        Adds the CFBundleGetInfoString property to the plist
    Add :CFBundleDocumentTypes: dict
        Adds a new item of type dict to the CFBundleDocumentTypes array
    Add :CFBundleDocumentTypes:0 dict
        Adds the new item to the beginning of the array
    Delete :CFBundleDocumentTypes:0 dict
        Deletes the FIRST item in the array
    Delete :CFBundleDocumentTypes
        Deletes the ENTIRE CFBundleDocumentTypes array

新建plist

~/Desktop ❯ /usr/libexec/PlistBuddy test.plist
File Doesn't Exist, Will Create: test.plist
Command: save
Saving...
Command: exit
~/Desktop ❯ ls | grep test.plist
test.plist

添加

添加普通字段

Add :字段名 类型 值

~ ❯ /usr/libexec/PlistBuddy -c 'Add :Version string 1.0' test.plist
~ ❯ /usr/libexec/PlistBuddy -c "print" test.plist
Dict 
    Version = 1.0

添加数组字段

先添加数组名
Add :数组名 array

~ ❯ /usr/libexec/PlistBuddy -c 'Add :Applications array' test.plist

再添加数组的值
Add :数组名: 类型 值

~ ❯ /usr/libexec/PlistBuddy -c 'Add :Applications: string app1' test.plist
~ ❯ /usr/libexec/PlistBuddy -c 'Add :Applications: string app2' test.plist
~ ❯ /usr/libexec/PlistBuddy -c 'Add :Applications: string app3' test.plist
~ ❯ /usr/libexec/PlistBuddy -c "print" test.plist
Dict 
    Version = 1.0
    Applications = Array 
        app1
        app2
        app3
    

添加字典字段

先添加字典名
Add :字典名 dict

~ ❯ /usr/libexec/PlistBuddy -c 'Add :Users dict' test.plist

再添加字典的key和value:
Add :字典名:key 类型 值

~ ❯ /usr/libexec/PlistBuddy -c 'Add :Users:Name string Bob' test.plist
~ ❯ /usr/libexec/PlistBuddy -c 'Add :Users:sex Bool ture' test.plist
~ ❯ /usr/libexec/PlistBuddy -c 'Add :Users:Age integer 20' test.plist
~ ❯ /usr/libexec/PlistBuddy -c "print" test.plist
Dict 
    Users = Dict 
        Name = Bob
        sex = false
        Age = 20
    
    Version = 1.0
    Applications = Array 
        app1
        app2
        app3
    

查看plsit

~ ❯ /usr/libexec/PlistBuddy -c "print" test.plist
Dict 
    Users = Dict 
        Name = Bob
        sex = false
        Age = 20
    
    Version = 1.0
    Applications = Array 
        app1
        app2
        app3
    

根据key查value

~ ❯ /usr/libexec/PlistBuddy -c 'Print :Users' test.plist
Dict 
    Name = Bob
    sex = false
    Age = 20

查数组的特定位置

~ ❯ /usr/libexec/PlistBuddy -c 'Print :Applications:2' test.plist
app3

修改

~ ❯ /usr/libexec/PlistBuddy -c 'Set :Applications:2 "reset application 2"' test.plist
~ ❯ /usr/libexec/PlistBuddy -c 'Print :Applications:2' test.plist
reset application 2

删除

~ ❯ /usr/libexec/PlistBuddy -c 'Delete :Applications' test.plist
~ ❯ /usr/libexec/PlistBuddy -c "print" test.plist
Dict 
    Users = Dict 
        Name = Bob
        sex = false
        Age = 20
    
    Version = 1.0

合并

合并test 2.plist 到 test.plist

~ ❯ /usr/libexec/PlistBuddy -c 'print' test.plist
Dict 
    Users = Dict 
        Name = Bob
        sex = false
        Age = 20
    
    Version = 1.0

~ ❯ /usr/libexec/PlistBuddy -c 'print' test\\ 2.plist
Dict 
    Users = Dict 
        Name = Bob
        sex = false
        Age = 20
    
    Version = 1.0
    Applications = Array 
        app1
        app2
        reset application 2
    

~ ❯ /usr/libexec/PlistBuddy -c 'Merge test\\ 2.plist' test.plist
Duplicate Entry Was Skipped: Users
Duplicate Entry Was Skipped: Version
~ ❯ /usr/libexec/PlistBuddy -c 'print' test.plist
Dict 
    Users = Dict 
        Name = Bob
        sex = false
        Age = 20
    
    Version = 1.0
    Applications = Array 
        app1
        app2
        reset application 2
    

以上是关于iOS逆向----增删改查plist文件的主要内容,如果未能解决你的问题,请参考以下文章

iOS CoreData 增删改查

用JAVA实现对txt文件文本增删改查

java io 简单实现对数据的增删改查

mybatis逆向工程之动态web项目

java io流对文件的增删改查

IOS sqlite数据库增删改查