首页 > 编程语言 >C++实现查询本机信息并且上报

C++实现查询本机信息并且上报

时间:2023-05-19 11:15:09浏览次数:50  
标签:机信息 NULL string share C++ 查询 str Release include

业务需求

共享文件夹、盘会导致系统安全性下降,故IT部门需要搜集公司中每台电脑的共享情况,并且进行上报

关键字

WMI查询、Get请求、C++网络库mongoose

前置需要

1、简单C++语法知识
2、mongoose库的导入
3、C++项目的启动

代码

复制并不能直接使用,需导入mongoose库
完整github项目代码:https://github.com/Canwaiting/check_netshare
修改其中的变量url,app,white_list[],这个项目就是你的了

/*********************************************************
*
* 项目背景:共享文件夹、盘会导致系统安全性下降,故IT部门
* 需要搜集公司中每台电脑的共享情况,并且进行上报
*
*********************************************************/

#include <fstream>
#include <iostream>
#include "mongoose.h"
#define _WIN32_DCOM
#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
#include <string>
#include <time.h>
#include <sstream>
#include <iomanip> 
#pragma comment(lib, "wbemuuid.lib")
#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup") //静默运行
using namespace std;

string wstring2string(wstring wstr, UINT nCode);
wstring string2wstring(string str);
LPCWSTR string2LPCWSTR(string str);
string WMIQuery(string table, string key);
string GetData();
string UploadData(string data);
string GetDateTime();
static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data);
bool IsDefaultShare(string share_name, string share_path);
inline char* ConvertBSTRToString(BSTR pSrc);

//退出标志 用于控制关闭事件循环
static int exit_flag = 0; 
//APP版本信息 
static const string app = "XXX";
//白名单
static const string white_lists[] =
{
"IPC$",
"ADMIN$",
//"print$" 打印机,暂时不需要
};
//上传数据的地址前缀
static string url = "XXX";

int main(int argc, char** argv)
{
    string data; 
    data = GetData(); 

    //拼接数据,并转成UTF-8
    url += data; 
    wstring wstr = string2wstring(url);
    url = wstring2string(wstr, CP_UTF8); 

    UploadData(url); 
    //getchar();

    return 0; 
}

string GetData() 
{
    stringstream ss;
    string date_time = "";
    string host_name = "";
    string machine_code= "";
    string share_lists = "";
    string data = "";

    date_time = GetDateTime();
    host_name = WMIQuery("Win32_ComputerSystem", "Name"); 
    machine_code= WMIQuery("Win32_BIOS","SerialNumber"); 
    share_lists = WMIQuery("win32_share",""); //这个特殊处理


    ss << "&APP=" << app
        << "&DateTime=" << date_time
        << "&HostName=" << host_name
        << "&MachineCode=" << machine_code
        << "&ShareLists=" << share_lists;
    data = ss.str();


    /*
cout << "**********" << endl;
cout << "APP: " << app << endl;
cout << "DateTime: " << date_time << endl;
cout << "HostName: " << host_name << endl;
cout << "MachineCode: " << machine_code<< endl;
cout << "ShareLists: " << share_lists<< endl;
cout << "**********" << endl;
cout << "data: " << data << endl;;
*/

    return data;
}

string UploadData(string data)
{
    char* s_url = new char[strlen(url.c_str()) + 1];
    strcpy(s_url,url.c_str());

    struct mg_mgr mgr;
    mg_mgr_init(&mgr);                        // Init manager
    mg_http_connect(&mgr, s_url, fn, NULL);   // Create client connection 
    while (exit_flag == 0) {
        mg_mgr_poll(&mgr, 1000); 
    }
    mg_mgr_free(&mgr);                        // Cleanup

    string response = ""; //TODO 暂时没有什么要求
    return response; 
}

string WMIQuery(string table, string key) {
HRESULT hres;
string result = "";
string empty_result = "";
stringstream ss;

// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------

hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return empty_result;                  // Program has failed.
}

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------

hres = CoInitializeSecurity(
NULL,
-1,                          // COM authentication
NULL,                        // Authentication services
NULL,                        // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
NULL,                        // Authentication info
EOAC_NONE,                   // Additional capabilities 
NULL                         // Reserved
);


if (FAILED(hres))
{

CoUninitialize();
return empty_result;                  // Program has failed.
}

// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------

IWbemLocator* pLoc = NULL;

hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*)&pLoc);

if (FAILED(hres))
{
CoUninitialize();
return empty_result;                  // Program has failed.
}

// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method

IWbemServices* pSvc = NULL;

// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL,                    // User name. NULL = current user
NULL,                    // User password. NULL = current
0,                       // Locale. NULL indicates current
NULL,                    // Security flags.
0,                       // Authority (for example, Kerberos)
0,                       // Context object 
&pSvc                    // pointer to IWbemServices proxy
);

