This file is indexed.

/usr/share/perl5/Net/Nessus/XMLRPC.pm is in libnet-nessus-xmlrpc-perl 0.30-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
package Net::Nessus::XMLRPC;

use XML::Simple;
use LWP::UserAgent;
use HTTP::Request::Common;

use warnings;
use strict;

=head1 NAME

Net::Nessus::XMLRPC - Communicate with Nessus scanner(v4.2+) via XMLRPC

=head1 VERSION

Version 0.30

=cut

our $VERSION = '0.30';


=head1 SYNOPSIS

This is Perl interface for communication with Nessus scanner over XMLRPC.
You can start, stop, pause and resume scan. Watch progress and status of 
scan, download report, etc.

	use Net::Nessus::XMLRPC;

	# '' is same as https://localhost:8834/
	my $n = Net::Nessus::XMLRPC->new ('','user','pass');

	die "Cannot login to: ".$n->nurl."\n" unless ($n->logged_in);

	print "Logged in\n";
	my $polid=$n->policy_get_first;
	print "Using policy ID: $polid ";
	my $polname=$n->policy_get_name($polid);
	print "with name: $polname\n";
	my $scanid=$n->scan_new($polid,"perl-test","127.0.0.1");

	while (not $n->scan_finished($scanid)) {
		print "$scanid: ".$n->scan_status($scanid)."\n";	
		sleep 15;
	}
	print "$scanid: ".$n->scan_status($scanid)."\n";	
	my $reportcont=$n->report_file_download($scanid);
	my $reportfile="report.xml";
	open (FILE,">$reportfile") or die "Cannot open file $reportfile: $!";
	print FILE $reportcont;
	close (FILE);

=head1 NOTICE

This CPAN module uses LWP for communicating with Nessus over XMLRPC via https.
Therefore, make sure that you have Net::SSL (provided by Crypt::SSLeay):
http://search.cpan.org/perldoc?Crypt::SSLeay
or IO::Socket::SSL:
http://search.cpan.org/perldoc?IO::Socket::SSL

If you think you have login problems, check this first!

=head1 METHODS

=head2 new ([$nessus_url], [$user], [$pass])

creates new object Net::Nessus::XMLRPC
=cut
sub new {
	my $class = shift;

	my $self; 

	$self->{_nurl} = shift;
	if ($self->{_nurl} eq '') {
		$self->{_nurl}='https://localhost:8834/';
	} elsif (substr($self->{_nurl},-1,1) ne '/') {
		$self->{_nurl}= $self->{_nurl}.'/';	
	} 
	my $user = shift;
	my $password = shift;
	$self->{_token} = undef;
	$self->{_name} = undef;
	$self->{_admin} = undef;
	$self->{_ua} = LWP::UserAgent->new;
	bless $self, $class;
	if ($user and $password) {
		$self->login($user,$password);
	}
	return $self;
}

=head2 DESTROY 

destructor, calls logout method on destruction
=cut
sub DESTROY {
	my ($self) = @_;
	$self->logout();
}

=head2 nurl ( [$nessus_url] )

get/set Nessus base URL
=cut
sub nurl {
	my ( $self, $nurl ) = @_;
	$self->{_nurl} = $nurl if defined($nurl);
	return ( $self->{_nurl} );
}

=head2 token ( [$nessus_token] )

get/set Nessus login token
=cut
sub token {
	my ( $self, $token ) = @_;
	$self->{_token} = $token if defined($token);
	return ( $self->{_token} );
}

=head2 nessus_http_request ( $uri, $post_data )

low-level function, makes HTTP request to Nessus URL	
=cut
sub nessus_http_request {
	my ( $self, $uri, $post_data ) = @_;
	my $ua = $self->{_ua};
	# my $ua = LWP::UserAgent->new;
	my $furl = $self->nurl.$uri;
	my $r = POST $furl, $post_data;
	my $result = $ua->request($r);
	# my $filename="n-".time; open (FILE,">$filename"); 
	# print FILE $result->as_string; close (FILE);
	if ($result->is_success) {
		return $result->content;
	} else {
		return '';
	}
}

