登录
首页 >  Golang >  Go问答

正则表达式捕获 IP 中的错误八位字节

来源:stackoverflow

时间:2024-02-09 10:57:25 356浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《正则表达式捕获 IP 中的错误八位字节》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

您好,有人可以解释一下为什么 ip 的最后一个八位字节(如果 01 或 001 没有被这个正则表达式捕获)吗?

(\.?)([2-9][5-9][6-9]|[3-9][0-9][0-9]|0[0-9][0-9]?)($|\.)

debuggex 演示

作为代码示例

badoctedipv4 := "(\\.?)([2-9][5-9][6-9]|[3-9][0-9][0-9]|0[0-9][0-9]?)($|\\.)"
ipv4format := badoctedipv4
matchme := regexp.mustcompile(ipv4format)
return matchme.findstring(input)

输入数据如下所示:

10.185.248.71 - - [09/jan/2015:19:12:06 +0000] 808840 "get /inventoryservice/inventory/purchaseitem?userid=20253471&itemid=23434300 http/1.1" 500 17 "-" "apache-httpclient/4.2.6 (java 1.5)"
[thu mar 13 19:04:13 2014] [error] [client 50.0.134.125] file does not exist: /var/www/favicon.ico
192.168.000.254 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 10 bad
092.168.000.254 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 9 bad
123.234.345.001 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 8 bad
123.234.145.001 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 7 bad
345.234.123.1 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 6 bad
092.168.72.177 - - [22/dec/2002:23:32:14 -0400] "get /favicon.ico http/1.1" 404 1997 www.yahoo.com "-" "mozilla/5.0 (windows; u; windows nt 5.1; rv:1.7.3)..." "-" 5 bad
123.234.145.001 - - 4 bad
123.234.145.01 - - 3 bad
123.234.05.100 - - 2 bad
123.234.005.100 - - 1 bad
123.234.5.100 - - last entry

以上代码返回的结果仅找到除最后一个 001 或 01 之外的所有错误 ip 八位字节

程序的输出:

❯ go run ./findInvalidIPv4.go logfile.log
[192.168.000.254] : [.000.] : 192.168.000.254 - - [13/Sep/2006:07:01:51 -0700] "PROPFIND /svn/[xxxx]/[xxxx]/trunk HTTP/1.1" 401 587 10 bad
[092.168.000.254] : [ 092.] : 092.168.000.254 - - [13/Sep/2006:07:01:51 -0700] "PROPFIND /svn/[xxxx]/[xxxx]/trunk HTTP/1.1" 401 587 9 bad
[123.234.345.001] : [.345.] : 123.234.345.001 - - [13/Sep/2006:07:01:51 -0700] "PROPFIND /svn/[xxxx]/[xxxx]/trunk HTTP/1.1" 401 587 8 bad
[  345.234.123.1] : [ 345.] : 345.234.123.1 - - [13/Sep/2006:07:01:51 -0700] "PROPFIND /svn/[xxxx]/[xxxx]/trunk HTTP/1.1" 401 587 6 bad
[ 092.168.72.177] : [ 092.] : 092.168.72.177 - - [22/Dec/2002:23:32:14 -0400] "GET /favicon.ico HTTP/1.1" 404 1997 www.yahoo.com "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)..." "-" 5 bad
[ 123.234.05.100] : [ .05.] : 123.234.05.100 - - 2 bad
[123.234.005.100] : [.005.] : 123.234.005.100 - - 1 bad

输出解释:

  • 第一列[...]它是发现坏八位组的完整坏ip
  • 第二列[...]它是错误的八位字节...第一个匹配就足够了
  • 第三列是传递给上面函数的完整行

有人可以指出我缺少什么以及为什么最后的 001 与模式不匹配吗?

谢谢


正确答案


@selbie 再次感谢您的帮助,似乎有了这里的所有建议,我越来越接近解决这个问题,这个正则表达式 (\.|^)([2-9][5-9][6-9]|[3-9][0-9][0-9]|0[0-9]+) 似乎它几乎满足了我所有需要的需求

