Chameleon

Chameleon Commit Details

Date:2017-03-25 13:41:11 (7 years 1 month ago)
Author:ErmaC
Commit:2868
Parents: 2867
Message:Added XMLConvertTagPtrToXMLString and ability to convert Tag to property list (Micky1979)
Changes:
M/branches/ErmaC/Enoch/i386/libsaio/xml.c
M/branches/ErmaC/Enoch/i386/libsaio/xml.h

File differences

branches/ErmaC/Enoch/i386/libsaio/xml.c
14621462
14631463
14641464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
XMLFreeTag(tmpTag);
return false;
}
//==========================================================================
// Micky1979
// XMLConvertTagPtrToXMLString
// convert back a TagPtr to a xml representation
// Here is still w/o header and footer
char *XMLConvertTagPtrToXMLString(TagPtr aDict, long parentType, int indent)
{
indent ++;
// TODO check if we can use a buffer for the booter stuff already reserved somewhere
char *string = "";
char *ind = "";
for (int i = 0; i < indent; i++)
{
char *ind2 = malloc(strlen(ind) + strlen("\t") +1);
strcpy(ind2, ind);
ind2[strlen(ind)] = '\t';
ind2[strlen(ind) +1] = '\0';
ind = ind2;
}
unsigned long length = 0;
int count = XMLTagCount(aDict);
while(count)
{
// parsing only supported tags
char *key = NULL;
TagPtr sub = NULL;
if (!XMLIsArray(aDict))
{
key = XMLCastString(XMLGetKey(aDict, count));
sub = XMLGetProperty(aDict, key);
}
else
{
key = "";
sub = XMLGetElement( aDict, count-1);
}
if(sub)
{
if(XMLIsData(sub))
{
char *temp;
if (parentType == kTagTypeArray)
{
length = (strlen(ind) +
strlen(sub->string ? sub->string : "") +
strlen("<data>") +
strlen("</data>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<data>%s</data>\n", string, ind, sub->string ? sub->string : "");
} else {
length = (strlen(ind) +
strlen(key) +
strlen("<key>") +
strlen("</key>\n") +
strlen(ind) +
strlen(sub->string ? sub->string : "") +
strlen("<data>") +
strlen("</data>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s<data>%s</data>\n",
string, ind, key, ind, sub->string ? sub->string : "");
}
temp[strlen(string) + length] = '\0';
string = temp;
}
else if(XMLIsString(sub))
{
char *temp;
if (parentType == kTagTypeArray)
{
length = (strlen(ind) +
strlen("<string>") +
strlen(XMLCastString(sub) ? XMLCastString(sub) : "") +
strlen("</string>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<string>%s</string>\n",
string, ind, XMLCastString(sub) ? XMLCastString(sub) : "");
} else {
length = (strlen(ind) +
strlen(key) +
strlen("<key>") +
strlen("</key>\n") +
strlen(ind) +
strlen("<string>") +
strlen(XMLCastString(sub) ? XMLCastString(sub) : "") +
strlen("</string>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s<string>%s</string>\n",
string, ind, key, ind, XMLCastString(sub) ? XMLCastString(sub) : "");
}
temp[strlen(string) + length] = '\0';
string = temp;
}
else if(XMLIsInteger(sub))
{
// 'real' (float/dowble) cannot be parsed. only integers
int x = XMLCastInteger(sub);
char xBuf[snprintf( NULL, 0, "%d", x )];
snprintf( xBuf, snprintf( NULL, 0, "%d", x ) +1, "%d", x );
char *temp;
if (parentType == kTagTypeArray)
{
length = (strlen(ind) +
strlen("<integer>") +
sizeof(xBuf) +
strlen("</integer>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<integer>%s</integer>\n", string, ind, (char *)xBuf);
} else {
length = (strlen(ind) +
strlen(key) +
strlen("<key>") +
strlen("</key>\n") +
strlen(ind) +
strlen("<integer>") +
sizeof(xBuf) +
strlen("</integer>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s<integer>%s</integer>\n",
string, ind, key, ind, (char *)xBuf);
}
temp[strlen(string) + length] = '\0';
string = temp;
}
else if(XMLIsBoolean(sub))
{
char *temp;
int v = XMLCastBoolean(sub);
if (parentType == kTagTypeArray)
{
length = (strlen(ind) +
strlen(v ? "<true/>\n" : "<false/>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s%s", string, ind, v ? "<true/>\n" : "<false/>\n");
} else {
length = (strlen(ind) +
strlen(key) +
strlen("<key>") +
strlen("</key>\n") +
strlen(ind) +
strlen(v ? "<true/>\n" : "<false/>\n") +
1);
temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s%s",
string, ind, key, ind, v ? "<true/>\n" : "<false/>\n");
}
temp[strlen(string) + length] = '\0';
string = temp;
}
else if (XMLIsDict(sub) || XMLIsArray(sub)) // its a contenitor..
{
int subCount = XMLTagCount(sub);
char *otag = subCount <=0 ? "" : XMLIsDict(sub) ? "<dict>\n" : "<array>\n";
char *ctag = subCount <=0 ? XMLIsDict(sub) ? "<dict/>\n" :
"<array/>\n" : XMLIsDict(sub) ? "</dict>\n" : "</array>\n";
char *dict = NULL;
dict = NULL;
dict = XMLConvertTagPtrToXMLString(sub, sub->type, indent);
length = (strlen(ind) +
strlen(key) +
strlen("<key>") +
strlen("</key>\n") +
strlen(ind) +
strlen(otag) +
strlen(dict) +
strlen(ind) +
strlen(ctag) +
1);
char *temp = malloc(strlen(string) + length);
snprintf(temp, length+strlen(string), "%s%s<key>%s</key>\n%s%s%s%s%s\n",
string, ind, key, ind, otag, dict, ind, ctag);
temp[strlen(string) + length] = '\0';
string = temp;
}
}
count--;
}
if(strlen(ind))
{
free(ind);
} // not mallocated if ""
return string;
}
// XMLConvertTagPtrToPropertyList_v1
// convert back a TagPtr to a xml representation
// with header and footer
char *XMLConvertTagPtrToPropertyList_v1(TagPtr aDict)
{
if (!aDict)
{
return NULL;
}
char *s = XMLConvertTagPtrToXMLString(aDict, aDict->type, 1);
if (s == NULL)
{
return NULL;
}
unsigned long length = ((strlen(kXMLv1) -1 /* -1 for %s included */) +
strlen(s) +
1);
char *propertyList = malloc(length);
snprintf(propertyList, length, kXMLv1, s);
if (strlen(s))
{
free(s);
}
return (char *)propertyList;
}
// XMLGenerateKextInjectorFromTag
// generate an full Info.plist to create a kext injector
// sDict should contains all the contents of IOKitPersonalities
// personalityName is a funcy name. Must not be a real kext name
// OSBundleRequired should be the same of the real kext you want to hack (Root, Safe Boot, Network-Root etc.)
// can also be NULL or "" assuming the target kext does not have this key.
char * XMLGenerateKextInjectorFromTag(TagPtr aDict, char *personalityName, char *OSBundleRequired)
{
if (!aDict || personalityName == NULL)
{
return NULL;
}
char *IOKitPersonalities = XMLConvertTagPtrToXMLString(aDict, aDict->type, 1);
if (IOKitPersonalities == NULL)
{
return NULL;
}
char *OBRkey = OSBundleRequired == NULL ? "" : OSBundleRequired;
char *propertyList;
unsigned long length = 0;
if (strlen(OBRkey))
{
length = (strlen("\t<key>OSBundleRequired</key>\n\t<string>") +
strlen(OBRkey) +
strlen("</string>\n") +
1);
char temp[length];
snprintf(temp, length, "\t<key>OSBundleRequired</key>\n\t<string>%s</string>\n", OBRkey);
unsigned long mlength = ((strlen(kFakeInjectorKext) -8 /* %s x 8 */) +
strlen(personalityName) +
strlen(personalityName) +
strlen(IOKitPersonalities) +
length +
1);
propertyList = malloc(mlength);
snprintf(propertyList, mlength, kFakeInjectorKext, personalityName, personalityName, IOKitPersonalities, temp);
}
else
{
unsigned long mlength = ((strlen(kFakeInjectorKext) -8 /* %s x 8 */) +
strlen(personalityName) +
strlen(personalityName) +
strlen(IOKitPersonalities) +
1);
propertyList = malloc(mlength);
snprintf(propertyList, mlength, kFakeInjectorKext, personalityName, personalityName, IOKitPersonalities, "");
}
if (strlen(IOKitPersonalities))
{
free(IOKitPersonalities);
}
return propertyList;
}
branches/ErmaC/Enoch/i386/libsaio/xml.h
7272
7373
7474
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
75117
76118
77119
#define kPropIOKitPersonalities("IOKitPersonalities")
#define kPropIONameMatch("IONameMatch")
// Micky1979
// kXMLv1 require one argument
#define kXMLv1 "<\?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
<plist version=\"1.0\">\n\
<dict>\n%s\n\
</dict>\n\
</plist>"
// Micky1979
// kFakeInjectorKext require 4 arguments (a generic name (used twice) + IOKitPersonalities + OSBundleRequired ("" if not needed)):
// name, name, IOKitPersonalities, OSBundleRequired
#define kFakeInjectorKext "<\?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
<plist version=\"1.0\">\n\
<dict>\n\
<key>CFBundleDevelopmentRegion</key>\n\
<string>English</string>\n\
<key>CFBundleIdentifier</key>\n\
<string>com.Enoch.plist.%s</string>\n\
<key>CFBundleInfoDictionaryVersion</key>\n\
<string>6.0</string>\n\
<key>CFBundleName</key>\n\
<string>%s</string>\n\
<key>CFBundlePackageType</key>\n\
<string>KEXT</string>\n\
<key>CFBundleSignature</key>\n\
<string>\?\?\?\?</string>\n\
<key>CFBundleVersion</key>\n\
<string>1.0.0</string>\n\
<key>IOKitPersonalities</key>\n\
<dict>\n%s\
</dict>\n%s\
</dict>\n\
</plist>\n"
// Micky1979 following 3 funtions are to convert a TagPtr to a property list v1 and to create kexts injector
char *XMLConvertTagPtrToXMLString(TagPtr aDict, long parentType, int indent);
char *XMLConvertTagPtrToPropertyList_v1(TagPtr aDict);
char *XMLGenerateKextInjectorFromTag(TagPtr aDict, char *personalityName, char *OSBundleRequired);
extern long gImageFirstBootXAddr;
extern long gImageLastKernelAddr;

Archive Download the corresponding diff file

Revision: 2868