=head2 nessus_request ($uri, $post_data) 

low-level function, makes XMLRPC request to Nessus URL and returns XML
=cut
sub nessus_request {
	my ( $self, $uri, $post_data ) = @_;
	my $cont=$self->nessus_http_request($uri,$post_data);
	if ($cont eq '') {
		return ''	
	}
	my $xmls;
	eval {
	$xmls=XMLin($cont, ForceArray => 1, KeyAttr => '', SuppressEmpty => '' );
	} or return '';
	if ($xmls->{'status'}->[0] eq "OK") {
		return $xmls; 
	} else { 
		return ''
	}
}

=head2 login ( $user, $password )

login to Nessus server via $user and $password	
=cut
sub login {
	my ( $self, $user, $password ) = @_;

	my $post=[ login => $user, password => $password ];
	my $xmls = $self->nessus_request("login",$post);

	if ($xmls eq '' or not defined($xmls->{'contents'}->[0]->{'token'}->[0])) {
		$self->token('');
	} else {
		$self->token ($xmls->{'contents'}->[0]->{'token'}->[0]);
	}
	return $self->token;
}

=head2 logout 

logout from Nessus server
=cut
sub logout {
	my ($self) = @_;
	my $post=[ "token" => $self->token ];
	my $xmls = $self->nessus_request("logout",$post);
	$self->token('');
}

=head2 logged_in

returns true if we're logged in
=cut
sub logged_in {
	my ($self) = @_;
	return $self->token;
}

=head2 scan_new ( $policy_id, $scan_name, $targets )

initiates new scan 
=cut
sub scan_new {
	my ( $self, $policy_id, $scan_name, $target ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"policy_id" => $policy_id,
		"scan_name" => $scan_name,
		"target" => $target
		 ];

	my $xmls = $self->nessus_request("scan/new",$post);
	if ($xmls) {
		return ($xmls->{'contents'}->[0]->{'scan'}->[0]->{'uuid'}->[0]);
	} else {
		return $xmls
	}
}	

=head2 scan_new_file ( $policy_id, $scan_name, $targets, $filename )

initiates new scan with hosts from file named $filename
=cut
sub scan_new_file {
	my ( $self, $policy_id, $scan_name, $target, $filename ) = @_;

	my $post={ 
		"token" => $self->token, 
		"policy_id" => $policy_id,
		"scan_name" => $scan_name,
		"target" => $target
		 };
	$post->{"target_file_name"} = $self->file_upload($filename);
	my $xmls = $self->nessus_request("scan/new",$post);
	if ($xmls) {
		return ($xmls->{'contents'}->[0]->{'scan'}->[0]->{'uuid'}->[0]);
	} else {
		return $xmls
	}
}	

=head2 scan_stop ( $scan_id )

stops the scan identified by $scan_id
=cut
sub scan_stop {
	my ( $self, $scan_uuid ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"scan_uuid" => $scan_uuid,
		 ];

	my $xmls = $self->nessus_request("scan/stop",$post);
	return $xmls;
}

=head2 scan_stop_all 

stops all scans
=cut
sub scan_stop_all {
	my ( $self ) = @_;

	my $list = $self->scan_list_uids;

	foreach my $uuid (@$list) {
		$self->scan_stop($uuid);
	}
	return $list;
}

=head2 scan_pause ( $scan_id )

pauses the scan identified by $scan_id
=cut
sub scan_pause {
	my ( $self, $scan_uuid ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"scan_uuid" => $scan_uuid,
		 ];

	my $xmls = $self->nessus_request("scan/pause",$post);
	return $xmls;
}

=head2 scan_pause_all 

pauses all scans
=cut
sub scan_pause_all {
	my ( $self ) = @_;

	my $list = $self->scan_list_uids;

	foreach my $uuid (@$list) {
		$self->scan_pause($uuid);
	}
	return $list;
}

=head2 scan_resume ( $scan_id )