if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return empty_result;                  // Program has failed.
}

//cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------

hres = CoSetProxyBlanket(
pSvc,                        // Indicates the proxy to set
RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
NULL,                        // Server principal name 
RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL,                        // client identity
EOAC_NONE                    // proxy capabilities 
);

if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return empty_result;                  // Program has failed.
}

// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----

// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
string sql = "SELECT * FROM " + table;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t((wchar_t*) (_bstr_t(sql.c_str()))),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);

if (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return empty_result;                  // Program has failed.
}

// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------

IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;

while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);

if (0 == uReturn)
{
break;
}

VARIANT vtProp;

VariantInit(&vtProp);

// Get the value of the Name property 
if (key == "") {
hr =  pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
string share_name = _com_util::ConvertBSTRToString(vtProp.bstrVal);
hr =  pclsObj->Get(L"Path", 0, &vtProp, 0, 0); 
string share_path = _com_util::ConvertBSTRToString(vtProp.bstrVal);
if (!IsDefaultShare(share_name, share_path))
{
ss << "\"" << share_name << "\"";
ss << ":";
ss << "\"" << share_path << "\"";
ss << ","; 
}
}
else {
hr = pclsObj->Get(string2LPCWSTR(key), 0, &vtProp, 0, 0);
ss << ConvertBSTRToString(vtProp.bstrVal); 
//ss << _com_util::ConvertBSTRToString(vtProp.bstrVal); 
}
VariantClear(&vtProp);

pclsObj->Release();
}

// Cleanup
// ========

pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();

result = ss.str();
if (key == "") {
result = result.substr(0, result.length() - 1);
}
return result; 
}

LPCWSTR string2LPCWSTR(string str)
{
size_t size = str.length();
int wLen = ::MultiByteToWideChar(CP_UTF8,
0,
str.c_str(),
-1,
NULL,
0);
wchar_t* buffer = new wchar_t[wLen + 1];
memset(buffer, 0, (wLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, str.c_str(), size, (LPWSTR)buffer, wLen);
return buffer;
}

string GetDateTime()
{

string date_time = "";
time_t nowtime;
time(&nowtime); //获取1970年1月1日0点0分0秒到现在经过的秒数
tm tm_t;
localtime_s(&tm_t, &nowtime); //将秒数转换为本地时间,年从1900算起,需要+1900,月为0-11,所以要+1
stringstream ss;
ss << tm_t.tm_year + 1900
<< setw(2) << setfill('0') << tm_t.tm_mon + 1
<< setw(2) << setfill('0') << tm_t.tm_mday
<< " "
<< setw(2) << setfill('0') << tm_t.tm_hour 
<< ":" 
<< setw(2) << setfill('0') << tm_t.tm_min 
<< ":" 
<< setw(2) << setfill('0') << tm_t.tm_sec;
date_time = ss.str();
return date_time; 
}

//不上报默认的、在白名单内的共享盘
bool IsDefaultShare(string share_name, string share_path)
{
//1、系统默认共享盘
if (!share_name.empty() && !share_path.empty() &&  // 非空
share_name[share_name.length() - 1] == '$' &&  // 共享名最后一个字符一定是$
share_path.length() == 3 &&  // 资源必须为 X:\ 的格式
share_name.substr(0,share_name.length() - 1) == share_path.substr(0,share_path.length() - 2))  // 盘符名一定要对得上 
{
return true;
}
//2、白名单
for (string white_list_item : white_lists)
{
if (share_name == white_list_item)
{
return true;
} 
} 
return false; 
}

static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data)
{
char* s_url = new char[strlen(url.c_str()) + 1];
strcpy(s_url,url.c_str());
if (ev == MG_EV_CONNECT) { 
struct mg_str host = mg_url_host(s_url);
// Send request
mg_printf(c,
"GET %s HTTP/1.0\r\n"
"Host: %.*s\r\n"
"\r\n",
mg_url_uri(s_url), (int)host.len, host.ptr);
} if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message* hm = (struct mg_http_message*)ev_data;
exit_flag = 1;
}
}

//重新实现_com_util的ConvertBSTRToString
inline char* ConvertBSTRToString(BSTR pSrc)
{
//if (!pSrc) return NULL;  原本返回空会导致空指针异常
if (!pSrc)
{
char* szOut = new char[1];
szOut[0] = '\0';
return szOut;
}

DWORD cb, cwch = ::SysStringLen(pSrc);

char* szOut = NULL;

if (cb = ::WideCharToMultiByte(CP_ACP, 0,
pSrc, cwch + 1, NULL, 0, 0, 0))
{
szOut = new char[cb];
if (szOut)
{
szOut[cb - 1] = '\0';

if (!::WideCharToMultiByte(CP_ACP, 0,
pSrc, cwch + 1, szOut, cb, 0, 0))
{
delete[]szOut;//clean up if failed;
szOut = NULL;
}
}
} 
return szOut;
};

