当前位置: 首页 > news >正文

Chromium 进程降权和提权模拟示例c++

 一、背景知识概念参考微软链接:

强制完整性控制 - Win32 应用程序 |Microsoft 学习

授权) (模拟级别 - Win32 apps | Microsoft Learn

DuplicateTokenEx 函数 (securitybaseapi.h) - Win32 apps | Microsoft Learn

本文主要演示 low, medium, high, and system 四种权限创建和使用例子:

 Windows defines four integrity levels: low, medium, high, and system

integrity levels定义如下:

C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um\winnt.h

#define SECURITY_MANDATORY_LABEL_AUTHORITY          {0,0,0,0,0,16}
#define SECURITY_MANDATORY_UNTRUSTED_RID            (0x00000000L)
#define SECURITY_MANDATORY_LOW_RID                  (0x00001000L)
#define SECURITY_MANDATORY_MEDIUM_RID               (0x00002000L)
#define SECURITY_MANDATORY_MEDIUM_PLUS_RID          (SECURITY_MANDATORY_MEDIUM_RID + 0x100)
#define SECURITY_MANDATORY_HIGH_RID                 (0x00003000L)
#define SECURITY_MANDATORY_SYSTEM_RID               (0x00004000L)
#define SECURITY_MANDATORY_PROTECTED_PROCESS_RID    (0x00005000L)

 或者字符串也一样:

 //  INTEGRITY_LEVEL_SYSTEM:      "S-1-16-16384" System Mandatory Level//  INTEGRITY_LEVEL_HIGH:        "S-1-16-12288" High Mandatory Level//  INTEGRITY_LEVEL_MEDIUM:      "S-1-16-8192"  Medium Mandatory Level//  INTEGRITY_LEVEL_MEDIUM_LOW:  "S-1-16-6144"//  INTEGRITY_LEVEL_LOW:         "S-1-16-4096"  Low Mandatory Level//  INTEGRITY_LEVEL_BELOW_LOW:   "S-1-16-2048"//  INTEGRITY_LEVEL_UNTRUSTED:   "S-1-16-0"     Untrusted Mandatory Level

   Low (SID: S-1-16-4096),
 Medium (SID: S-1-16-8192),
 High (SID: S-1-16-12288)
 System (SID: S-1-16-16384). 

二、代码:

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <windows.h>
#include <iostream>#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/threading/thread.h"
#include "base/win/access_token.h"
#include "base/win/scoped_handle.h"
#include "base/win/sid.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace {// Copies the process token making it a primary impersonation token.
// The returned handle will have |desired_access| rights.
bool CopyProcessToken(DWORD desired_access,base::win::ScopedHandle* token_out) {HANDLE temp_handle;if (!::OpenProcessToken(::GetCurrentProcess(),TOKEN_DUPLICATE | desired_access, &temp_handle)) {LOG(ERROR) << "Failed to open process token";return false;}base::win::ScopedHandle process_token(temp_handle);if (!::DuplicateTokenEx(process_token.Get(), desired_access, nullptr,SecurityImpersonation, TokenPrimary, &temp_handle)) {LOG(ERROR) << "Failed to duplicate the process token";return false;}token_out->Set(temp_handle);return true;
}void RunAccessTokenTest(DWORD integrity_level) {base::win::ScopedHandle privileged_token;CopyProcessToken(MAXIMUM_ALLOWED, &privileged_token);absl::optional<base::win::AccessToken> token =base::win::AccessToken::FromToken(std::move(privileged_token));token->SetIntegrityLevel(integrity_level);DWORD level = token->IntegrityLevel();if (level == integrity_level) {LOG(INFO) << "IntegrityLevel: " << level;} else {LOG(ERROR) << "failed IntegrityLevel: " << level;}base::LaunchOptions options;options.as_user = token->get();static const base::CommandLine::CharType* argvTmp[] = {FILE_PATH_LITERAL("C:/Windows/System32/Notepad.exe")};base::CommandLine command_line(1, argvTmp);LOG(INFO) << "Browser: " << command_line.GetCommandLineString();base::Process child_process = base::LaunchProcess(command_line, options);
}}  // namespaceint main(int argc, const char* argv[]) {// SYSTEM HIGH MEDIUM LOW UNTRUSTED//  Note: These levels map to SIDs under the hood.//  INTEGRITY_LEVEL_SYSTEM:      "S-1-16-16384" System Mandatory Level//  INTEGRITY_LEVEL_HIGH:        "S-1-16-12288" High Mandatory Level//  INTEGRITY_LEVEL_MEDIUM:      "S-1-16-8192"  Medium Mandatory Level//  INTEGRITY_LEVEL_MEDIUM_LOW:  "S-1-16-6144"//  INTEGRITY_LEVEL_LOW:         "S-1-16-4096"  Low Mandatory Level//  INTEGRITY_LEVEL_BELOW_LOW:   "S-1-16-2048"//  INTEGRITY_LEVEL_UNTRUSTED:   "S-1-16-0"     Untrusted Mandatory Level// DWORD integrity_level PSID 定义如下//  #define SECURITY_MANDATORY_LABEL_AUTHORITY          {0,0,0,0,0,16}//  #define SECURITY_MANDATORY_UNTRUSTED_RID            (0x00000000L)//  #define SECURITY_MANDATORY_LOW_RID                  (0x00001000L)//  #define SECURITY_MANDATORY_MEDIUM_RID               (0x00002000L)//  #define SECURITY_MANDATORY_MEDIUM_PLUS_RID (SECURITY_MANDATORY_MEDIUM_RID//  + 0x100) #define SECURITY_MANDATORY_HIGH_RID                 (0x00003000L)//  #define SECURITY_MANDATORY_SYSTEM_RID               (0x00004000L)//  #define SECURITY_MANDATORY_PROTECTED_PROCESS_RID    (0x00005000L)base::CommandLine::Init(argc, argv);RunAccessTokenTest(SECURITY_MANDATORY_SYSTEM_RID);  // system权限进程RunAccessTokenTest(SECURITY_MANDATORY_HIGH_RID);    // high权限进程RunAccessTokenTest(SECURITY_MANDATORY_MEDIUM_RID);  // medium权限进程RunAccessTokenTest(SECURITY_MANDATORY_LOW_RID);     // low权限进程return 0;
}

