auth.log日志查看

     新手文章!!!请多指教!      
     以前一直想分析下自己主机的log日志,来看看究竟是哪些人想入侵我的主机(虽然我现在主机是个实验性质的,里面几乎没有什么有价值的东西)。
     我的主机系统为ubuntu server 12.04 64位。虽然我很早就知道auth.log记录了登陆的信息,但由于shell的基本功不好,面对着长长的log不知道如何下手。这些天看了下shell脚本的写法,终于有了点眉目。

Feb  3 07:44:39 server sshd[28013]: reverse mapping checking getaddrinfo for mail1.tdabbat.ru [88.147.142.29] failed - POSSIBLE BREAK-IN ATTEMPT!
Feb  3 07:44:34 server sshd[28011]: Invalid user recruit from 88.147.142.29
Feb  3 07:44:34 server sshd[28011]: input_userauth_request: invalid user recruit [preauth]
Feb  3 07:44:34 server sshd[28011]: pam_unix(sshd:auth): check pass; user unknown
Feb  3 07:44:34 server sshd[28011]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=88.147.142.29
Feb  3 07:44:36 server sshd[28011]: Failed password for invalid user recruit from 88.147.142.29 port 36392 ssh2
Feb  3 07:44:36 server sshd[28011]: Received disconnect from 88.147.142.29: 11: Bye Bye [preauth]
     上面为auth.log里面一条基本的ssh认证失败记录。这次我想做的就是把里面的IP地址以及用户名提取出来,统计一下次数。
     首先,我使用grep提取出所有带IP地址的行。
grep -i 'invalid' $1 |\
    grep -v 'Failed' |\
    grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' |\
     这个表达式首先提取了带invalid且不带Failed的这一行,因为每一次失败的log记录有好几条,而我只需要一条就可以了。类似于下面这行:
Feb  3 07:44:26 server sshd[28006]: Invalid user staff from 88.147.142.29
      当所有记录提取成这样后,就可以轻松地使用awk来提取想要的字段了。比如我想提取用户名和IP地址,使用awk就是:
awk '{print $8,$10}'
       如果还想有进一步的操作,只需再在结果上处理即可。下面是我提取最近试图入侵我的主机的统计信息(第一行为次数,第二行为IP):
8 107.23.144.48
    133 110.75.189.5
      8 114.80.138.6
    156 119.163.120.182
      3 121.101.222.146
    186 121.125.73.22
      2 122.192.35.240
     53 122.225.96.117
      5 123.127.160.102
    649 151.237.189.113
      1 180.186.74.94
     34 183.61.244.190
      2 192.34.62.41
      3 210.48.231.70
      1 211.100.61.26
     11 219.239.113.241
    112 221.132.73.154
    130 58.215.75.198
      4 60.21.206.60
  24418 61.182.202.57
      7 61.236.64.56
      2 61.240.36.1
     36 88.147.142.29
      由上可以看到,IP地址61.182.202.57共进行了24418次尝试。而尝试比较多的用户名如下(第一行为次数,第二行为用户名):
102 test
    101 oracle
     43 admin
     42 nagios
     29 guest
     21 webmaster
     20 user
     20 mythtv
     17 david
     17 dan
     15 test1
     14 tomcat
     14 recruit
     14 hasegawa
     14 ftpuser
     13 www
      由上可以看出,一些简单的用户名尝试的次数更多,更容易被破解。所以一个好的用户名也是很重要的。
     上面这些数据只是我个人主机在某一段时间所得,没有特别的统计意义哈。另外,希望有热心的朋友指教!3Q