resumes the scan identified by $scan_id
=cut
sub scan_resume {
	my ( $self, $scan_uuid ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"scan_uuid" => $scan_uuid,
		 ];

	my $xmls = $self->nessus_request("scan/resume",$post);
	return $xmls;
}

=head2 scan_resume_all 

resumes all scans
=cut
sub scan_resume_all {
	my ( $self ) = @_;

	my $list = $self->scan_list_uids;

	foreach my $uuid (@$list) {
		$self->scan_resume($uuid);
	}
	return $list;
}

=head2 scan_list_uids 

returns array of IDs of (active) scans
=cut
sub scan_list_uids {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token
	];

	my $xmls = $self->nessus_request("scan/list",$post);
	my @list;
	if ($xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}) {
	foreach my $scan (@{$xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}}) {
		push @list, $scan->{'uuid'}->[0];
	} # foreach
	return \@list;
	} # if
}

=head2 scan_get_name ( $uuid ) 

returns name of the scan identified by $uuid 
=cut
sub scan_get_name {
	my ( $self, $uuid ) = @_;

	my $post=[ 
		"token" => $self->token
	];

	my $xmls = $self->nessus_request("scan/list",$post);
	if ($xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}) {
	foreach my $scan (@{$xmls->{'contents'}->[0]->{'scans'}->[0]->{'scanList'}->[0]->{'scan'}}) {
		if ($scan->{'uuid'}->[0] eq $uuid) {
			return $scan->{'readableName'}->[0];
		}
	} # foreach
	} # if
}

=head2 scan_status ( $uuid ) 

returns status of the scan identified by $uuid 
=cut
sub scan_status {
	my ( $self, $uuid ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"report" => $uuid,
		 ];

	my $xmls = $self->nessus_request("report/list",$post);
	if ($xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}}) {
		if ($report->{'name'}->[0] eq $uuid) {
			return $report->{'status'}->[0];
		}
	} # foreach
	} # if
	return ''; # nothing found
}

=head2 scan_finished ( $uuid ) 

returns true if scan is finished/completed (identified by $uuid)
=cut
sub scan_finished {
	my ( $self, $uuid ) = @_;
	my $status = $self->scan_status($uuid);
	if ( $status eq "completed" ) {
		return $status;
	} else {
		return '';
	}
}	

=head2 nessus_http_upload_request ( $uri, $post_data )

low-level function, makes HTTP upload request to URI specified
=cut
sub nessus_http_upload_request {
	my ( $self, $uri, $post_data ) = @_;
	my $ua = $self->{_ua};
	# my $ua = LWP::UserAgent->new;
	my $furl = $self->nurl.$uri;
	my $r = POST $furl, Content_Type => 'form-data', Content => $post_data;
	my $result = $ua->request($r);
	# my $filename="u-".time; open (FILE,">$filename"); 
	# print FILE $result->as_string; close (FILE);
	if ($result->is_success) {
		return $result->content;
	} else {
		return '';
	}
}

=head2 file_upload ( $filename )

uploads $filename to nessus server, returns filename of file uploaded
or '' if failed

Note that uploaded file is per session (i.e. it will be there until logout/attack.) 
So, don't logout or login again and use the filename! You need to upload it 
again!
=cut
sub file_upload {
	my ( $self, $filename ) = @_;
	my $post=[ "token" => $self->token, Filedata => [ $filename] ];
	my $cont=$self->nessus_http_upload_request("file/upload",$post);
	if ($cont eq '') {
		return ''	
	}
	my $xmls;
	eval {
	$xmls=XMLin($cont, ForceArray => 1, KeyAttr => '', SuppressEmpty => '');
	} or return '';
	if ($xmls->{'status'}->[0] eq "OK") {
		return $xmls->{'contents'}->[0]->{'fileUploaded'}->[0]; 
	} else { 
		return ''
	}
}

=head2 upload ( $filename, $content )

uploads $filename to nessus server using $content as content of file, returns filename of file uploaded
or '' if failed