三、编译之后运行效果如图:

     可以看到已经按照预定完整性级别创建了进程。 

    system级别创建失败 是因为02-test.exe进程级别是high的,按照规则不可以模拟出大于原始进程完整性级别。强制完整性控制 - Win32 应用程序 |Microsoft 学习

 四、总结:

      核心也是利用DuplicateTokenEx复制进程token完整性级别,在设置到token中【SetTokenInformation】

SetIntegrityLevel函数定义如下:

template <typename T>
bool Set(const ScopedHandle& token,TOKEN_INFORMATION_CLASS info_class,T& value) {return !!::SetTokenInformation(token.get(), info_class, &value,sizeof(value));
}bool AccessToken::SetIntegrityLevel(DWORD integrity_level) {absl::optional<base::win::Sid> sid = Sid::FromIntegrityLevel(integrity_level);if (!sid) {::SetLastError(ERROR_INVALID_SID);return false;}TOKEN_MANDATORY_LABEL label = {};label.Label.Attributes = SE_GROUP_INTEGRITY;label.Label.Sid = sid->GetPSID();return Set(token_, TokenIntegrityLevel, label);
}

 base\win\access_token.cc

  base\win\sid.cc

也可以参考windows 进程降权和提权代码示例(2)-CSDN博客 

http://www.lryc.cn/news/479166.html

相关文章:

  • 【测试语言篇一】Python进阶篇:内置容器数据类型
  • 湘潭大学软件工程专业选修 SOA 期末考试复习(二)
  • 改进的正弦余弦算法复现
  • Day13杨辉三角
  • 【c知道】Hadoop工作原理。
  • React.lazy() 懒加载
  • 【自学笔记】神经网络(1)
  • c#————扩展方法
  • 前向-后向卡尔曼滤波器(Forward-Backward Kalman Filter)资料汇总
  • 云集电商:如何通过 OceanBase 实现降本 87.5%|OceanBase案例
  • 详解Rust标准库:BTreeMap
  • .NET WPF CommunityToolkit.Mvvm框架
  • 微信小程序使用阿里巴巴矢量图标库正确姿势
  • 【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
  • Java基础Day-Thirteen
  • LangChain实际应用
  • 【数据结构】哈希/散列表
  • flutter 项目初建碰到的控制台报错无法启动问题
  • Java字符串深度解析:String的实现、常量池与性能优化
  • leetcode 2043.简易银行系统
  • 基于SSM(Spring + Spring MVC + MyBatis)框架的文物管理系统
  • yakit中的规则详细解释
  • [c语言]strcmp函数的使用和模拟实现
  • 如何把子组件的v-model修改数据,进行接收然后定义数据格式,子传父的实现
  • linux dpkg 查看 安装 卸载 .deb
  • 【算法】递归+深搜:105.从前序与中序遍历序列构造二叉树
  • ESP32 gptimer通用定时器初始化报错:assert failed: timer_ll_set_clock_prescale
  • 基于Python的旅游景点推荐系统
  • 【开源社区】ELK 磁盘异常占用解决及优化实践
  • 达梦数据守护集群_动态增加实时备库