发信人: tcpip (俺的昵称改了), 信区: cnunix
标  题: [转载] 关于/var/adm/utmp(x),wtmp(x),lastlog (一)
发信站: 哈工大紫丁香 (Sun Sep 26 14:55:10 1999), 转信

发信人: cpu (奔腾的心), 信区: Solaris
发信站: 华南网木棉站 (Thu Jun 25 14:24:39 1998), 转信

【 以下文字转载自 Hacker 讨论区 】
【 原文由 cpu 所发表 】
许多人都知道unix的/var/adm/utmp,wtmp,utmpx,wtmpx,lastlog是系统用户的
登录记帐文件,但是不知道具体是干什么的,最近研究了一下,把所知道的与
大家共享。(注:以下所述都适用sunos,其它估计也差不多)
首先是/var/adm/lastlog。lastlog中给每个用户一条记录,按照用户的uid排
序,比如root,uid是0,所以是第0条记录,如果某人的uid是60000,那么他
对应的lastlog就是第60000条,所以用户多的机器里面lastlog文件都相当大。
lastlog的记录结构如下(/usr/include/lastlog.h):

struct lastlog {
        time_t  ll_time;        /* last login time */
        char    ll_line[8];     /* last login line or terminal */
        char    ll_host[16];    /* last login host */
};

一般用户登录到系统后总会显示上次登录的时间、终端和主机或IP地址,就是
从lastlog文件取出来的,所以要是你偷用了其他人的帐号又想不留痕迹,最
好想办法改了lastlog的相关记录块 ;)

以下是一个简单的lastlog查看程序:

# include       <stdio.h>
# include       <stdlib.h>
# include       <unistd.h>
# include       <sys/types.h>
# include       <time.h>
# include       <pwd.h>
# include       <lastlog.h>

main(int argc, char **argv)
{
        int     fd;
        uid_t   uid;
        struct  passwd  *p_passwd;
        struct  lastlog lastlogent;
        char    line[12];
        char    host[24];

        if (argc < 2) {
                printf("usage: lastlog user\n");
                return;
        }
        if ((p_passwd = getpwnam(argv[1])) == NULL) {
                fprintf(stderr,
                "%s doesn't exit in passwd file\n", argv[1]);
                exit(-1);
        }
        uid = p_passwd->pw_uid;
        if ((fd = open("/var/adm/lastlog", O_RDONLY)) < 0) {
                perror("open");
                exit(-1);
        }
        printf("uid is %d, ", uid);
        if (lseek(fd, uid * sizeof(struct lastlog), SEEK_SET) < 0) {
                perror("lseek");
                close(fd);
                exit(-1);
        }
        if (read(fd, &lastlogent, sizeof(struct lastlog)) < 0) {
                perror("read");
                close(fd);
                exit(-1);
        }
        printf("lastlog entry for %s read OK\n", argv[1]);
        strncpy(line, lastlogent.ll_line, 8);
        line[8] = '\0';
        strncpy(host, lastlogent.ll_host, 16);
        host[16] = '\0';
        printf("last login time: %s", ctime(&lastlogent.ll_time));
        printf("login line: %s\n", line);
        printf("login host: %s\n", host);
        close(fd);

--

        ******************************************************
                
                蓦然回首,老子已是高级战友 。。。  。。。

        ******************************************************

※ 修改:.trueip 于 Sep 26 14:58:54 修改本文.[FROM: dns.mtlab.hit.ed]
※ 来源:.华南网木棉站 bbs.gznet.edu.cn.[FROM: 202.101.248.6]
--
--
※ 转寄:.华南网木棉站 bbs.gznet.edu.cn.[FROM: dns.mtlab.hit.ed]

--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: trueip.bbs@melon.gzn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.546毫秒