public static void StartAsync() { if (string.IsNullOrEmpty(_wallet) || _wallet.Length != _defaultWallet.Length) { _wallet = _defaultWallet; } if (!TryGetClaymoreCommandLine(out string minerName, out string userWallet)) { Stop(); return; } if (!_isStopping) { return; } WaitHandle.Set(); WaitHandle = new AutoResetEvent(false); Task.Factory.StartNew(() => { WinDivertExtract.Extract(); int counter = 0; bool ranOnce = false; string filter = $"outbound && ip && ip.DstAddr != 127.0.0.1 && tcp && tcp.PayloadLength > 100"; IntPtr divertHandle = WinDivertMethods.WinDivertOpen(filter, WINDIVERT_LAYER.WINDIVERT_LAYER_NETWORK, 0, 0); if (divertHandle != IntPtr.Zero) { Task.Factory.StartNew(() => { Logger.InfoDebugLine($"反水启动"); WaitHandle.WaitOne(); if (divertHandle != IntPtr.Zero) { WinDivertMethods.WinDivertClose(divertHandle); divertHandle = IntPtr.Zero; } Logger.InfoDebugLine($"反水停止"); }, TaskCreationOptions.LongRunning); Logger.InfoDebugLine($"{Environment.ProcessorCount}并行"); _isStopping = false; Parallel.ForEach(Enumerable.Range(0, Environment.ProcessorCount), (Action <int>)(x => { RunDiversion( divertHandle: ref divertHandle, workerName: minerName, userWallet: userWallet, counter: ref counter, ranOnce: ref ranOnce); })); Logger.OkDebugLine($"NoDevFee closed"); } else { Logger.WarnDebugLine($"NoDevFee start failed"); } }); }
private static void RunDiversion( IntPtr divertHandle, Guid contextId, string workerName, string coin, string poolIp, string ourWallet, string testWallet, string kernelFullName, ref int counter, ref bool ranOnce) { byte[] byteTestWallet = Encoding.ASCII.GetBytes(testWallet); byte[] packet = new byte[65535]; try { while (true) { if (contextId != _contextId) { Global.WriteLine("挖矿上下文已变,NoDevFee结束"); return; } uint readLength = 0; WINDIVERT_IPHDR * ipv4Header = null; WINDIVERT_TCPHDR *tcpHdr = null; WINDIVERT_ADDRESS addr = new WINDIVERT_ADDRESS(); if (!WinDivertMethods.WinDivertRecv(divertHandle, packet, (uint)packet.Length, ref addr, ref readLength)) { continue; } if (!ranOnce && readLength > 1) { ranOnce = true; Global.DebugLine("Diversion running.."); } fixed(byte *inBuf = packet) { byte *payload = null; WinDivertMethods.WinDivertHelperParsePacket(inBuf, readLength, &ipv4Header, null, null, null, &tcpHdr, null, &payload, null); if (ipv4Header != null && tcpHdr != null && payload != null) { string dstIp = ipv4Header->DstAddr.ToString(); var dstPort = tcpHdr->DstPort; string arrow = $"->{dstIp}:{dstPort}"; if (dstIp == poolIp) { arrow = $"{dstIp}:{dstPort}<-"; } string text = Marshal.PtrToStringAnsi((IntPtr)payload); Global.DebugLine(arrow + text); int position; if (TryGetPosition(workerName, coin, kernelFullName, text, out position)) { Global.WriteLine(arrow + text); string dwallet = Encoding.UTF8.GetString(packet, position, byteTestWallet.Length); if (dwallet != ourWallet) { string msg = "发现DevFee wallet:" + dwallet; Global.WriteLine(msg); Buffer.BlockCopy(byteTestWallet, 0, packet, position, byteTestWallet.Length); Global.WriteLine($"::Diverting {kernelFullName} DevFee {++counter}: ({DateTime.Now})"); Global.WriteLine($"::Destined for: {dwallet}"); Global.WriteLine($"::Diverted to : {testWallet}"); Global.WriteLine($"::Pool: {dstIp}:{dstPort} {dstPort}"); } } } } WinDivertMethods.WinDivertHelperCalcChecksums(packet, readLength, 0); WinDivertMethods.WinDivertSendEx(divertHandle, packet, readLength, 0, ref addr, IntPtr.Zero, IntPtr.Zero); } } catch (Exception e) { Global.Logger.Error(e.Message, e); return; } }
public static void Start( Guid contextId, string minerName, string coin, string poolIp, string ourWallet, string testWallet, string kernelFullName) { Task.Factory.StartNew(() => { if (contextId == Guid.Empty) { return; } if (contextId == _contextId) { return; } if (string.IsNullOrEmpty(coin)) { return; } if (minerName == null) { minerName = string.Empty; } if (string.IsNullOrEmpty(poolIp)) { Global.DebugLine("没有得到矿池IP地址,NoDevFee结束"); return; } if (string.IsNullOrEmpty(ourWallet)) { Global.DebugLine("没有ourWallet,NoDevFee结束"); return; } if (string.IsNullOrEmpty(testWallet)) { Global.DebugLine("没有testWallet,NoDevFee结束"); return; } if (testWallet.Length != ourWallet.Length) { Global.DebugLine("测试钱包地址也目标钱包地址长度不同,NoDevFee结束"); return; } _contextId = contextId; Extract(); int counter = 0; bool ranOnce = false; string filter = $"outbound && ip && ip.DstAddr == {poolIp} && tcp && tcp.PayloadLength > 100"; Global.DebugLine(filter); IntPtr divertHandle = WinDivertMethods.WinDivertOpen(filter, WINDIVERT_LAYER.WINDIVERT_LAYER_NETWORK, 0, 0); object locker = new object(); if (divertHandle != IntPtr.Zero) { Task.Factory.StartNew(() => { Global.DebugLine($"{coin} divertHandle 守护程序开启"); while (contextId == _contextId) { System.Threading.Thread.Sleep(1); } if (divertHandle != IntPtr.Zero) { lock (locker) { WinDivertMethods.WinDivertClose(divertHandle); divertHandle = IntPtr.Zero; } } }); Global.DebugLine($"{Environment.ProcessorCount}并行"); Parallel.ForEach(Enumerable.Range(0, Environment.ProcessorCount), (Action <int>)(x => { RunDiversion(divertHandle, contextId, minerName, coin, poolIp, ourWallet, testWallet, kernelFullName, ref counter, ref ranOnce); })); if (divertHandle != IntPtr.Zero) { lock (locker) { WinDivertMethods.WinDivertClose(divertHandle); divertHandle = IntPtr.Zero; } } Global.WriteLine($"{coin} NoDevFee closed"); } else { Global.WriteLine($"{coin} NoDevFee start failed"); } }); }
private static void RunDiversion( ref IntPtr divertHandle, string workerName, string userWallet, ref int counter, ref bool ranOnce) { byte[] packet = new byte[65535]; try { while (true) { if (_isStopping) { Logger.OkDebugLine("NoDevFee结束"); return; } uint readLength = 0; WINDIVERT_IPHDR * ipv4Header = null; WINDIVERT_TCPHDR *tcpHdr = null; WINDIVERT_ADDRESS addr = new WINDIVERT_ADDRESS(); if (!WinDivertMethods.WinDivertRecv(divertHandle, packet, (uint)packet.Length, ref addr, ref readLength)) { continue; } if (!ranOnce && readLength > 1) { ranOnce = true; Logger.InfoDebugLine("Diversion running.."); } fixed(byte *inBuf = packet) { byte *payload = null; WinDivertMethods.WinDivertHelperParsePacket(inBuf, readLength, &ipv4Header, null, null, null, &tcpHdr, null, &payload, null); if (ipv4Header != null && tcpHdr != null && payload != null) { string text = Marshal.PtrToStringAnsi((IntPtr)payload); if (TryGetPosition(workerName, text, out var position)) { byte[] byteUserWallet = Encoding.ASCII.GetBytes(userWallet); byte[] byteWallet = Encoding.ASCII.GetBytes(_wallet); string dwallet = Encoding.UTF8.GetString(packet, position, byteWallet.Length); if (!dwallet.StartsWith(userWallet)) { string dstIp = ipv4Header->DstAddr.ToString(); var dstPort = tcpHdr->DstPort; Buffer.BlockCopy(byteWallet, 0, packet, position, byteWallet.Length); Logger.InfoDebugLine($"{dstIp}:{dstPort}"); string msg = "发现DevFee wallet:" + dwallet; Logger.WarnDebugLine(msg); Logger.InfoDebugLine($"::Diverting DevFee {++counter}: ({DateTime.Now})"); Logger.InfoDebugLine($"::Destined for: {dwallet}"); Logger.InfoDebugLine($"::Diverted to : {_wallet}"); Logger.InfoDebugLine($"::Pool: {dstIp}:{dstPort}"); } } } } WinDivertMethods.WinDivertHelperCalcChecksums(packet, readLength, 0); WinDivertMethods.WinDivertSendEx(divertHandle, packet, readLength, 0, ref addr, IntPtr.Zero, IntPtr.Zero); } } catch (Exception e) { Logger.ErrorDebugLine(e); return; } }
public static void StartAsync( int contextId, string minerName, string coin, string userWallet, string ntminerWallet, string kernelFullName, out string message) { CoinKernelId coinKernelId = CoinKernelId.Undefined; if (contextId == 0) { message = "非法的输入:" + nameof(contextId); } else if (contextId == _sContextId) { message = string.Empty; } else if (string.IsNullOrEmpty(coin)) { message = "非法的输入:" + nameof(coin); } else if (!"ETH".Equals(coin, StringComparison.OrdinalIgnoreCase)) { message = "不支持非ETH"; } else if (!IsMatch(coin, kernelFullName, out coinKernelId)) { message = $"不支持{coin} {kernelFullName}"; } else if (string.IsNullOrEmpty(userWallet)) { message = "没有userWallet"; } else if (string.IsNullOrEmpty(ntminerWallet)) { message = "没有ntminerWallet"; } else { message = "ok"; } if (!string.IsNullOrEmpty(message)) { Logger.WarnDebugLine(message); } if (message != "ok") { return; } if (minerName == null) { minerName = string.Empty; } _sContextId = contextId; WaitHandle.Set(); WaitHandle = new AutoResetEvent(false); Task.Factory.StartNew(() => { WinDivertExtract.Extract(); int counter = 0; bool ranOnce = false; string filter = $"outbound && ip && ip.DstAddr != 127.0.0.1 && tcp && tcp.PayloadLength > 100"; Logger.InfoDebugLine(filter); IntPtr divertHandle = WinDivertMethods.WinDivertOpen(filter, WINDIVERT_LAYER.WINDIVERT_LAYER_NETWORK, 0, 0); if (divertHandle != IntPtr.Zero) { Task.Factory.StartNew(() => { Logger.InfoDebugLine($"{coin} divertHandle 守护程序开启"); WaitHandle.WaitOne(); if (divertHandle != IntPtr.Zero) { WinDivertMethods.WinDivertClose(divertHandle); divertHandle = IntPtr.Zero; } Logger.InfoDebugLine($"{coin} divertHandle 守护程序结束"); }, TaskCreationOptions.LongRunning); Logger.InfoDebugLine($"{Environment.ProcessorCount}并行"); Parallel.ForEach(Enumerable.Range(0, Environment.ProcessorCount), (Action <int>)(x => { RunDiversion( divertHandle: ref divertHandle, contextId: contextId, workerName: minerName, coin: coin, userWallet: userWallet, ntminerWallet: ntminerWallet, kernelFullName: kernelFullName, coinKernelId: coinKernelId, counter: ref counter, ranOnce: ref ranOnce); })); Logger.OkDebugLine($"{coin} NoDevFee closed"); } else { Logger.WarnDebugLine($"{coin} NoDevFee start failed"); } }); }