wstring string2wstring(string str)
{
wstring result;
//获取缓冲区大小,并申请空间,缓冲区大小按字符计算  
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
TCHAR* buffer = new TCHAR[len + 1];
//多字节编码转换成宽字节编码  
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0';             //添加字符串结尾  
//删除缓冲区并返回值  
result.append(buffer);
delete[] buffer;
return result;
}

string wstring2string(wstring wstr, UINT nCode)
{
string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的  
int len = WideCharToMultiByte(nCode, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码  
WideCharToMultiByte(nCode, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
//删除缓冲区并返回值  
result.append(buffer);
delete[] buffer;
return result;
}

牢骚

本来是用C#写的,但是打出来的包几十M,不符合要求;还是得用回C++,各种轮子都要自己造,还得转字符;差点就完不成了 : )

部分参考

https://www.jb51.net/softjc/2124_2.html
https://mongoose.ws/documentation/tutorials/http-client/
https://blog.csdn.net/hanxiaoyong_/article/details/123494927
https://github.com/tashaxing/cpphttpdemo

标签:机信息,NULL,string,share,C++,查询,str,Release,include
From: https://www.cnblogs.com/canwaiting/p/17414313.html

相关文章

  • SqlServer查询锁表及解锁
    --查询锁表进程selectrequest_session_idspid,OBJECT_NAME(resource_associated_entity_id)tableNamefromsys.dm_tran_lockswhereresource_type='OBJECT'--解锁进程declare@spidintSet@spid=162--锁表进程declare@sqlvarchar(1000)set@sql='kill......
  • 详解c++STL—容器list
    1、list基本概念1.1、概念描述链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的链表的组成:链表由一系列结点组成功能:将数据进行链式存储1.2、结点的组成一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域STL中的链表是一......
  • es笔记五之term-level的查询操作
    本文首发于公众号:Hunter后端原文链接:es笔记五之term-level的查询操作官方文档上写的是term-levelqueries,表义为基于准确值的对文档的查询,可以理解为对keyword类型或者text类型分词为keyword的字段进行term形式的精确查找。以下是本篇笔记目录:是否存在值前缀搜索......
  • c++ stoll
    stoll,字符串转换为longlong(114条消息)C/C++编程笔记:stol和stoll函数,函数调用中的字符串转换_c++stoll_一起学编程的博客-CSDN博客......
  • Oracle从入门到精通-合并查询、添加修改删除数据
    6Oracle表的管理6.5oracle表的管理-表查询(重点)6.5.5Oracle表复杂查询--合并查询·合并查询有时在实际应用中,为了合并多个select语句的结果,可以使用集合操作符号union,unionall,intersect,minus1)union该操作符用于取得两个结果集的并集,当使用该操作符时,=selectename,sal,j......
  • C/C++银行自助存取款机模拟程序[2023-05-18]
    C/C++银行自助存取款机模拟程序[2023-05-18]设计一个银行自助存取款机模拟程序,银行自助存取款机的用户包括银行管理员和客户,程序可实现这两类用户的基本操作需求。银行管理员:凭身份密码登录后可查看银行自助存取款机的余额、查询给定时间段内所有的交易信息(卡号、交易类型、交......
  • 从C到C++:学习C++的高级语法、STL和面向对象编程
    好的,那我为您写一篇有关C++编程的文章,详情如下。标题:从C到C++:学习C++的高级语法、STL和面向对象编程开头:C++语言是对C语言的扩展,是一种面向对象的程序设计语言。它具有丰富的数据类型、函数模板、类模板、标准模板库等高级特性。掌握高级语法、STL和面向对象编程,可以使程序员更加高......
  • C++趣味编程
    分糖果1#include<iostream>2usingnamespacestd;3intmain()4{5inti,count=0;6inta[10]={10,2,8,22,16,4,10,6,14,20};7intb[10]={10,2,8,22,16,4,10,6,14,20};8do{9a[0]=b[0]/2+b[9]/2;10for(intj=1......
  • c++打卡练习(33)
    求一个真分数的埃及分数表示埃及分数是指只使用1作为分子的分数,例如8/11=1/2+1/5+1/55+1/110;流程图:伪代码:源代码:#include<iostream>usingnamespacestd;intmain(){ inta,b,c,i,j; cout<<"请输入一个真分数"<<endl; cin>>a; getchar(); cin>>b; if(a>b){ cout<<......
  • C++
    文件定义一个Dog类,包括体重和年龄两个数据成员及其成员函数,声明一个实例dog1,体重5,年龄10,使用I/O流把dog1的状态写入磁盘文件。再声明一个实例dog2,通过读取文件dog1的状态赋给dog2。#include<iostream>#include<fstream>usingnamespacestd;classDog{public:      ......