Note that uploaded file is per session (i.e. it will be there until logout/attack.) 
So, don't logout or login again and use the filename! You need to upload it 
again!
=cut
sub upload {
	my ( $self, $filename, $content ) = @_;
	# Content => [ $PARAM => [undef,$FILENAME, Content => $CONTENTS ] ]);
	my $post=[ "token" => $self->token, Filedata => [ undef, $filename, Content => $content] ];
	my $cont=$self->nessus_http_upload_request("file/upload",$post);
	if ($cont eq '') {
		return ''	
	}
	my $xmls;
	eval {
	$xmls=XMLin($cont, ForceArray => 1, KeyAttr => '', SuppressEmpty => '');
	} or return '';
	if ($xmls->{'status'}->[0] eq "OK") {
		return $xmls->{'contents'}->[0]->{'fileUploaded'}->[0]; 
	} else { 
		return ''
	}
}

=head2 policy_get_first

returns policy id for the first policy found
=cut
sub policy_get_first {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];
	
	my $xmls = $self->nessus_request("policy/list",$post);
	if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		return $report->{'policyID'}->[0];
	} # foreach
	} # if
	return '';
}

=head2 policy_get_firsth

returns ref to hash %value with basic info of first policy/scan 
returned by the server

$value{'id'}, $value{'name'}, $value{'owner'}, $value{'visibility'},
$value{'comment'}
=cut
sub policy_get_firsth {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];

	my %info;	
	my $xmls = $self->nessus_request("policy/list",$post);
	if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		$info{'id'} = $report->{'policyID'}->[0];
		$info{'name'} = $report->{'policyName'}->[0];
		$info{'owner'} = $report->{'policyOwner'}->[0];
		$info{'visibility'} = $report->{'visibility'}->[0];
		$info{'comment'} = $report->{'policyContents'}->[0]->{'policyComments'}->[0];
		return \%info;
	} # foreach
	} # if
	return \%info;
}

=head2 policy_list_hash

returns ref to array of hashes %value with basic info of first policy/scan 
returned by the server

$value{'id'}, $value{'name'}, $value{'owner'}, $value{'visibility'},
$value{'comment'}
=cut
sub policy_list_hash {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];

	my @list;
	my $xmls = $self->nessus_request("policy/list",$post);
	if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		my %info;	
		$info{'id'} = $report->{'policyID'}->[0];
		$info{'name'} = $report->{'policyName'}->[0];
		$info{'owner'} = $report->{'policyOwner'}->[0];
		$info{'visibility'} = $report->{'visibility'}->[0];
		$info{'comment'} = $report->{'policyContents'}->[0]->{'policyComments'}->[0];
		push @list, \%info;
	} # foreach
	} # if
	return \@list;
}

=head2 policy_list_uids 

returns ref to array of IDs of policies available
=cut
sub policy_list_uids {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];

	my $xmls = $self->nessus_request("policy/list",$post);
	my @list;
	if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		push @list,$report->{'policyID'}->[0];
	} # foreach
	return \@list;
	} # if
	return '';
}

=head2 policy_list_names 

returns ref to array of names of policies available
=cut
sub policy_list_names {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];

	my $xmls = $self->nessus_request("policy/list",$post);
	my @list;
	if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		push @list,$report->{'policyName'}->[0];
	} # foreach
	return \@list;
	} # if
	return '';
}

=head2 policy_get_info ( $policy_id ) 

returns ref to hash %value with basic info of policy/scan identified by $policy_id 

$value{'id'}, $value{'name'}, $value{'owner'}, $value{'visibility'},
$value{'comment'}
=cut
sub policy_get_info {
	my ( $self, $policy_id ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];
	my %info;
	my $xmls = $self->nessus_request("policy/list",$post);
	if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
	if ($report->{'policyID'}->[0] eq $policy_id) {
		$info{'id'} = $report->{'policyID'}->[0];
		$info{'name'} = $report->{'policyName'}->[0];
		$info{'owner'} = $report->{'policyOwner'}->[0];
		$info{'visibility'} = $report->{'visibility'}->[0];
		$info{'comment'} = $report->{'policyContents'}->[0]->{'policyComments'}->[0];
		return \%info;
	}
	} # foreach
	} # if
	return \%info;
}

