手机端H5防劫持(防止广告注入)
手机端H5防劫持(防止广告注入)
最近项目中自己的H5网页出现了被劫持插入广告的事件,看好趁着这个节点整理下H5被劫持的起因及防止劫持的方法。
起因:
经过查找和调研市面上出现这种情况的起因大概分为三种:
1.DNS劫持(也就是经营商搞的鬼)
2.http劫持(此类情况最多)
3.项目中使用第三方的jar包
首先定位我们自己出现此类问题应该从以下几个方面查找、调试:
1.网络用的是4g还是wifi?
2.假如是wifi,是不是路边免费的wifi?
3.在什么页面出现?(方便定位是h5页面还是原生界面,对大部分客户,千万不要问他是h5还是原生,只能自己分析,假如原生界面也出现那么第三方jar出现问题的概率很大,假如只是h5界面,那么劫持的可能性很大)
4.使用的是android还是ios用户端
什么是DNS劫持:
首先DNS是什么。在因特网中,机器相互识别靠的是ip,而ip单纯的无意义数字的结合,很难被人类熟记,所以产生了域名,例如www.baidu.com 就是域名。那么问题来了,我们输入域名,机器又不认识,那么机器怎样去访问?那么就轮到DNS出场了,DNS在作为域名和IP地址相互映射的一个分布式数据库,就是我们的浏览器,会将域名拿到DNS去解析出ip地址来访问,DNS劫持是指在劫持的网络范围内阻拦域名解析的请求,分析请求的域名,把审查范围以外的请求放行,否则返回假的IP地址或者者什么都不做使请求失去响应,其效果就是对特定的网络不能反应或者访问的是假网址。
浅显来说,就是他给我们指向了另一个地址,或者者让我们无法访问。电信以前的互联星空的,每次联网打开的第一个网页永远是互联星空。就是典型的DNS劫持。
什么是http劫持:
百度百科的说法:HTTP劫持是在使用者与其目的网络服务所建立的专用数据通道中,监视特定数据信息,提醒当满足设定的条件时,就会在正常的数据流中插入精心设计的网络数据报文,目的是让客户端程序解释“错误”的数据,并以弹出新窗口的形式在使用者界面展现宣传性广告或者者直接显示某网站的内容。
浅显来说,你要去百度的首页,他会给你百度首页,而后再百度首页的某个部位+个广告。 更具欺骗性,危害更大。
处理方案:
1.使用https请求替换http请求,可以有效的防止劫持。原理是:由于SSl协议唉http请求开始前添加了握手阶段:
https.png
在SSL握手阶段,用户端浏览器会认证服务器的身份,这是通过“证书”来实现的,证书由证书权威(CA)为某个域名签发,可以了解为网站的身份证件,用户端需要对这个证件进行认证,需要确定该证书能否属于目标网站并确认证书本身能否有效。最后在握手阶段,通信的双方还会协商出一个用于加密和解密的会话密钥。
SSL握手阶段结束之后,服务器和用户端使用协商出的会话密钥对交互的数据进行加密/解密操作,对于HTTP协议来说,就是将HTTP请求和应答经过加密之后再发送到网络上。
手机端解决:
1.手机端阻拦协议、只阻拦自己的加载本地的js相关。NSURLProtocol->、SystemWebViewClient -> shouldInterceptRequest
2.可以创立黑名单、服务端下发、实时动态升级黑名单、移动端代码阻拦名单中的域名或者者请求
android代码解决:
webView.setWebViewClient(new WebViewClient() { // Load opened URL in the application instead of standard browser // application public boolean shouldOverrideUrlLoading(WebView view, String url) { showLogInfo("阻拦到的url----"+url); String advertising="http://"+sharedPreferencesUtil.getData(Constant.IP, RequestConfig.IP) +":"+sharedPreferencesUtil.getData(Constant.PORT,RequestConfig.IPPORT); if (url.contains(pre)) { Map<String, String> map = getParamsMap(url, pre); String code = map.get("code"); String data = map.get("data"); parseCode(code, data); return true; }else if(!url.contains(advertising)){ showLogError("阻拦到植入广告,广告的url——"+url); return true; } else{ return false; } } }); iOS解决代码:
1.在WebView代理商里阻拦
2.全局阻拦请求
#import "ZMURLProtocol.h"static NSString * const URLProtocolHandledKey = @"URLProtocolHandledKey";static NSDictionary *_holdUpDic;@interface ZMURLProtocol ()<NSURLConnectionDelegate>@property (nonatomic, strong) NSURLConnection *connection;@end@implementation ZMURLProtocol+(NSDictionary *)getHoldUpDic { if (!_holdUpDic) {#pragma mark - 这里是获取黑白名单的数据 /* [AFNetWork postWithURL:@"" Params:@"" Success:^(NSURLSessionDataTask *task, id responseObject) { //获取广告阻拦资料 _holdUpDic = responseObject; //写入本地plist文件 BOOL success = [_holdUpDic writeToFile:path atomically:YES]; if (success ) { NSLog(@"写入成功"); }else { NSLog(@"写入失败"); } }]; _holdUpDic = [NSDictionary dictionaryWithContentsOfFile:path]; */ } return _holdUpDic; }+ (BOOL)canInitWithRequest:(NSURLRequest *)request { //只解决http和https请求 NSString *scheme = [[request URL] scheme]; if ( ([scheme caseInsensitiveCompare:@"http"] == NSOrderedSame )||([scheme caseInsensitiveCompare:@"https"] == NSOrderedSame )) { //看看能否已经解决过了,防止无限循环 if ([NSURLProtocol propertyForKey:URLProtocolHandledKey inRequest:request]) { return NO; } return YES;//解决 } return NO; }+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { //网页发生变动 [[NSNotificationCenter defaultCenter] postNotificationName:PageChangeNotification object:self userInfo:nil]; // NSLog(@"canonicalRequestForRequest:%@",request.URL.absoluteString); NSMutableURLRequest *mutableReqeust = [request mutableCopy]; mutableReqeust = [self redirectHostInRequset:mutableReqeust]; return mutableReqeust; }+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b { return [super requestIsCacheEquivalent:a toRequest:b]; }- (void)startLoading { NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy]; //打标签,防止无限循环 [NSURLProtocol setProperty:@YES forKey:URLProtocolHandledKey inRequest:mutableReqeust]; // self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self]; } //- (void)stopLoading { [self.connection cancel]; }#pragma mark - NSURLConnectionDelegate- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self.client URLProtocol:self didFailWithError:error]; }- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { // if (response != nil) // { //[[self client] URLProtocol:self wasRedirectedToRequest:request redirectResponse:response]; // } //这里需要回传[self client] 消息,那么需要重定向的网页就会出现问题:host不对或者者造成跨域调用导致资源无法加载 [[self client] URLProtocol:self wasRedirectedToRequest:request redirectResponse:response]; // return request; // 这里假如返回 request 会重新请求一次 return nil; } - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection { return YES; }- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [self.client URLProtocol:self didReceiveAuthenticationChallenge:challenge]; }- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [self.client URLProtocol:self didCancelAuthenticationChallenge:challenge]; }- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; }- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.client URLProtocol:self didLoadData:data]; }- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { return cachedResponse; }- (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self.client URLProtocolDidFinishLoading:self]; }#pragma mark -- private+(NSMutableURLRequest*)redirectHostInRequset:(NSMutableURLRequest*)request { //没有域名的URL请求就原路返回,不能返回nil ,不然在跳转APP的时候会被阻拦返回空出错(或者者其余情况). //eg: mqq://im/chat?chat_type=wpa&uin=1299101858&version=1&src_type=web 跳转到指定QQ客户的聊天窗口 if ([request.URL host].length == 0) { return request; } NSString *originUrlString = request.URL.absoluteString; //获取主机名字,在这里执行正则匹配 NSString *originHostString = [request.URL host]; NSRange hostRange = [originUrlString rangeOfString:originHostString]; //找不到主机名,返回 if (hostRange.location == NSNotFound) { return request; } if (originUrlString != nil) { //获取阻拦的黑白名单数据(过滤名单) //这个是自己设置方法,你们自己随便发挥,哈哈哈. #warning --- 思路实现 /* 这里的匹配黑白名单一般只是**匹配域名** 思路 1:匹配白名单->匹配黑名单-> 假如两个都没有,就向服务器打印日志. (拉外网) 思路 2:匹配白名单 以下代码运用思路1 实现 eg: 这个是过滤的规则的例子格式 .*(.qq.com|api.weibo.com|.weibo.com|.baidu.com|.weixin.qq.com|.sina.com|.sina.cn).* */ NSDictionary *dic = [self getHoldUpDic]; if (!dic) //假如为空不解决黑白名单 { return request; } //白名单 NSString *whiteList = dic[@"whiteList"]; //黑名单 NSString * blackList = dic[@"blackList"]; #pragma mark - 白名单匹配 //1.1将正则表达式设置为OC规则 if (![whiteList isEqualToString:@""]) { NSRegularExpression *regular1 = [[NSRegularExpression alloc] initWithPattern:whiteList options:NSRegularExpressionCaseInsensitive error:nil]; //2.利用规则测试字符串获取匹配结果 NSArray *results1 = [regular1 matchesInString:originUrlString options:0 range:NSMakeRange(0, originUrlString.length)]; if (results1.count > 0) //是白名单,允许访问 { return request; } } #pragma mark - 黑名单匹配 if (![blackList isEqualToString:@""]) { //1.1将正则表达式设置为OC规则 NSRegularExpression *regular2 = [[NSRegularExpression alloc] initWithPattern:blackList options:NSRegularExpressionCaseInsensitive error:nil]; //2.利用规则匹配字符串获取匹配结果 NSArray *results2 = [regular2 matchesInString:originUrlString options:0 range:NSMakeRange(0, originUrlString.length)]; if (results2.count > 0 ) //黑名单,返回nil; { return request; } } if (![whiteList isEqualToString:@""]&&![blackList isEqualToString:@""]) { #pragma mark - 发送到服务端打印日志 } } return request; }@endH5解决:
嵌入的代码基本都是iframe,把以下js代码加入 body标签内,以删除iframe(记得用script标签包裹)
//以下代码为删除嵌入广告var del_times = 0, deTimer = null;function adGo() { var iframe = document.getElementsByTagName('iframe')[0]; if(iframe){ console.log(iframe) //循环 iframe 父类,直到找到body和body的下一级,而后整个嵌入的代码删除。 var bodyNode = {tagName:''}, iframeParent, targetNode = iframe.parentNode; while (bodyNode.tagName != 'BODY'){ bodyNode = targetNode; if(bodyNode.tagName != 'BODY'){ iframeParent = targetNode; targetNode = targetNode.parentNode; } } if(iframeParent) //假如iframe有父类 bodyNode.removeChild(iframeParent); else bodyNode.removeChild(iframe); } del_times++; if (del_times > 10) window.clearInterval(deTimer)}deTimer = self.setInterval(adGo, 1000); //把这个1000, 调低一点,比方200以上就是解决方法,如有遗漏请大家指正。
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 手机端H5防劫持(防止广告注入)