old_flsystem/PCRobot/PCWechat/Wechat_Xiaoxie_QY - 复制.cs

352 lines
14 KiB
C#
Raw Permalink Normal View History

2022-09-20 03:10:29 +00:00
using CsharpHttpHelper;
using Newtonsoft.Json;
using NM_WeChat;
using PCRobot.Entitys;
using PCRobot.Pack;
using PCRobot.Properties;
using PCRobot.Utils;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;
using System.Windows.Forms;
namespace PCRobot.PCWechat
{
/*
https://www.showdoc.cc/WxWorkNewHook?page_id=3846144660177490
/////密码784615627.
*/
public partial class Wechat_Xiaoxie_QY : BaseApi
{
#region
public delegate void AcceptCallbackFunc(uint dwClientId);
public delegate void RecvCallbackFunc(uint dwClientId, IntPtr intPtr, uint dwSize);
public delegate void CloseCallbackFunc(uint dwClientId);
private static AcceptCallbackFunc m_AcceptCallbackFunc;
private static RecvCallbackFunc m_RecvCallbackFunc;
private static CloseCallbackFunc m_CloseCallbackFunc;
// 引入DLL导出函数
//[DllImport("Bin\\WeChatServer_QY.dll")]
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern bool InitWxWorkSocket(AcceptCallbackFunc acceptCallback,
RecvCallbackFunc recvCallback, CloseCallbackFunc closeCallback);
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern uint InjectWxWork(byte[] strDllPath, byte[] strWxWorkExePath);
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern uint InjectWxWorkMultiOpen(byte[] strDllPath, byte[] strWxWorkExePath);
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern bool SendWxWorkData(uint dwClienId, byte[] strJsonData);
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern bool DestroyWxWork();
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern uint InjectWxWorkPid(int dwPid, byte[] strDllPath);
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern bool UseUtf8();
[DllImport("Bin\\WxWorkLoader.dll")]
public static extern bool GetUserWxWorkVersion(StringBuilder v);
[DllImport("Bin\\WxWorkLoader.dll", EntryPoint = "SendWxWorkData")]
public static extern bool _SendWxWorkData(uint dwClienId, byte[] strJsonData);
public bool SendWxWorkData(uint dwClienId, string strJsonData)
{
var ss = User.DwClientId;
if (WechatClient.IsDebug)
LogHelper.GetSingleObj().Debug("_调试发送_", $"{dwClienId} -> {strJsonData} - {User != null} - 微信id:{User?.Wxid}");
if (User != null && !string.IsNullOrEmpty(User.Wxid))
{
var json = HttpExtend.JsonToDictionary(strJsonData);
var type = json["type"].ToString();
MsgType msgType;
if (!Enum.TryParse(type, out msgType))
{
if (WechatClient.IsDebug)
LogHelper.GetSingleObj().Debug("调试发送@转换失败", $"{dwClienId} -> type{type}");
return false;
}
switch (msgType)
{
//截取发送消息 - 为是否是辅助输入提供判断
case MsgType.MT_SEND_TEXT_MSG:// 发送文本消息
case MsgType.MT_SEND_IMAGE_MSG:// 发送图片消息
case MsgType.MT_SEND_FILE_MSG:// 发送文件消息
case MsgType.MT_SEND_FOLDER_MSG:// 发送目录消息
case MsgType.MT_SEND_LINK_MSG:// 发送链接消息
//case MsgType.MT_SEND_PERSON_CARD_MSG:// 发送个人名片
case MsgType.MT_SEND_VIDEO_MSG:// 发送视频
{
json = json["data"] as Dictionary<string, object>;
string conversation_id = json["conversation_id"].ToString();
var to_wxid = conversation_id;
if (conversation_id.StartsWith("S:"))//私人消息处理一下,群消息不用管
{
to_wxid = conversation_id.Replace($"S:{User.Wxid}_", "");
}
string message = string.Empty;
if (json.ContainsKey("content")) message = json["content"].ToString();
else if (json.ContainsKey("card_wxid")) message = json["card_wxid"].ToString();
else if (json.ContainsKey("file")) message = json["file"].ToString();
//else if (json.ContainsKey("user_id")) message = json["user_id"].ToString();
if (!string.IsNullOrEmpty(message))
{
Common.SetCache(User.Wxid, to_wxid, message);
}
}
break;
}
}
return _SendWxWorkData(dwClienId, Encoding.UTF8.GetBytes(strJsonData));
}
static string DllPath = HttpExtend.MapFile($"WxWorkHelper_{Version}.dll", "Bin");
private const string Version = "4.0.2.6026";
private const string PcMd5 = "E77637926604364C4AD65625032B2E8A";
public override string GetVersion()
{
return Version;
}
public Wechat_Xiaoxie_QY(WechatUser User) : base(User) { }
public static void Install()
{
if (IsQYInstall) return;
try
{
m_AcceptCallbackFunc = WxAcceptCallback;
m_RecvCallbackFunc = WxRecvCallback;
m_CloseCallbackFunc = WxCloseCallback;
UseUtf8();
// 初始化Callback
InitWxWorkSocket(m_AcceptCallbackFunc, m_RecvCallbackFunc, m_CloseCallbackFunc);
StringBuilder sb = new StringBuilder();
var path1 = HttpExtend.MapPath("WXWork");
var path2 = HttpExtend.MapPath("WXWork\\" + Version);
InitPc();
}
catch (Exception)
{
}
finally
{
IsQYInstall = true;
}
}
/// <summary>
/// 杀死注入 pc
/// </summary>
/// <param name="isInject"></param>
private static void KillPc(bool isInject = false)
{
Process[] MyProcesses = Process.GetProcesses();
var path1 = HttpExtend.MapPath("WXWork");
var path2 = HttpExtend.MapPath("WXWork\\" + Version);
foreach (Process MyProcess in MyProcesses)
{
try
{
var name = MyProcess.ProcessName.ToUpper();
if (name == "WXWORK" && MyProcess.MainModule.FileName.StartsWith(path1))
{
try
{
//判断微信版本
if (MyProcess.MainModule.FileName.StartsWith(path2))
{
var id = MyProcess.Id;
if (isInject)
{
var result = Task<bool>.Factory.StartNew(delegate
{
try
{
var WechatWinFile = CsharpHttpHelper.HttpExtend.MapFile("WXWork.exe", $"WXWork\\{Version}");
var md5 = Common.GetMD5Hash(WechatWinFile).ToUpper();
if (!File.Exists(WechatWinFile) || md5 != PcMd5.ToUpper())
{
return false;
}
else
{
LogHelper.GetSingleObj().Info("系统", $"开始企业注入进程:{id}...");
var uin = InjectWxWorkPid(id, Encoding.UTF8.GetBytes(DllPath));
LogHelper.GetSingleObj().Info("系统", $"注入企业成功:{id},句柄ID:{uin}");
return true;
}
}
catch (Exception)
{ }
return false;
}).Wait(20000);
if (!result)
{
try
{
LogHelper.GetSingleObj().Info("系统cc", "杀死企业进程:" + MyProcess.Id);
MyProcess.Kill();
}
catch (Exception)
{ }
}
}
else
{
try
{
LogHelper.GetSingleObj().Info("主动dd", "杀死进程:" + MyProcess.Id);
MyProcess.Kill();
}
catch (Exception)
{ }
}
}
}
catch (Exception)
{ }
}
}
catch (Exception ex)
{
LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message);
}
}
}
private static void InitPc()
{
KillPc();
}
private static string RemoteCache = "get_login_code_cache_workwechat";
/// <summary>
/// 等待登录结果
/// </summary>
public static Dictionary<string, string> WaitLoginResult = new Dictionary<string, string>();
/// <summary>
/// 远程登录微信
/// </summary>
public static List<RemoteLoginCode> RemoteLoginCodes = new List<RemoteLoginCode>();
/// <summary>
/// pc微信登录二维码
/// </summary>
/// <param name="ServerMsgID"></param>
public static void LoginQrcode(string ServerMsgID)
{
try
{
RemoteLoginCode result = null;
try
{
string downloadsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var wxConfig = HttpExtend.MapFile(@"Config.cfg", downloadsPath + @"\WXWork\Global");
if (File.Exists(wxConfig))
{
try
{
File.Delete(wxConfig);
}
catch (Exception)
{ }
StartWechat(true);
}
}
catch (Exception ex)
{
result = new RemoteLoginCode() { pid = -1, rType = RobotType., b64Md5 = ex.Message };
}
if (result == null)
{
result = Task.Factory.StartNew<RemoteLoginCode>(delegate ()
{
var awaitTime = DateTime.Now.AddSeconds(10);
do
{
Thread.Sleep(200);
Cache cache = HttpRuntime.Cache;
object item = cache[RemoteCache];
if (item != null)
{
cache.Remove(RemoteCache);
var r = item as RemoteLoginCode;
return r;
}
} while (awaitTime >= DateTime.Now);
return null;
}).Result;
}
var socketClient = EasySoc.GetSocket();
var msg = new CommonResult();
msg.RobotUsername = string.Empty;
msg.RobotUsernick = string.Empty;
msg.RobotType = RobotType.;
msg.MsgId = ServerMsgID;
msg.Cmd = PCRobotCMD.rcvLoginCode_workWeChat;
msg.Data = result == null ? string.Empty : JsonConvert.SerializeObject(result);
socketClient.Send(msg);
}
catch (Exception ex)
{
LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message);
}
}
/// <summary>
/// 卸载安装
/// </summary>
public static void UnInstall()
{
try
{
//DestroyWxWork();
}
catch (Exception ex)
{
LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message + "..");
}
}
#endregion
}
}