#!/usr/bin/perl
package RvglobalsoftManager::Rvlibary;
##LICENSE##
use strict;
use Carp;
use warnings;
use Cwd;
use Cwd 'realpath';
use File::Basename qw(&basename &dirname);
use CGI ':standard';
use Data::Dumper;
use File::Basename;
use Socket;
use Net::SSLeay qw(get_https post_https sslcat make_headers make_form);
use lib dirname(realpath(__FILE__));
use lib dirname(realpath(__FILE__)).'/lib';
use lib dirname(realpath(__FILE__)).'/modules';
use LWP::Simple qw/get/;
use vars qw(@ISA @EXPORT_OK $CONF);
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
$OUTPUT $ERROR $CONF $VERSIONCACHE
);
sub parseFile {
my $file = $_[0];
my @aLines = ();
if( open(my $FILERead, '<', $file) ) {
@aLines = <$FILERead>;
close($FILERead);
for (my $i = 0; $i < @aLines; $i++) {
$aLines[$i] =~ s/\r|\n//g;
}
}
for (my $i = 0; $i < @aLines; $i++) {
$aLines[$i] =~ s/\r|\n//g;
}
return @aLines;
}
sub parseTextFile {
my ($file) = $_[0];
my @aLines = ();
if(open(my $R, '<', $file)) {
@aLines = <$R>;
close($R);
}
return @aLines;
}
sub function_exists {
no strict 'refs';
my $funcname = shift;
return \&{$funcname} if defined &{$funcname};
return;
}
sub getHomePath {
return (getpwuid $>)[7] if ( -e (getpwuid $>)[7] );
}
sub getLatestVersion {
my $product = $_[0];
return '-' if(!defined $product || $product eq '');
my $latestversion = '1.0.0';
$latestversion = getContentUrl('https://download.rvglobalsoft.com/latestversion.php?program=' . $product);
return $latestversion;
}
sub getContentUrl {
my $url = $_[0];
my $content = get $url;
#die "Couldn't get $url" unless defined $content;
#print Dumper($content);
return $content;
}
sub getDataFromURL {
my $host = shift;
my $uri = shift;
my $type = ($_[0]) ? $_[0] : 'GET';
my $port = ($_[1]) ? $_[1] : '80';
my $saveAs = ($_[2]) ? $_[2] : '';
my $TARPATH = '';
my $isSaveFile = 0;
$uri = '/'.$uri if ($uri !~ m|^/| && $type =~ m/GET/i);
#print "\$uri=$uri\n";
my $proto = getprotobyname('tcp');
my $ipaddr = inet_aton($host) || print STDERR "host($host) not found.\n";
my $sockaddr = pack_sockaddr_in($port, $ipaddr);
socket(SOCK, AF_INET, SOCK_STREAM, $proto) || print STDERR "socket error" . ": $!";
my $sin = sockaddr_in($port, $ipaddr) || print STDERR "sockaddr_in error" . ": $!";
connect(SOCK, $sin) || print STDERR "socket connect error" . ": $!";
if (uc($type) eq 'GET') {
send SOCK, "GET " . $uri . " HTTP/1.0\r\nHost: " . $host . "\r\n\r\n", 0;
} else {
my ($path, $query) = split(/\?/, $uri, 2);
send SOCK, "POST " . $path . " HTTP/1.0\r\n", 0;
send SOCK, "Host: " . $host . "\r\n", 0;
send SOCK, "Content-Type: application/x-www-form-urlencoded\r\n", 0;
send SOCK, "Content-Length: " . length($query) . "\r\n", 0;
send SOCK, "Connection: Keep-Alive\r\n", 0;
send SOCK, "Cache-Control: no-cache\r\n", 0;
send SOCK, "\r\n", 0;
send SOCK, $query, 0;
}
my $inheaders = 1;
my $result = '';
my $filePointer;
if (defined($saveAs) && $saveAs ne '') {
if (!-e $TARPATH) {
mkdir($TARPATH, 0755);
}
if (-e $saveAs) {
unlink($saveAs);
}
if (open($filePointer, '>', $saveAs)) {
$isSaveFile = 1;
}
}
while (my $temp = <SOCK>) {
if (!$inheaders) {
$result .= $temp;
if ($isSaveFile) {
print $filePointer $temp;
}
}
$temp =~s/\n|\r\n//g;
$temp =~s/\s$//g;
if ($temp=~/^$/ && $inheaders) { $inheaders = 0; }
}
close(SOCK);
if ($isSaveFile) {
close($filePointer);
}
return $result;
}
sub getCachePath {
return '/tmp';
}
sub callBackticks{
my $cmd = $_[0];
my $getCacheFile = getCachePath() . '/.rvsBackticks-' . localtime(time);
if (-w $getCacheFile) {
unlink($getCacheFile) if (-w $getCacheFile);
}
my ($TestBackticks) = `echo 'RV Test Backticks'`;
my ($skipBackticks) = 0;
if ($TestBackticks !~/RV Test Backticks/) {
$skipBackticks = 1;
}
if ($skipBackticks eq 1) {
system("$cmd > $getCacheFile 2>&1");
}
my $resuft = '';
if (-f $getCacheFile) {
my ($fd);
open($fd, '<', $getCacheFile);
$resuft = join('',<$fd>);
close($fd);
unlink($getCacheFile) if (-f $getCacheFile);
} else {
$resuft = `$cmd`;
}
return $resuft;
}
sub rvsLicenseEncode {
my @aAllIP = @_;
my $aRVCode = {
'ips' => serialize(\@aAllIP),
'virtualization' => 0,
'encrypt-type' => 'ioncube',
'license-code' => '',
};
$aRVCode = SerializerData($aRVCode);
$aRVCode =~ s/\r|\t|\n//gi;
return $aRVCode;
}
sub serialize {
my ($ref) = @_;
my $s;
if ( ref($ref) =~ /hash|array/i ) {
$s = serialize_sub( $ref, '', 1 );
}
else {
return serialize_value($ref);
}
return $s;
}
sub serialize_sub {
my ( $ref, $k, $no_key ) = @_;
my $s;
$s .= serialize_value($k) unless ($no_key);
if ($no_key) {
#print "\n\nno_key is set, ref=$ref, ". ref($ref) .", k=$k, s=$s\n\n";
}
if ( ref($ref) =~ /hash/i ) {
my $num = keys( %{$ref} );
$s .= "a:$num:{";
foreach my $k ( keys(%$ref) ) {
$s .= serialize_sub( $$ref{$k}, $k );
}
$s .= "}";
}
elsif ( ref($ref) =~ /array/i ) {
my $num = @{$ref};
$s .= "a:$num:{";
for ( my $k = 0 ; $k < @$ref ; $k++ ) {
$s .= serialize_sub( $$ref[$k], $k );
}
$s .= "}";
}
elsif ( !ref($ref) ) {
$s .= serialize_value($ref);
}
else {
die( "Cannot handle $ref = type (" . ref($ref) . ")" );
}
return $s;
}
sub serialize_value {
my ($value) = @_;
my $s;
if ( $value =~ /^\-?\d+$/ ) {
if ( abs($value) > 2**32 ) {
$s = "d:$value;";
}
else {
$s = "i:$value;";
}
}
elsif ( $value =~ /^\-?(\d+)\.(\d+)$/ ) {
$s = "d:$value;";
}
elsif ( $value eq "\0" ) {
$s = "N;";
}
else {
my $vlen = length($value);
$s = "s:$vlen:\"$value\";";
}
return $s;
}
sub getAllIP{
my $cmd_uname = rvsWhich('uname');
my $system = callBackticks2("$cmd_uname -s");
my @allIPList = ();
$system =~s/\n|\r//gi;
my $ips = '';
my @ListIPS = ();
if ( $system =~/freebsd/i) {
$ips = callBackticks2('/sbin/ifconfig -a');
} else {
$ips = callBackticks2("/sbin/ifconfig");
}
@ListIPS = split('\n', $ips);
foreach (@ListIPS) {
if ( /(\d*)\.(\d*)\.(\d*).(\d*)/) {
if ( $1 ne '127' ) {
push(@allIPList, $1 . '.' . $2 . '.' . $3 . '.' . $4);
}
}
}
return @allIPList;
}
sub rvsWhich {
my ($cmd) = $_[0];
return if ($cmd eq '');
return if ($cmd =~/\//);
my @binpathList = (
'/bin',
'/usr/bin',
'/usr/local/bin',
'/sbin',
'/usr/sbin',
'/usr/local/sbin'
);
my $whichCmd = '';
foreach my $path(@binpathList) {
if ( -x $path . '/' . 'which') {
$whichCmd = $path . '/' . 'which';
last;
}
}
if ($whichCmd eq '') {
print STDERR "which command is not support.\n";
return '';
}
my $binpath = '';
$binpath = callBackticks2("$whichCmd $cmd");
chomp ($binpath);
$binpath =~s/\n|\r//gi;
if ($binpath eq '') {
foreach my $path(@binpathList) {
if ( -x $path . '/' . $cmd) {
$binpath = $path . '/' . $cmd;
last;
}
}
}
if ($binpath eq '') {
print STDERR "$cmd command is not support.\n";
return '';
}
return $binpath;
}
sub callBackticks2{
my $cmd = join(' ', @_);
if (-f '/tmp/.rvsSkinBackticks') {
system('rm -f /tmp/.rvsSkinBackticks');
}
my ($TestBackticks) = `echo 'RV Test RVSkin Backticks'`;
my ($skipBackticks) = 0;
if ($TestBackticks !~/RV Test RVSkin Backticks/) {
$skipBackticks = 1;
}
if (-f '/tmp/.rvsSkinBackticks' || $skipBackticks eq 1) {
system("$cmd > /tmp/.rvsSkinBackticks 2>&1");
}
my ($resuft);
if (-f '/tmp/.rvsSkinBackticks') {
if (open(my $fd, '<', '/tmp/.rvsSkinBackticks')) {
$resuft = join('',<$fd>);
close($fd);
}
system('rm -f /tmp/.rvsSkinBackticks');
} else {
$resuft = `$cmd 2>&1`;
}
return $resuft;
}
sub SerializerData {
eval {
use MIME::Base64;
return encode_base64(serialize($_[0]));
};
}
sub getcontentbyurl {
my ($url,$method,$args) = @_;
my ($host,$port,$path) = extracturl($url);
my %OPT;
if ($url =~ '^https:\/\/') {
$OPT{'usessl'} = 1;
}
$OPT{'host'} = $host;
$OPT{'port'} = $port;
$OPT{'request'} = $path;
$OPT{'args'} = $args;
$OPT{'method'} = (defined $method && $method ne '')? $method : 'GET';
my %result = _socket(%OPT);
return %result;
}
sub extracturl {
my $url = $_[0];
my ($host,$port,$path);
$url =~s/http:\/\///;
$url =~s/https:\/\///;
$host = $url;
if ($url =~ /\//) {
($host,$path)=split(/\//,$url,2);
$path = '/' . $path;
$url =~s/\/.*//;
}
if ($url =~ /:/) {
($host,$port) = split(/:/,$host,2);
}
return ($host,$port,$path);
}
sub _socket {
my %OPT = @_;
#prepare vars
my %result;
my ($host,$port,$request,$args,$method,$usessl);
$host = defined $OPT{'host'} ? $OPT{'host'} : '';
$port = defined $OPT{'port'} ? $OPT{'port'} : '';
$request = defined $OPT{'request'} ? $OPT{'request'} : '/';
$args = defined $OPT{'args'} ? $OPT{'args'} : '';
$method = defined $OPT{'method'} ? $OPT{'method'} : 'GET';
$usessl = defined $OPT{'usessl'} ? $OPT{'usessl'} : 0;
my ($authBasic,$user,$password,$accountEncode);
$authBasic = defined $OPT{'authbasic'} ? $OPT{'authbasic'} : '0';
$user = defined $OPT{'user'} ? $OPT{'user'} : '';
$password = defined $OPT{'password'} ? $OPT{'password'} : '';
$accountEncode = '';
if ($authBasic) {
$password =~ s/[\r\n]+//g;
$accountEncode = "$user:$password";
$accountEncode = encode_base64_2("$user:$password");
}
if ($usessl) {
%result = _sslsocket($host,$port,$request,$args,$method,$accountEncode);
} else {
%result = _nonsslsocket($host,$port,$request,$args,$method,$authBasic,$accountEncode);
}
return %result;
}
sub _nonsslsocket {
my ($host,$port,$request,$args,$method,$authBasic,$accountEncode) = @_;
$port = ($port ne '') ? $port : '80';
my %result;
my ($sin,$ipaddr,$len,$header);
socket (SOCKET,PF_INET,SOCK_STREAM,0);
$ipaddr = gethostbyname($host);
if (!defined ($ipaddr)) {
$result{'socketfail'} = "Cannot get protocal by hostname $host";
return %result;
}
$sin = sockaddr_in($port,$ipaddr);
if (!connect (SOCKET,$sin)) {
$result{'socketfail'} = "Cannot create socket connection to server `$host`";
return %result;
}
if ($method eq 'GET') {
if ($args ne '') {
$request .= "?$args";
}
$args = '';
}
my $oldfh = select(SOCKET);
$| = 1;
select($oldfh);
my ($referer )="http://$host$request";
$len = length($args);
$header = "$method $request HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: $host\r\n";
$header .= "Content-Length: $len\r\n";
$header .= "Connection: Keep-Alive\r\n";
$header .= "Cache-Control: no-cache\r\n";
$header .= "Referer: $referer\r\n";
$header .= "\r\n";
if ($authBasic) {
$header .= "Authorization: Basic ".
$accountEncode."\r\n";
}
$header.= $args;
print SOCKET $header;
my ($inheaders) = 1;
my($chunk_size,$lefttoget,$thisread,$buf);
while (<SOCKET>) {
my $temp = $_;
if ($temp =~/^\n|^\r\n$/) {
$inheaders = 0;
}
chomp($temp);
if($inheaders) {
$temp =~s/\n//g;
$temp =~s/\s$//g;
my ($key,$value)=split(/: /,$temp);
chomp($key);
chomp($value);
if ($key =~/^http\//i) {
$result{'header'}{'httpresponse'} = $temp;
}
$result{'header'}{lc($key)} = $value;
} else {
$result{'page'} .= $temp;
}
}
close SOCKET;
return %result;
}
sub _sslsocket {
my ($host,$port,$request,$args,$method,$accountEncode) = @_;
my %result;
my $socketResult = '';
my $page = '';
my $httpresponse = '';
my %headers;
$port = ($port ne '') ? $port : '443';
my $perlcommand = rvsWhich('perl');
my $hasNetSSLeay = `$perlcommand -e 'eval { require Net::SSLeay; print 1; }' 2>&1`;
if ($hasNetSSLeay ne '1') {
$result{'socketfail'} = 'Not support Net::SSLeay';
return %result;
}
if ( $method && $method eq 'POST' ) {
$socketResult = `$perlcommand -e "
use Net::SSLeay;
print join\('{RVNL}',Net::SSLeay::post_https\( '$host', '$port', '$request',Net::SSLeay::make_headers\( 'Authorization' => 'Basic $accountEncode', 'Connection' => 'close' \),'$args'\)\);
" 2>&1`;
} else {
if ($args ne '') {
$request .= "?$args";
}
$socketResult = `$perlcommand -e "
use Net::SSLeay;
print join\('{RVNL}',Net::SSLeay::get_https\( '$host', '$port', '$request',Net::SSLeay::make_headers\( 'Authorization' => 'Basic $accountEncode', 'Connection' => 'close' \)\)\);
" 2>&1`;
}
($page, $httpresponse, %headers) = split('{RVNL}', $socketResult);
$result{'header'}{'httpresponse'} = $httpresponse;
foreach my $headkey(keys %headers) {
$result{'header'}{lc($headkey)} = $headers{$headkey};
}
$result{'page'} = $page;
return %result;
}
sub encode_base64_2 {
my $res = "";
my $eol = "\n";
pos($_[0]) = 0;
while ($_[0] =~ /(.{1,45})/gs) {
$res .= substr(pack("u", $1), 1);
chop($res);
}
$res =~ tr/` -_/AA-Za-z0-9+\//;
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/"=" x $padding/e if $padding;
if (length $eol) {
$res =~ s/(.{1,76})/$1$eol/g;
}
return $res;
}
sub UnSerializerData{
eval {
use MIME::Base64;
return %{unserialize(decode_base64($_[0]))};
};
}
sub unserialize {
my ($string) = @_;
if ( $string =~/^a:(\d+):(\{(.*)\})$/s ) {
#print "Unserializing complex array ($string)" if ($SERIALIZE_DBG);
my $keys = $1 * 2;
my @chars = split( //, $2 );
undef $string;
return unserialize_sub( {}, $keys, \@chars );
}
elsif ( $string =~/^s/ ) {
#print "Unserializing single string ($string)" if ($SERIALIZE_DBG);
$string =~/^s:(\d+):/;
return substr( $string, length($1) + 4, $1 );
}
elsif ( $string =~/^i|^d/ ) {
#print "Unserializing integer or double ($string)" if ($SERIALIZE_DBG);
return substr( $string, 2 ) + 0;
}
elsif ( $string =~/^N/i ) {
#print "Unserializing NULL value ($string)" if ($SERIALIZE_DBG);
return "\0";
}
else {
#print "Unserializing BAD DATA! ($string)" if ($SERIALIZE_DBG);
return '';
}
}
sub unserialize_sub {
my ( $hashref, $keys, $chars ) = @_;
my ( $temp, $keyname, $skip, $strlen );
my $mode = 'normal';
#print "unserialize: $hashref, $keys, $chars\n" if ($SERIALIZE_DBG);
while ( defined( my $c = shift @{$chars} ) ) {
#print "\t[$mode] = $c\n" if ($SERIALIZE_DBG);
if ( $mode eq 'string' ) {
$skip = 1;
if ( $c =~/\d+/ ) {
$strlen = $strlen . $c;
#print "string length = $strlen ($c)\n" if ($SERIALIZE_DBG);
}
if ( ( $strlen =~/\d+/ ) && ( $c eq ':' ) ) {
$mode = 'readstring';
}
}
elsif ( $mode eq 'readstring' ) {
next if ( $skip-- > 0 );
$mode = 'set', next if ( !$strlen-- );
$temp .= $c;
}
elsif ( $mode eq 'integer' ) {
next if ( $c eq ':' );
$mode = 'set', next if ( $c eq ';' );
if ( $c =~/\-|\d+/ ) {
if ( $c eq '-' ) {
$temp .= $c unless $temp;
}
else {
$temp .= $c;
}
}
}
elsif ( $mode eq 'double' ) {
next if ( $c eq ':' );
$mode = 'set', next if ( $c eq ';' );
if ( $c =~/\-|\d+|\./ ) {
if ( $c eq '-' ) {
$temp .= $c unless $temp;
}
else {
$temp .= $c;
}
}
}
elsif ( $mode eq 'null' ) {
$temp = "\0";
$mode = 'set', next;
}
elsif ( $mode eq 'array' ) {
if ( $c eq '{' ) {
$$hashref{$keyname} = unserialize_sub( $$hashref{$keyname}, ( $temp * 2 ), $chars );
undef $keyname;
$mode = 'normal';
}
elsif ( $c =~/\d+/ ) {
$temp = $temp . $c;
#print "array_length = $temp ($c)\n" if ($SERIALIZE_DBG);
}
}
elsif ( $mode eq 'set' ) {
if ( defined($keyname) ) {
#print "set VALUE=$temp\n" if ($SERIALIZE_DBG);
$$hashref{$keyname} = $temp;
undef $keyname;
}
else {
#print "set KEYNAME=$temp\n" if ($SERIALIZE_DBG);
$keyname = $temp;
}
$mode = 'normal';
}
if ( $mode eq 'normal' ) {
$strlen = $temp = '';
if ( !$keys ) {
#print "return, no more keys to process = ($keys)\n" if ($SERIALIZE_DBG);
return $hashref;
}
if ( $c eq 'i' ) {
$mode = 'integer';
$keys--;
}
if ( $c eq 'd' ) {
$mode = 'double';
$keys--;
}
if ( $c eq 'b' ) {
$mode = 'integer';
$keys--;
}
if ( $c eq 's' ) {
$mode = 'string';
$keys--;
}
if ( $c eq 'a' ) {
$mode = 'array';
$keys--;
}
if ( $c eq 'N' ) {
$mode = 'null';
$keys--;
}
}
}
#print "return normally (chars=" . ( join ',', @$chars ) . ")\n" if ($SERIALIZE_DBG);
return $hashref;
}
sub is_privateIP {
my @ip = @_;
foreach my $aip (@ip) {
my @ipsplit = split(/\./, $aip , 4);
#if ip prefix is not 10,172,192, this ip is not private ip
if ($ipsplit[0] !~/10|172|192/) {
return 0;
}
my $ipCompare = join('',@ipsplit);
if ($ipCompare >= 10000 && $ipCompare <= 10255255255) { #between 10.0.0.0 to 10.255.255.255
return 1;
}
if ($ipCompare >= 1721600 && $ipCompare <= 17231255255) { #between 172.16.0.0 to 172.31.255.255
return 1;
}
if ($ipCompare >= 19216800 && $ipCompare <= 192168255255) { #between 192.168.0.0 to 192.168.255.255
return 1;
}
}
return 0;
}
1;
Copyright 2K16 - 2K18 Indonesian Hacker Rulez