using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.Caching; using CsharpHttpHelper; using Newtonsoft.Json; using PCRobot.Entitys; using PCRobot.Pack; using PCRobot.Utils; namespace PCRobot.PCWechat.Enterprise { public partial class Wechat_Xiaoxie_QY : WechatEnterpriseBase { public Wechat_Xiaoxie_QY(WechatUser User) : base(User) { } #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?.Nickname}({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 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)) { LogHelper.GetSingleObj().Debug("", "发送@@@@@@@@@@@@@@@@: " + message + @" "); Common.SetCache(User.Wxid, to_wxid, message, 60 * 8); } } 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 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; } } /// /// 杀死注入 pc /// /// 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.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"; /// /// 等待登录结果 /// public static Dictionary WaitLoginResult = new Dictionary(); /// /// 远程登录微信 /// public static List RemoteLoginCodes = new List(); /// /// pc微信登录二维码 /// /// 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(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); } } /// /// 卸载安装 /// public static void UnInstall() { try { //DestroyWxWork(); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message + ".."); } } #endregion } }