[  192.168.2.001] : [ .001] : 192.168.2.001 - - [28/jul/2006:10:27:10 -0300] "get /cgi-bin/try/ http/1.0" 200 3395
[192.168.000.254] : [ .000] : 192.168.000.254 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 10 bad
[092.168.000.254] : [  092] : 092.168.000.254 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 9 bad
[123.234.345.001] : [ .345] : 123.234.345.001 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 8 bad
[123.234.145.001] : [ .001] : 123.234.145.001 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 7 bad
[  345.234.123.1] : [  345] : 345.234.123.1 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 6 bad
[  300.234.123.1] : [  300] : 300.234.123.1 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 6 bad
[300.300.300.300] : [  300] : 300.300.300.300 - - [13/sep/2006:07:01:51 -0700] "propfind /svn/[xxxx]/[xxxx]/trunk http/1.1" 401 587 6 bad
[ 092.168.72.177] : [  092] : 092.168.72.177 - - [22/dec/2002:23:32:14 -0400] "get /favicon.ico http/1.1" 404 1997 www.yahoo.com "-" "mozilla/5.0 (windows; u; windows nt 5.1; rv:1.7.3)..." "-" 5 bad
[123.234.145.001] : [ .001] : 123.234.145.001 - - 4 bad
[ 123.234.145.01] : [  .01] : 123.234.145.01 - - 3 bad
[ 123.234.05.100] : [  .05] : 123.234.05.100 - - 2 bad
[123.234.005.100] : [ .005] : 123.234.005.100 - - 1 bad

并且它跳过了像200.200.200.200或100.100.100.100这样的好ip 所以我们越来越接近让该模式工作,现在我看到混乱的唯一情况是当我有时间字符串 02:49:12 时,它启动字符串 02 等等作为示例:

[      127.0.0.1] : [   02] : 02:49:12 127.0.0.1 GET / 200
[      127.0.0.1] : [   02] : 02:49:35 127.0.0.1 GET /index.html 200
[      127.0.0.1] : [   03] : 03:01:06 127.0.0.1 GET /images/sponsered.gif 304
[      127.0.0.1] : [   03] : 03:52:36 127.0.0.1 GET /search.php 200
[      127.0.0.1] : [   04] : 04:17:03 127.0.0.1 GET /admin/style.css 200
[      127.0.0.1] : [   05] : 05:04:54 127.0.0.1 GET /favicon.ico 404
[      127.0.0.1] : [   05] : 05:38:07 127.0.0.1 GET /js/ads.js 200

所以我仍在寻找正则表达式中我缺少的答案

==================================

编辑 好的,这似乎可以完成工作,并且能够找到错误的 ip 八位字节 (\.|^)([2-9][5-9][6-9]|[3-9][0-9][0-9]|0[0-9]+)([ ^:/-]) 添加了 lat 3rd 组 ([^:/-]) 以排除任何带有两位数字的时间格式

最后的组 3:

($|\.)

坚持在最后一个八位字节之后出现一个点或行尾字符。对于保证有 . 继续执行的前三个八位位组来说,这很好。但最后一个就不行了。

简单的修复方法就是删除它或将其设为可选:

(\.?)([2-9][5-9][6-9]|[3-9][0-9][0-9]|0[0-9][0 -9]?)($|\.?)

为第 3 组添加空格:

(\.?)([2-9][5-9][6-9]|[3-9][0-9][0-9]|0[0-9][0 -9]?)(\s|$|\.)

或者直接删除它:

(\.?)([2-9][5-9][6-9]|[3-9][0-9][0-9]|0[0-9][0 -9]?)

所有这些都有问题。因此,也许您真正想要的是将任何 3 位数字序列与前导点或尾随点相匹配。

\.[2-9][5-9][6-9]|\.[3-9][0-9][0-9]|\.0[0-9][0 -9]|\[2-9][5-9][6-9]\.|[3-9][0-9][0-9]\.|0[0-9][0- 9]\.

我们开始进入正则表达式“一次写入,不再读取”的领域。

今天关于《正则表达式捕获 IP 中的错误八位字节》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>