转载于:https://my.oschina.net/dmdgeeker/blog/108377

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## 什么是graylog Graylog 是一个简单易用、功能较全面的日志管理工具,相比 ELK 组合, 优点: - 部署维护简单 - 查询语法简单易懂(对比ES的语法…) - 内置简单的告警 - 可以将搜索结果导出为 json - 提供简单的聚合统计功能 - UI 比较友好 - 当然, 拓展性上比 ELK 差很多。 整套依赖: - Graylog 提供 graylog 对外接口 - Elasticsearch 日志文件的持久化存储和检索 - MongoDB 只是存储一些 Graylog 的配置 ## 安装 > 可以是裸机安装,也可以是docker安装,这里用docker安装 环境要求: - centos7.4 - cpu2个 内存2G 参考: https://hub.docker.com/r/graylog2/graylog/ ### 环境准备 ``` mkdir /root/graylog && cd /root/graylog //挂载目录 mkdir -p mongo_data graylog_journal es_data //配置文件目录 mkdir -p ./graylog/config cd ./graylog/config wget https://raw.githubusercontent.com/Graylog2/graylog-docker/3.0/config/graylog.conf wget https://raw.githubusercontent.com/Graylog2/graylog-docker/3.0/config/log4j2.xml //提前准备镜像 docker pull mongo:3 docker pull graylog/graylog:3.0 docker pull elasticsearch:5.6.9 ``` ### docker-compose.yml ``` version: '2' services: # MongoDB: https://hub.docker.com/_/mongo/ mongo: image: mongo:3 volumes: - ./mongo_data:/data/db - /etc/localtime:/etc/localtime # Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/docker.html elasticsearch: image: elasticsearch:5.6.9 volumes: - ./es_data:/usr/share/elasticsearch/data - /etc/localtime:/etc/localtime environment: - http.host=0.0.0.0 - transport.host=localhost - network.host=0.0.0.0 # Disable X-Pack security: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/security-settings.html#general-security-settings - xpack.security.enabled=false - "ES_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 mem_limit: 1g # Graylog: https://hub.docker.com/r/graylog/graylog/ graylog: image: graylog/graylog:3.0 volumes: - ./graylog_journal:/usr/share/graylog/data/journal - ./graylog/config:/usr/share/graylog/data/config - /etc/localtime:/etc/localtime environment: # CHANGE ME! - GRAYLOG_PASSWORD_SECRET=somepassword
除了使用inotify监控/etc/passwd文件外,也可以利用/var/log/auth.log日志文件来实时获取新增用户。auth.log文件记录了系统的用户认证和授权信息,包括用户登录、密码修改、sudo使用等等。 具体实现步骤如下: 1. 使用系统函数open()打开/var/log/auth.log日志文件,并使用lseek()将文件指针定位到文件末尾。 2. 使用系统函数read()读取文件内容,并处理新增用户的日志信息。 3. 在处理日志信息时,可以使用正则表达式匹配新增用户的信息,从而得知新增用户的信息。 下面是一个简单的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <regex.h> #define BUF_SIZE 1024 int main(int argc, char **argv) { int fd, n; char buf[BUF_SIZE]; off_t off; regex_t reg; regmatch_t match[2]; fd = open("/var/log/auth.log", O_RDONLY); if (fd < 0) { perror("open"); exit(EXIT_FAILURE); } off = lseek(fd, 0, SEEK_END); while (1) { n = read(fd, buf, BUF_SIZE); if (n < 0) { perror("read"); exit(EXIT_FAILURE); } if (n == 0) { usleep(1000000); // wait for new data continue; } if (regcomp(&reg, "useradd: new user: name=([^,]+)", REG_EXTENDED) != 0) { perror("regcomp"); exit(EXIT_FAILURE); } if (regexec(&reg, buf, 2, match, 0) == 0) { char *name = strndup(&buf[match[1].rm_so], match[1].rm_eo - match[1].rm_so); printf("New user added: %s\n", name); free(name); } regfree(&reg); off += n; lseek(fd, off, SEEK_SET); } close(fd); return 0; } ``` 这段代码可以实时监控/var/log/auth.log日志文件的新增用户信息,并输出新增用户的用户名。需要注意的是,我们使用正则表达式匹配日志信息时,需要使用regcomp()函数编译正则表达式,并使用regexec()函数执行正则表达式匹配。同时,为了避免重复输出新增用户信息,我们在每次读取日志文件内容时,将文件指针定位到文件末尾,从而避免重复处理已经处理过的日志信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值