大多数开发人员都需要保证其代码的安全性。本文就会讲到 MQL5 软件的几种不同的保护方式。本文中所有示例均涉及 EA 交易,但相同的法则亦适用于脚本及指标。本文会从简单的密码保护开始,然后再讲解密钥生成器、授予某给定经纪账户许可以及时限防护。之后还会引入一种远程许可服务器理念。我在有关 MQL5-RPC 框架的上一篇文章中 ,讲到过从 MetaTrader 5 到任何 XML-RPC 服务器的“远程过程调用”内容。
我会利用该解决方案,作为远程许可的一个示例。我还会讲到如何利用 base64 编码增强该解决方案,并提供 PGP 支持建议,以实现对于 MQL5 EA 交易与指标的超安全保护。我知道,MetaQuotes Software Corp. 会提供一些直接通过 MQL5.com 市场部分授予代码许可的选项。这对于所有开发人员而言,都非常有好处,而且不会与本文中所述理念出现冲突。两种解决方案结合使用,只会让防护更加有力,在对抗软件盗版的问题上也会更加安全。
我们从简单的地方开始。计算机软件防护中最最常用的解决方案,就是密码或许可证密钥防护。安装后第一次运行期间,就会出现一个对话框,要求用户插入一个与软件捆绑的密码(类似于 Microsoft Windows 或 Microsoft Office 序列号),如果输入的密码匹配,则允许用户使用软件的一份单独注册拷贝。我们可以利用一个输入变量或直接文本框来输入代码。下面所示为存根代码示例。
下方代码会初始化一个用于插入密码的 CChartObjectEdit 字段。有一个预先定义的允许密码数组,与将由用户插入的密码形成匹配。密码会在接收到 CHARTEVENT_OBJECT_ENDEDIT 事件后,在 OnChartEvent() 方法中进行检查。
//+------------------------------------------------------------------+ //| PasswordProtectedEA.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" #include <ChartObjects/ChartObjectsTxtControls.mqh> CChartObjectEdit password_edit; const string allowed_passwords[] = { "863H-6738725-JG76364", "145G-8927523-JG76364", "263H-7663233-JG76364" }; int password_status = -1; string password_message[] = { "WRONG PASSWORD. Trading not allowed.", "EA PASSWORD verified." }; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- password_edit.Create(0, "password_edit", 0, 10, 10, 260, 25); password_edit.BackColor(White); password_edit.BorderColor(Black); password_edit.SetInteger(OBJPROP_SELECTED, 0, true); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- password_edit.Delete(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if (password_status>0) { // password correct } } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" ) { password_status = -1; for (int i=0; i<ArraySize(allowed_passwords); i++) if (password_edit.GetString(OBJPROP_TEXT) == allowed_passwords[i]) { password_status = i; break; } if (password_status == -1) password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]); else password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]); } } //+------------------------------------------------------------------+
这种方法很简单,但一旦有人将破解的序列号发布到网上,便会丧失防护能力。EA 作者无能为力,除非发布新版本 EA 交易,以及将被盗的密码列入黑名单。
密钥生成器是一种允许使用一系列基于预先定义规则的密码的机制。我会通过为下面的密钥生成器提供一个存根的方式,大概地讲一下。在下述示例中,密钥必须由用两个连字符分隔开来的三个数字构成。由此,密码允许的格式即为 XXXXX-XXXXX-XXXXX。
第一个数必须可被 3 整除,第二个数必须可被 4 整除,而第三个数则必须可被 5 整除。由此,允许的密码可能是 3-4-5、18000-20000-20000 又或是更加复杂的 3708-102792-2844770。
//+------------------------------------------------------------------+ //| KeyGeneratorProtectedEA.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" #include <ChartObjects/ChartObjectsTxtControls.mqh> #include <Strings/String.mqh> CChartObjectEdit password_edit; CString user_pass; const double divisor_sequence[] = { 3.0, 4.0, 5.0 }; int password_status = -1; string password_message[] = { "WRONG PASSWORD. Trading not allowed.", "EA PASSWORD verified." }; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- password_edit.Create(0, "password_edit", 0, 10, 10, 260, 25); password_edit.BackColor(White); password_edit.BorderColor(Black); password_edit.SetInteger(OBJPROP_SELECTED, 0, true); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- password_edit.Delete(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if (password_status==3) { // password correct } } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" ) { password_status = 0; user_pass.Assign(password_edit.GetString(OBJPROP_TEXT)); int hyphen_1 = user_pass.Find(0, "-"); int hyphen_2 = user_pass.FindRev("-"); if (hyphen_1 == -1 || hyphen_2 == -1 || hyphen_1 == hyphen_2) { password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]); return; } ; long pass_1 = StringToInteger(user_pass.Mid(0, hyphen_1)); long pass_2 = StringToInteger(user_pass.Mid(hyphen_1 + 1, hyphen_2)); long pass_3 = StringToInteger(user_pass.Mid(hyphen_2 + 1, StringLen(user_pass.Str()))); // PrintFormat("%d : %d : %d", pass_1, pass_2, pass_3); if (MathIsValidNumber(pass_1) && MathMod((double)pass_1, divisor_sequence[0]) == 0.0) password_status++; if (MathIsValidNumber(pass_2) && MathMod((double)pass_2, divisor_sequence[1]) == 0.0) password_status++; if (MathIsValidNumber(pass_3) && MathMod((double)pass_3, divisor_sequence[2]) == 0.0) password_status++; if (password_status != 3) password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]); else password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]); } } //+------------------------------------------------------------------+
当然,数字中的位数可设定为某个给定值,而计算亦可更加复杂。有人可能还将硬盘序列号或 CPU ID 添加到计算当中,从而添加一个只对某给定硬件有效的变量。这种情况下,运行该 EA 的人就必须要运行基本此硬件计算得出的另一个生成器。
输出会是向某个密钥生成器的一个输入,而生成的密码则仅对某给定的硬件有效。如果有人更改计算机硬件,或是利用虚拟专用服务器运行 EA,这种方法也会出现局限,但可以通过放弃两三个有效密码解决。MQL5 网站市场部分的情况就是如此。
由于任何给定经纪终端的账户号都是独一无二的,所以,可利用这一点,来允许 EA 在一个或一组账户号上使用。这种情况下,利用 AccountInfoString(ACCOUNT_COMPANY) 和 AccountInfoInteger(ACCOUNT_LOGIN) 方法来获取账户资料并与预先编译的允许值进行比对就足够了:
//+------------------------------------------------------------------+ //| AccountProtectedEA.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" const string allowed_broker = "MetaQuotes Software Corp."; const long allowed_accounts[] = { 979890, 436290, 646490, 225690, 279260 }; int password_status = -1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- string broker = AccountInfoString(ACCOUNT_COMPANY); long account = AccountInfoInteger(ACCOUNT_LOGIN); printf("The name of the broker = %s", broker); printf("Account number = %d", account); if (broker == allowed_broker) for (int i=0; i<ArraySize(allowed_accounts); i++) if (account == allowed_accounts[i]) { password_status = 1; Print("EA account verified"); break; } if (password_status == -1) Print("EA is not allowed to run on this account."); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if (password_status == 1) { // password correct } }
这是一种简单但又非常有力的保护方法。而其缺点在于,有必要就添加到账户数据库的每一个新的账户号重新编译 EA。
如果许可是临时授予,则会用到时限保护。比如使用软件的试用版本,或是许可按月或按年授予。正因如此,很明显,这种保护可应用于 EA 交易与指标。
首先想到的是检查服务器时间,并以此为基础,让用户在给定期间内使用指标或 EA 交易。待其过期后,许可方能够部分或全部地禁用其许可功能。
//+------------------------------------------------------------------+ //| TimeLimitProtectedEA.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" datetime allowed_until = D'2012.02.11 00:00'; int password_status = -1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- printf("This EA is valid until %s", TimeToString(allowed_until, TIME_DATE|TIME_MINUTES)); datetime now = TimeCurrent(); if (now < allowed_until) Print("EA time limit verified, EA init time : " + TimeToString(now, TIME_DATE|TIME_MINUTES)); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if (TimeCurrent() < allowed_until) { } else Print("EA expired."); }
该解决方案唯一的缺点在于,它必须要针对每个被许可方单独编译。
要是按每位用户完全控制是否禁用许可或延长试用期限,那不是很好?这一点,只需使用 MQL5-RPC 调用即可办到。它会发送一个带账户名的查询,并接收是按试用模式运行脚本、还是禁用它的值。
下述代码即为一个样本实施:
from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler class RequestHandler( SimpleXMLRPCRequestHandler ): rpc_path = ( '/RPC2', ) class RemoteLicenseExample(SimpleXMLRPCServer): def __init__(self): SimpleXMLRPCServer.__init__( self, ("192.168.2.103", 9099), requestHandler=RequestHandler, logRequests = False) self.register_introspection_functions() self.register_function( self.xmlrpc_isValid, "isValid" ) self.licenses = {} def addLicense(self, ea_name, broker_name, account_number): if ea_name in self.licenses: self.licenses[ea_name].append({ 'broker_name': broker_name, 'account_number' : account_number }) else: self.licenses[ea_name] = [ { 'broker_name': broker_name, 'account_number' : account_number } ] def listLicenses(self): print self.licenses def xmlrpc_isValid(self, ea_name, broker_name, account_number): isValidLicense = False ea_name = str(ea_name) broker_name = str(broker_name) print "Request for license", ea_name, broker_name, account_number try: account_number = int(account_number) except ValueError as error: return isValidLicense if ea_name in self.licenses: for license in self.licenses[ea_name]: if license['broker_name'] == broker_name and license['account_number'] == account_number: isValidLicense = True break print "License valid:", isValidLicense return isValidLicense if __name__ == '__main__': server = RemoteLicenseExample() server.addLicense("RemoteProtectedEA", "MetaQuotes Software Corp.", 1024221) server.addLicense("RemoteProtectedEA", "MetaQuotes Software Corp.", 1024223) server.listLicenses() server.serve_forever()
这是一个在 Python 中实施的简单的 XML-RPC 服务器,带有两个预先定义的 MetaTrader 5 许可。这些许可专为运行于默认 MetaQuotes 演示服务器 (access.metatrader5.com:443) 上、账号为 1024221 和 1024223 的 "RemoteProtectedEA" EA 交易而设。而行业解决方案则很可能利用 Postgresql 或任何其它数据库中的一个许可数据库,但上方示例对于本文来讲已经足够了,因为它会很好地处理远程许可。
如果您需要如何安装 Python 的简短说明,请阅读 《MQL5-RPC,来自 MQL5 的远程过程调用:针对乐趣及获利的网络服务访问及 XML-RPC 自动交易锦标赛分析程序》。
使用远程许可的 EA 只需要准备一个远程 MQL5-RPC 调用到 isValid() 方法,并根据许可是否有效而返回 true 或 false 布尔值。下方所示为基于账户防护的一个样本 EA 示例:
//+------------------------------------------------------------------+ //| RemoteProtectedEA.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" #include <MQL5-RPC.mqh> #include <Arrays\ArrayObj.mqh> #include <Arrays\ArrayInt.mqh> #include <Arrays\ArrayString.mqh> #include <Arrays\ArrayBool.mqh> bool license_status=false; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- /* License proxy server */ CXMLRPCServerProxy s("192.168.2.103:9099"); if(s.isConnected()==true) { CXMLRPCResult *result; /* Get account data */ string broker= AccountInfoString(ACCOUNT_COMPANY); long account = AccountInfoInteger(ACCOUNT_LOGIN); printf("The name of the broker = %s",broker); printf("Account number = %d",account); /* Get remote license status */ CArrayObj* params= new CArrayObj; CArrayString* ea = new CArrayString; CArrayString* br = new CArrayString; CArrayInt *ac=new CArrayInt; ea.Add("RemoteProtectedEA"); br.Add(broker); ac.Add((int)account); params.Add(ea); params.Add(br); params.Add(ac); CXMLRPCQuery query("isValid",params); result=s.execute(query); CArrayObj *resultArray=result.getResults(); if(resultArray!=NULL && resultArray.At(0).Type()==TYPE_BOOL) { CArrayBool *stats=resultArray.At(0); license_status=stats.At(0); } else license_status=false; if(license_status==true) printf("License valid."); else printf("License invalid."); delete params; delete result; } else Print("License server not connected."); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(license_status==true) { // license valid } } //+------------------------------------------------------------------+
如果您执行两个脚本,则能为您的账户号添加一个远程许可。这种远程许可亦能作为试用期过后可以远程去除激活的时限许可或密码许可。比如说,您赋予某人使用 EA 10 天试用的权限,如其对该产品不满意,您就可以去除该许可的激活状态;而如其满意,您则可以激活许可任意给定的时长。
上一段中讲述的理念,是利用“远程过程调用”在许可服务器与客户端之间交换信息。而这可能通过在 EA 某注册拷贝上使用封包嗅探器被破解。通过使用嗅探程序,黑客就能捕获到两台机器之间发送的所有 TCP 数据包。而我们则通过使用 base64 编码发送账户数据的方式来克服这一问题并接收加密信息。
对于经验老道的人来讲,还可能使用 PGP 和(或)将所有代码置入一个 DLL 中以进一步加强防护。我冒出了这样一个想法:该信息实际上会成为另一种将被进一步转换为 MQL5 数据的 RPC 信息(像 俄罗斯嵌套娃娃一样)。
第一步是为 MQL5-RPC 添加 base64 编码与解码支持。幸运的是,此举已于 MetaTrader 4 https://www.mql5.com/zh/code/8098 由 Renat 完成,因此,我只需要将其转换为 MQL5。
//+------------------------------------------------------------------+ //| Base64.mq4 | //| Copyright © 2006, MetaQuotes Software Corp. | //| MT5 version © 2012, Investeo.pl | //| https://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, MetaQuotes Software Corp." #property link "https://www.metaquotes.net" static uchar ExtBase64Encode[64]={ 'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z', '0','1','2','3','4','5','6','7','8','9','+','/' }; static uchar ExtBase64Decode[256]={ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; void Base64Encode(string in,string &out) { int i=0,pad=0,len=StringLen(in); while(i<len) { int b3,b2,b1=StringGetCharacter(in,i); i++; if(i>=len) { b2=0; b3=0; pad=2; } else { b2=StringGetCharacter(in,i); i++; if(i>=len) { b3=0; pad=1; } else { b3=StringGetCharacter(in,i); i++; } } //---- int c1=(b1 >> 2); int c2=(((b1 & 0x3) << 4) | (b2 >> 4)); int c3=(((b2 & 0xf) << 2) | (b3 >> 6)); int c4=(b3 & 0x3f); out=out+CharToString(ExtBase64Encode[c1]); out=out+CharToString(ExtBase64Encode[c2]); switch(pad) { case 0: out=out+CharToString(ExtBase64Encode[c3]); out=out+CharToString(ExtBase64Encode[c4]); break; case 1: out=out+CharToString(ExtBase64Encode[c3]); out=out+"="; break; case 2: out=out+"=="; break; } } //---- } void Base64Decode(string in,string &out) { int i=0,len=StringLen(in); int shift=0,accum=0; while(i<len) { int value=ExtBase64Decode[StringGetCharacter(in,i)]; if(value<0 || value>63) break; accum<<=6; shift+=6; accum|=value; if(shift>=8) { shift-=8; value=accum >> shift; out=out+CharToString((uchar)(value & 0xFF)); } i++; } //---- } //+------------------------------------------------------------------+
欲知 base64 编码描述详情,敬请访问 维基文章。
下面所示为 MQL5 base64 编码与解码脚本的样本测试:
//+------------------------------------------------------------------+ //| Base64Test.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" #include <Base64.mqh> //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- string to_encode = "<test>Abrakadabra</test>"; string encoded; string decoded; Base64Encode(to_encode, encoded); Print(encoded); Base64Decode(encoded, decoded); Print(decoded); } //+------------------------------------------------------------------+
该脚本会生成下述结果。
DK 0 Base64Test (EURUSD,H1) 16:21:13 Original string: <test>Abrakadabra</test> PO 0 Base64Test (EURUSD,H1) 16:21:13 Base64 encoded string: PHRlc3Q+QWJyYWthZGFicmE8L3Rlc3Q+ FM 0 Base64Test (EURUSD,H1) 16:21:13 Base64 decoded string: <test>Abrakadabra</test>
编码的有效性可于 Python 的 4 行代码中轻松查得:
import base64 encoded = 'PHRlc3Q+QWJyYWthZGFicmE8L3Rlc3Q+' decoded = base64.b64decode(encoded) print decoded <test>Abrakadabra</test>
第二步,是利用 base64 (又名“嵌套玩偶技术”)加密 XMLRPC 结果:
import base64 from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler class RequestHandler( SimpleXMLRPCRequestHandler ): rpc_path = ( '/RPC2', ) class RemoteLicenseExampleBase64(SimpleXMLRPCServer): def __init__(self): SimpleXMLRPCServer.__init__( self, ("192.168.2.103", 9099), requestHandler=RequestHandler, logRequests = False) self.register_introspection_functions() self.register_function( self.xmlrpc_isValid, "isValid" ) self.licenses = {} def addLicense(self, ea_name, broker_name, account_number): if ea_name in self.licenses: self.licenses[ea_name].append({ 'broker_name': broker_name, 'account_number' : account_number }) else: self.licenses[ea_name] = [ { 'broker_name': broker_name, 'account_number' : account_number } ] def listLicenses(self): print self.licenses def xmlrpc_isValid(self, ea_name, broker_name, account_number): isValidLicense = False ea_name = str(ea_name) broker_name = str(broker_name) print "Request for license", ea_name, broker_name, account_number try: account_number = int(account_number) except ValueError as error: return isValidLicense if ea_name in self.licenses: for license in self.licenses[ea_name]: if license['broker_name'] == broker_name and license['account_number'] == account_number: isValidLicense = True break print "License valid:", isValidLicense # additional xml encoded with base64 xml_response = "<?xml version='1.0'?><methodResponse><params><param><value><boolean>%d</boolean></value></param></params></methodResponse>" retval = xml_response % int(isValidLicense) return base64.b64encode(retval) if __name__ == '__main__': server = RemoteLicenseExampleBase64() server.addLicense("RemoteProtectedEA", "MetaQuotes Software Corp.", 1024221) server.addLicense("RemoteProtectedEA", "MetaQuotes Software Corp.", 1024223) server.listLicenses() server.serve_forever()
许可加密后,我们就可以利用 MQL5-RPC 方法将解密信息转换回 MQL5 数据:
//+------------------------------------------------------------------+ //| RemoteProtectedEABase64.mq5 | //| Copyright 2012, Investeo.pl | //| http://www.investeo.pl | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, Investeo.pl" #property link "http://www.investeo.pl" #property version "1.00" #include <MQL5-RPC.mqh> #include <Arrays\ArrayObj.mqh> #include <Arrays\ArrayInt.mqh> #include <Arrays\ArrayString.mqh> #include <Arrays\ArrayBool.mqh> #include <Base64.mqh> bool license_status=false; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- /* License proxy server */ CXMLRPCServerProxy s("192.168.2.103:9099"); if(s.isConnected()==true) { CXMLRPCResult *result; /* Get account data */ string broker= AccountInfoString(ACCOUNT_COMPANY); long account = AccountInfoInteger(ACCOUNT_LOGIN); printf("The name of the broker = %s",broker); printf("Account number = %d",account); /* Get remote license status */ CArrayObj* params= new CArrayObj; CArrayString* ea = new CArrayString; CArrayString* br = new CArrayString; CArrayInt *ac=new CArrayInt; ea.Add("RemoteProtectedEA"); br.Add(broker); ac.Add((int)account); params.Add(ea); params.Add(br); params.Add(ac); CXMLRPCQuery query("isValid",params); result=s.execute(query); CArrayObj *resultArray=result.getResults(); if(resultArray!=NULL && resultArray.At(0).Type()==TYPE_STRING) { CArrayString *stats=resultArray.At(0); string license_encoded=stats.At(0); printf("encoded license: %s",license_encoded); string license_decoded; Base64Decode(license_encoded,license_decoded); printf("decoded license: %s",license_decoded); CXMLRPCResult license(license_decoded); resultArray=license.getResults(); CArrayBool *bstats=resultArray.At(0); license_status=bstats.At(0); } else license_status=false; if(license_status==true) printf("License valid."); else printf("License invalid."); delete params; delete result; } else Print("License server not connected."); //--- return(0); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(license_status==true) { // license valid } } //+------------------------------------------------------------------+
RemoteLicenseExampleBase64 服务器运行前提下,该脚本的运行结果如下:
KI 0 RemoteProtectedEABase64 (EURUSD,H1) 19:47:57 The name of the broker = MetaQuotes Software Corp. GP 0 RemoteProtectedEABase64 (EURUSD,H1) 19:47:57 Account number = 1024223 EM 0 RemoteProtectedEABase64 (EURUSD,H1) 19:47:57 <?xml version='1.0'?><methodResponse><params><param><value><string>PD94bWwgdmVyc2lvbj0nMS4wJz8+PG1ldGhvZFJlc3BvbnNlPjxwYXJhbXM+PHBhcmFtPjx2YWx1ZT48Ym9vbGVhbj4xPC9ib29sZWFuPjwvdmFsdWU+PC9wYXJhbT48L3BhcmFtcz48L21ldGhvZFJlc3BvbnNlPg==</string></value></param></params></methodResponse> DG 0 RemoteProtectedEABase64 (EURUSD,H1) 19:47:57 encoded license: PD94bWwgdmVyc2lvbj0nMS4wJz8+PG1ldGhvZFJlc3BvbnNlPjxwYXJhbXM+PHBhcmFtPjx2YWx1ZT48Ym9vbGVhbj4xPC9ib29sZWFuPjwvdmFsdWU+PC9wYXJhbT48L3BhcmFtcz48L21ldGhvZFJlc3BvbnNlPg== FL 0 RemoteProtectedEABase64 (EURUSD,H1) 19:47:57 decoded license: <?xml version='1.0'?><methodResponse><params><param><value><boolean>1</boolean></value></param></params></methodResponse> QL 0 RemoteProtectedEABase64 (EURUSD,H1) 19:47:57 License valid.
您可以看出,XML-RPC 的有效负荷包含一个实际上是经过 base64 加密的 XML-RPC 信息的字符串。这种经过 base64 编码的信息,会被解码为 XML 字符串,之后再被解码为 MQL5 数据。
只要 MQL5 代码被反编译,就算是最为牢固的防护,在面对经验老道的逆向工程人员时,也都会被轻松破解。经过一些网上搜索之后,我发现了一个提供 MQL5 反编译器的网站,但我怀疑这是个假冒货色,只是想从那些乐于偷窃他人代码的天真人群中骗些钱而已。不过,我没去试,所以可能我是错的。就算这种解决方案真有,您也要具备通过发送加密 EA/指标输入参数或对象指数传递的方式实现更强保护的能力。
对于黑客来讲,要获取受保护 EA 的正确输入参数,或是查看受保护指标的正确输入参数(反过来又会令其毫无用处)非常难。还有可能这样:如果账户 ID 匹配,则发送正确参数;或者,如果账户 ID 无效,则发送未经加密的假参数。针对这种解决方案,有人可能想要使用 PGP (良好隐私)。就算代码被反编译,数据也会利用私人的 PGP 密钥加密发送,而且,只有在账户 ID 与 PGP 密钥匹配的情况下,才可能解密 EA 参数。
我在本文中讲到了保护 MQL5 代码的几种方法。我还介绍了经由 MQL5-RPC 调用的远程许可理念和添加 base64 编码支持。我希望本文能够作为如何确保 MQL5 代码安全的深入理念的一个基础。本文附带了全部源代码。
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程