=head2 policy_get_id ( $policy_name ) 

returns ID of the scan/policy identified by $policy_name 
=cut
sub policy_get_id {
	my ( $self, $policy_name ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];
	 my $xmls = $self->nessus_request("policy/list",$post);
	 if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	 foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		if ($report->{'policyName'}->[0] eq $policy_name) {
			return $report->{'policyID'}->[0];
		}
	 } # foreach
	 } # if
	 return '';
}

=head2 policy_get_name ( $policy_id ) 

returns name of the scan/policy identified by $policy_id 
=cut
sub policy_get_name {
	my ( $self, $policy_id ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];
	 my $xmls = $self->nessus_request("policy/list",$post);
	 if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
	 foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		if ($report->{'policyID'}->[0] eq $policy_id) {
			return $report->{'policyName'}->[0];
		}
	 } # foreach
	 } # if
	 return '';
}

=head2 policy_delete ( $policy_id )

delete policy identified by $policy_id
=cut
sub policy_delete {
	my ( $self, $policy_id ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"policy_id" => $policy_id,
		 ];

	my $xmls = $self->nessus_request("policy/delete",$post);
	return $xmls;
}

=head2 policy_copy ( $policy_id )

copy policy identified by $policy_id, returns $policy_id of new copied policy
=cut
sub policy_copy {
	my ( $self, $policy_id ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"policy_id" => $policy_id,
		 ];

	my $xmls = $self->nessus_request("policy/copy",$post);
	if ($xmls->{'contents'}->[0]->{'policy'}->[0]) {
		return $xmls->{'contents'}->[0]->{'policy'}->[0]->{'policyID'}->[0];
	} # if
	return '';
}

=head2 policy_rename ( $policy_id, $policy_name )

rename policy to $policy_name identified by $policy_id
=cut
sub policy_rename {
	my ( $self, $policy_id, $policy_name ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"policy_id" => $policy_id,
		"policy_name" => $policy_name
		 ];

	my $xmls = $self->nessus_request("policy/rename",$post);
	return $xmls;
}

=head2 policy_edit ( $policy_id, $params )

edit policy identified by $policy_id

%params (must be present): 
policy_name => name
policy_shared => 1

%params can be (examples)
max_hosts => 50,
max_checks=> 10,
use_mac_addr => no,
throttle_scan => yes,
optimize_test => yes,
log_whole_attack => no,
ssl_cipher_list => strong,
save_knowledge_base => no,
port_range => 1-65535
=cut
sub policy_edit {
	my ( $self, $policy_id, $params ) = @_;

	my $post={ 
		"token" => $self->token, 
		"policy_id" => $policy_id
		 };
	while (my ($key, $value) = each(%{$params}))
	{
		$post->{$key} = $value;
	}

	my $xmls = $self->nessus_request("policy/add",$post);
	return $xmls;
}

=head2 policy_new ( $params )

create new policy with $params, 
%params must be present:
policy_name
policy_shared

the others parameters are same as policy_edit
=cut
sub policy_new {
	my ( $self, $params ) = @_;

	my $xmls = $self->policy_edit(0, %{$params});
	return $xmls;
}

=head2 policy_get_opts ( $policy_id ) 

