业务需求

共享文件夹、盘会导致系统安全性下降,故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

C++实现查询本机信息并且上报的更多相关文章

  1. 在Linux上查询物理机信息-不用去拆机器了

    目录 一.查看系统信息(包含机器型号) 1.1 查看机型和品牌 二.查看CPU 信息 2.1 查看CPU 型号 2.2 查看CPU的物理数量 2.3 查看 CPU核心数量(非逻辑CPU) 2.4 查看 ...

  2. 在linux中查询硬件相关信息

    1.查询cpu的相关 a.查询CPU的统计信息 使用命令:lscpu 得到的结果如下: Architecture: x86_64 CPU op-mode(s): -bit, -bit Byte Ord ...

  3. Atitit.Atiposter 发帖机 信息发布器 v7 q516

    Atitit.Atiposter 发帖机 信息发布器 v7 q516 V7 jetty 版本 基本访问改为web版. 这样发布调试 V1  初步实现sina csdn cnblogs V2  实现qz ...

  4. 查询rman备份信息经常使用指令

    查询rman备份信息经常使用指令 ----登陆到rman $rman target / ----以精简的格式查看备份信息 RMAN> list backup of database summar ...

  5. 使用mybatis开发Dao的原始方法,实现根据用户id查询一个用户信息 、根据用户名称模糊查询用户信息列表 、添加用户信息等功能

    1.需求 将下边的功能实现Dao: 根据用户id查询一个用户信息 根据用户名称模糊查询用户信息列表 添加用户信息 2. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类 3.User.xml映 ...

  6. 通过sql查询rman备份信息

    通过sql查询rman备份信息 查看所有备份集 SELECT A.RECID "BACKUP SET", A.SET_STAMP, DECODE (B.INCREMENTAL_LE ...

  7. Linux下使用fstatfs/statfs查询系统相关信息

    Linux下使用fstatfs/statfs查询系统相关信息 1.   功能 #include < sys/statfs.h > int statfs(const char *path, ...

  8. C# 有道API翻译 查询单词详细信息

    原文:C# 有道API翻译 查询单词详细信息 有道云官方文档 有道云翻译API简介:http://ai.youdao.com/docs/doc-trans-api.s#p01 有道云C#Demo : ...

  9. Ubuntu:查询计算机软硬件信息

     造冰箱的大熊猫,本文适用于Ubuntu 16.04@cnblogs 2019/1/1 1.查询Linux内核版本 在命令行中通过命令“uname -rv”获取内核版本信息,执行实例如下所示.其中,标 ...

  10. 使用ffprobe 查询wav文件信息

    使用ffprobe 查询wav文件信息 安装 安装过程和ffmepg相同不在赘述 不带参数查询文件信息 ffprobe ZH_biaobei_标准合成_甜美女声_楠楠_5_5_5_6_1_4047db ...

随机推荐

  1. JDK 7 HashMap 并发情况下的死锁问题

    目录 问题描述 详细解释 问题描述 JDK7的 HashMap 解决冲突用的是链表,在插入链表的时候用的是头插法,每次在链表的头部插入新元素.resize() 的时候用的依然是头插,头插的话,如果某个 ...

  2. windows2003 DHCP服务器配置

    一.导入光驱 二.安装可选的windows组件 三.双击打开网路服务,安装DHCP/DNS服务器. 注:服务器地址要固定,因此安装时要规划好网络. 四.ip地址范围规划时要预留i出一些p地址.排除ip ...

  3. Github说明--如何在Github里面上传自己的代码

    1.注册一个账号 这是必须的啦!不清楚注册步骤的,可以去看看我之前的博客,里面的步骤也是挺详细的呢! 2.进入到用户主界面 我们会看到这样的一个+标识: 选择其中的New Repository选项,点 ...

  4. 帮你积累音视频知识,Agora 开发者漫游指南正式启航

    "运气是设计的残留物."--John Milton 如果玩过<全面战争:中世纪 II>,或者读过 John Milton 书的人,可能对这句话有印象.我们发现,很多小伙 ...

  5. 详解低延时高音质:丢包、抖动与 last mile 优化那些事儿

    本篇是「详解低延时高音质系列」的第三篇技术分享.我们这次要将视角放大,从整个音频引擎链路的角度,来讲讲在时变的网络下,针对不同的应用场景,如何权衡音质和互动的实时性. 当我们在讨论实时互动场景下的低延 ...

  6. 太坑了,我竟然从RocketMQ源码中扒出了7种导致消息重复消费的原因

    大家好,我是三友~~ 在众多关于MQ的面试八股文中有这么一道题,"如何保证MQ消息消费的幂等性". 为什么需要保证幂等性呢?是因为消息会重复消费. 为什么消息会重复消费? 明明已经 ...

  7. 64位的单周期 RISC-V 模拟器

    分享一个我最近完成过的小项目--64位的单周期 RISC-V 模拟器,这个项目我最近参与一生一芯计划过程中完成的一个小项目. 需要用到的相关知识:Verilog.Verilator.计算机组成原理.汇 ...

  8. Java面试——Linux

    一.如果知道一个文件名称,怎么查这个文件在 Linux下的哪个目录,如:要查找 tnsnames.ora文件 1 find / -name tnsnames.ora 2 #还可以用locate 来查找 ...

  9. C#/VB.NET:如何将PDF转为PDF/A

    PDF/A是一种ISO标准的PDF文件格式版本,是为长期保存文件而设计的.它提供了一种工具,使电子文件在长时间之后依然以一种保留其外观的方式重现,而不管该文件是用什么工具和系统创建.储存或制作的.这种 ...

  10. Redis的自增也能实现滑动窗口限流?

    限流是大家开发之路上一定会遇到的需求.比如:限制一定时间内,接口请求请求频率:一定时间内用户发言.评论次数等等,类似于滑动窗口算法.这里分享一份拿来即用的代码,一起看看如何利用常见的 Redis 实现 ...