1.前言
公司要为别的公司提供服务器防护,采用本地环回,节点转发的方法实现对服务器真实ip进行隐藏。但是有一个问题:我节点端要创建一个端口接收映射数据和保活数据,采用固定端口的话,攻击者很容易通过扫描端口来获取我较多的节点,进行攻击
2.解决
这里采用的是通过节点ip来生成一个随机端口,客户端和节点通过这个随机端口来进行数据通信
3.代码片段
由ip生成随机端口的一个算法
std::string generateport(const std::string &strvalue)
{
std::string strport("");
if (!strvalue.empty())
{
std::string strip = strvalue;
deletechar(strip, '.');
int nfirst = (strip.at(strip.size() - 1) - '0') % 5 + 5;
int nsecond = 0, nthird = 1, nfourth = 0;
for (std::string::iterator it = strip.begin(); it != strip.end(); ++it)
{
nsecond += (*it - '0');
nthird *= (*it - '0');
if (*it >= '3' && *it <= '9')
nfourth += (*it - '0');
}
nsecond %= 10;
nthird %= 10;
nfourth %= 10;
char szport[64] = {0};
sprintf(szport, "%d%d%d%d", nfirst, nsecond, nthird, nfourth);
strport = szport;
}
return strport;
}
4.备注
1.但是在实际中发现生成的随机端口可能是木马端口,被360直接误报,这是很纠结的,后续再进行优化
2.由ip生成端口部分算法不能保证所有ip产生的端口都是唯一的,但是可以保证大部分ip产生的端口都不一样,后续还要继续进行优化处理