returns hashref with different options for policy identified by $policy_id 
=cut
sub policy_get_opts {
	my ( $self, $policy_id ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];
	my $xmls = $self->nessus_request("policy/list",$post);

	if ($xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}) {
		my %opts;
		foreach my $report (@{$xmls->{'contents'}->[0]->{'policies'}->[0]->{'policy'}}) {
		if ($report->{'policyID'}->[0] eq $policy_id) {
			$opts{'policy_name'}=$report->{'policyName'}->[0];
			if ($report->{'visibility'}->[0] eq "shared") {
				$opts{'policy_shared'}=1;
			} else {
				$opts{'policy_shared'}=0;
			}
			if ($report->{'policyContents'}->[0]->{'policyComments'}->[0]) {
				$opts{'policy_comments'}=$report->{'policyContents'}->[0]->{'policyComments'}->[0];
			}
			foreach my $prefs (@{$report->{'policyContents'}->[0]->{'Preferences'}->[0]->{'ServerPreferences'}->[0]->{'preference'}}) {
				$opts{$prefs->{'name'}->[0]} = $prefs->{'value'}->[0] if ($prefs->{'name'}->[0]);
			}
			foreach my $prefp (@{$report->{'policyContents'}->[0]->{'Preferences'}->[0]->{'PluginsPreferences'}->[0]->{'item'}}) {
				$opts{$prefp->{'fullName'}->[0]} = $prefp->{'selectedValue'}->[0] if ($prefp->{'fullName'}->[0]);
			}
			foreach my $plugf (@{$report->{'policyContents'}->[0]->{'FamilySelection'}->[0]->{'FamilyItem'}}) {
				$opts{"plugin_selection.family.".$plugf->{'FamilyName'}->[0]} = $plugf->{'Status'}->[0] if ($plugf->{'FamilyName'}->[0]);
			}
			foreach my $plugi (@{$report->{'policyContents'}->[0]->{'IndividualPluginSelection'}->[0]->{'PluginItem'}}) {
				$opts{"plugin_selection.individual_plugin.".$plugi->{'PluginId'}->[0]} = $plugi->{'Status'}->[0] if ($plugi->{'PluginId'}->[0]);
			}
			return \%opts;
		}
	 } # foreach
	 } # if
	 return '';
}

=head2 policy_set_opts ( $policy_id , $params ) 

sets policy options via hashref $params identified by $policy_id 
=cut
sub policy_set_opts {
	my ( $self, $policy_id, $params ) = @_;

	my $post = $self->policy_get_opts ($policy_id);
	while (my ($key, $value) = each(%{$params}))
	{
		$post->{$key} = $value;
	}
	$post->{"token"} = $self->token;
	$post->{"policy_id"} = $policy_id;

	my $xmls = $self->nessus_request("policy/edit",$post);
	return $xmls;
}

=head2 report_list_uids 

returns ref to array of IDs of reports available
=cut
sub report_list_uids {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token 
		 ];

	my $xmls = $self->nessus_request("report/list",$post);
	my @list;
	if ($xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}}) {
		push @list, $report->{'name'}->[0];
	}
	}

	return \@list;
}

=head2 report_list_hash 

returns ref to array of hashes with basic info of reports 
hash has following keys:
name
status
readableName
timestamp
=cut
sub report_list_hash {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token 
		 ];

	my $xmls = $self->nessus_request("report/list",$post);
	my @list;
	if ($xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}) {
	foreach my $report (@{$xmls->{'contents'}->[0]->{'reports'}->[0]->{'report'}}) {	
		my %r;
		$r{'name'} = $report->{'name'}->[0];
		$r{'status'} = $report->{'status'}->[0];
		$r{'readableName'} = $report->{'readableName'}->[0];
		$r{'timestamp'} = $report->{'timestamp'}->[0];
		
		push @list, \%r;
	}
	}

	return \@list;
}

=head2 report_file_download ($report_id)

returns XML report identified by $report_id (Nessus XML v2)
=cut
sub report_file_download {
	my ( $self, $uuid ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"report" => $uuid,
		 ];

	my $file = $self->nessus_http_request("file/report/download", $post);
	return $file;
}	

=head2 report_file1_download ($report_id)

returns XML report identified by $report_id (Nessus XML v1)
=cut
sub report_file1_download {
	my ( $self, $uuid ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"report" => $uuid,
		"v1" => "true",
		 ];

	my $file = $self->nessus_http_request("file/report/download", $post);
	return $file;
}	

=head2 report_delete ($report_id)

delete report identified by $report_id
=cut
sub report_delete {
	my ( $self, $uuid ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"report" => $uuid,
		 ];

	my $xmls = $self->nessus_request("report/delete", $post);
	return $xmls;
}	

=head2 report_import ( $filename )

tells nessus server to import already uploaded file named $filename
( i.e. you already uploaded the file via file_upload() )
=cut
sub report_import {
	my ( $self, $filename ) = @_;
	my $post={ "token" => $self->token, "file" => $filename };
	my $xmls = $self->nessus_request("file/report/import",$post);
	return $xmls;
}

=head2 report_import_file ( $filename )

uploads $filename to nessus server and imports it as nessus report
=cut
sub report_import_file {
	my ( $self, $filename ) = @_;
	my $post={ "token" => $self->token};
	$post->{"file"} = $self->file_upload($filename);
	my $xmls = $self->nessus_request("file/report/import",$post);
	return $xmls;
}

=head2 users_list 

returns ref to array of hash %values with users info 
$values{'name'}
$values{'admin'}
$values{'lastlogin'}
=cut
sub users_list {
	my ( $self ) = @_;

	my $post=[ 
		"token" => $self->token, 
		 ];
	my @users;
	my $xmls = $self->nessus_request("users/list",$post);
	if ($xmls->{'contents'}->[0]->{'users'}->[0]->{'user'}) {
	foreach my $user (@{$xmls->{'contents'}->[0]->{'users'}->[0]->{'user'}}) {
		my %info;
		$info{'name'} = $user->{'name'}->[0];
		$info{'admin'} = $user->{'admin'}->[0];
		$info{'lastlogin'} = $user->{'lastlogin'}->[0];
		push @users, \%info

	} # foreach
	} # if
	return \@users;
}

=head2 users_delete ( $login ) 

deletes user with $login

=cut
sub users_delete {
	my ( $self, $login ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"login" => $login
		 ];
	my $xmls = $self->nessus_request("users/delete",$post);
	my $user = '';
	if ($xmls->{'contents'}->[0]->{'user'}->[0]->{'name'}->[0]) {
		$user = $xmls->{'contents'}->[0]->{'user'}->[0]->{'name'}->[0];
	}
	return $user;
}

=head2 users_add ( $login, $password )

deletes user with $login and $password, return username created, '' if not

=cut
sub users_add {
	my ( $self, $login, $password ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"login" => $login,
		"password" => $password
		 ];
	my $xmls = $self->nessus_request("users/add",$post);
	my $user = '';
	if ($xmls->{'contents'}->[0]->{'user'}->[0]->{'name'}->[0]) {
		$user = $xmls->{'contents'}->[0]->{'user'}->[0]->{'name'}->[0];
	}
	return $user;
}

=head2 users_passwd ( $login, $password )

change user password to $password identified with $login, return username, '' if not

=cut
sub users_passwd {
	my ( $self, $login, $password ) = @_;

	my $post=[ 
		"token" => $self->token, 
		"login" => $login,
		"password" => $password
		 ];
	my $xmls = $self->nessus_request("users/chpasswd",$post);
	my $user = '';
	if ($xmls->{'contents'}->[0]->{'user'}->[0]->{'name'}->[0]) {
		$user = $xmls->{'contents'}->[0]->{'user'}->[0]->{'name'}->[0];
	}
	return $user;
}

=head1 AUTHOR

Vlatko Kosturjak, C<< <kost at linux.hr> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-net-nessus-xmlrpc at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Nessus-XMLRPC>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Net::Nessus::XMLRPC


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Nessus-XMLRPC>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Net-Nessus-XMLRPC>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Net-Nessus-XMLRPC>

=item * Search CPAN

L<http://search.cpan.org/dist/Net-Nessus-XMLRPC>

=back


=head1 REPOSITORY

Repository is available on GitHub: http://github.com/kost/nessus-xmlrpc-perl

=head1 ACKNOWLEDGEMENTS

I have made Ruby library as well: http://nessus-xmlrpc.rubyforge.org/

There you can find some early documentation about XMLRPC protocol used.

=head1 COPYRIGHT & LICENSE

Copyright 2010 Vlatko Kosturjak, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


=cut

1; # End of Net::Nessus::XMLRPC