| 715 | | # XXX: Remove these, eventually. |
|---|
| 716 | | # $URL = HoneyClient::Manager::VM->init(); |
|---|
| 717 | | # print "VM Daemon Listening On: " . $URL . "\n"; |
|---|
| 718 | | # |
|---|
| 719 | | # $stubVM = getClientHandle(namespace => "HoneyClient::Manager::VM", |
|---|
| 720 | | # fault_handler => \&_handleFaultAndCleanup); |
|---|
| 721 | | |
|---|
| 722 | | # print "Calling setMasterVM()...\n"; |
|---|
| 723 | | # $som = $stubVM->setMasterVM(config => $args{'master_vm_config'}); |
|---|
| 724 | | # print "Result: " . $som->result() . "\n"; |
|---|
| 725 | | |
|---|
| 726 | | # print "Calling quickCloneVM()...\n"; |
|---|
| 727 | | # $som = $stubVM->quickCloneVM(); |
|---|
| 728 | | # print "Result: " . $som->result() . "\n"; |
|---|
| 729 | | # $vmCloneConfig = $som->result(); |
|---|
| 730 | | |
|---|
| 731 | | # # Make sure the VM is fully cloned, before trying to make any subsequent calls. |
|---|
| 732 | | # print "Calling isRegisteredVM()...\n"; |
|---|
| 733 | | # $som = $stubVM->isRegisteredVM(config => $vmCloneConfig); |
|---|
| 734 | | # $ret = $som->result(); |
|---|
| 735 | | |
|---|
| 736 | | # if (defined($ret)) { |
|---|
| 737 | | # print "Result: " . $ret . "\n"; |
|---|
| 738 | | # } |
|---|
| 739 | | |
|---|
| 740 | | # while (!defined($ret)) { |
|---|
| 741 | | # sleep (3); |
|---|
| 742 | | # print "Calling isRegisteredVM()...\n"; |
|---|
| 743 | | # $som = $stubVM->isRegisteredVM(config => $vmCloneConfig); |
|---|
| 744 | | # $ret = $som->result(); |
|---|
| 745 | | # if (defined($ret)) { |
|---|
| 746 | | # print "Result: " . $ret . "\n"; |
|---|
| 747 | | # } |
|---|
| 748 | | # } |
|---|
| 749 | | |
|---|
| 750 | | # print "Calling getStateVM()...\n"; |
|---|
| 751 | | # $som = $stubVM->getStateVM(config => $vmCloneConfig); |
|---|
| 752 | | # $vmState = $som->result(); |
|---|
| 753 | | # |
|---|
| 754 | | # if ($vmState == VM_EXECUTION_STATE_ON) { |
|---|
| 755 | | # print "ON\n"; |
|---|
| 756 | | # } elsif ($vmState == VM_EXECUTION_STATE_OFF) { |
|---|
| 757 | | # print "OFF\n"; |
|---|
| 758 | | # } elsif ($vmState == VM_EXECUTION_STATE_SUSPENDED) { |
|---|
| 759 | | # print "SUSPENDED\n"; |
|---|
| 760 | | # } elsif ($vmState == VM_EXECUTION_STATE_STUCK) { |
|---|
| 761 | | # print "STUCK\n"; |
|---|
| 762 | | # } else { |
|---|
| 763 | | # print "UNKNOWN\n"; |
|---|
| 764 | | # } |
|---|
| 765 | | |
|---|
| 766 | | # while ($vmState != VM_EXECUTION_STATE_ON) { |
|---|
| 767 | | # sleep (3); |
|---|
| 768 | | # |
|---|
| 769 | | # print "Calling getStateVM()...\n"; |
|---|
| 770 | | # $som = $stubVM->getStateVM(config => $vmCloneConfig); |
|---|
| 771 | | # $vmState = $som->result(); |
|---|
| 772 | | # |
|---|
| 773 | | # if ($vmState == VM_EXECUTION_STATE_ON) { |
|---|
| 774 | | # print "ON\n"; |
|---|
| 775 | | # } elsif ($vmState == VM_EXECUTION_STATE_OFF) { |
|---|
| 776 | | # print "OFF\n"; |
|---|
| 777 | | # } elsif ($vmState == VM_EXECUTION_STATE_SUSPENDED) { |
|---|
| 778 | | # print "SUSPENDED\n"; |
|---|
| 779 | | # } elsif ($vmState == VM_EXECUTION_STATE_STUCK) { |
|---|
| 780 | | # print "STUCK\n"; |
|---|
| 781 | | # } else { |
|---|
| 782 | | # print "UNKNOWN\n"; |
|---|
| 783 | | # } |
|---|
| 784 | | # } |
|---|
| 785 | | |
|---|
| 786 | | # print "Calling getMACaddrVM()...\n"; |
|---|
| 787 | | # $som = $stubVM->getMACaddrVM(config => $vmCloneConfig); |
|---|
| 788 | | # print "Result: " . $som->result() . "\n"; |
|---|
| 789 | | # $vmMAC = $som->result(); |
|---|
| 790 | | |
|---|
| 791 | | # # Figure out when the Agent on the VM is alive and well. |
|---|
| 792 | | # $ret = undef; |
|---|
| 793 | | # my $logMsgPrinted = 0; |
|---|
| 794 | | # while (!$ret) { |
|---|
| 795 | | # sleep (3); |
|---|
| 796 | | # print "Calling getIPaddrVM()...\n"; |
|---|
| 797 | | # $som = $stubVM->getIPaddrVM(config => $vmCloneConfig); |
|---|
| 798 | | # if (defined($som->result())) { |
|---|
| 799 | | # print "Result: " . $som->result() . "\n"; |
|---|
| 800 | | # } |
|---|
| 801 | | # $vmIP = $som->result(); |
|---|
| 802 | | # |
|---|
| 803 | | # print "Calling getNameVM()...\n"; |
|---|
| 804 | | # $som = $stubVM->getNameVM(config => $vmCloneConfig); |
|---|
| 805 | | # print "Result: " . $som->result() . "\n"; |
|---|
| 806 | | # $vmName = $som->result(); |
|---|
| 807 | | # |
|---|
| 808 | | # if (defined($vmIP) && defined($vmName)) { |
|---|
| 809 | | # if (!$logMsgPrinted) { |
|---|
| 810 | | # $LOG->info("Created clone VM (" . $vmName . ") using IP (" . $vmIP . ") and MAC (" . $vmMAC . ")."); |
|---|
| 811 | | # $logMsgPrinted = 1; |
|---|
| 812 | | # } |
|---|
| 813 | | # |
|---|
| 814 | | # # Try contacting the Agent; ignore any faults. |
|---|
| 815 | | # $SUPPRESS_ERRORS = 1; |
|---|
| 816 | | # $stubAgent = getClientHandle(namespace => "HoneyClient::Agent", |
|---|
| 817 | | # address => $vmIP, |
|---|
| 818 | | # fault_handler => \&_handleFault); |
|---|
| 819 | | # |
|---|
| 820 | | # eval { |
|---|
| 821 | | # print "Calling getStatus()...\n"; |
|---|
| 822 | | # $som = $stubAgent->getStatus(); |
|---|
| 823 | | # $ret = thaw(decode_base64($som->result())); |
|---|
| 824 | | # print "Result:\n"; |
|---|
| 825 | | # # Make Dumper format more verbose. |
|---|
| 826 | | # $Data::Dumper::Terse = 0; |
|---|
| 827 | | # $Data::Dumper::Indent = 2; |
|---|
| 828 | | # print Dumper($ret); |
|---|
| 829 | | # |
|---|
| 830 | | # }; |
|---|
| 831 | | # # Clear returned state, if any fault occurs. |
|---|
| 832 | | # if ($@) { |
|---|
| 833 | | # $ret = undef; |
|---|
| 834 | | # } |
|---|
| 835 | | # $SUPPRESS_ERRORS = 0; |
|---|
| 836 | | # } |
|---|
| 837 | | # } |
|---|
| 838 | | |
|---|
| | 828 | |
|---|
| | 829 | # Insert Compromised Fingerprint into DB. |
|---|
| | 830 | if ($DB_ENABLE && ($vm->database_id > 0)) { |
|---|
| | 831 | # Put URL History in database. |
|---|
| | 832 | $LOG->info("Saving URL History to Database."); |
|---|
| | 833 | insert_url_history(agent_state => $args{'agent_state'}, |
|---|
| | 834 | client_id => $vm->database_id); |
|---|
| | 835 | |
|---|
| | 836 | # Delete the 'last_resource' attribute. |
|---|
| | 837 | delete $fingerprint->{last_resource}; |
|---|
| | 838 | |
|---|
| | 839 | # Associate the client who has this fingerprint. |
|---|
| | 840 | $fingerprint->{client_id} = $vm->database_id; |
|---|
| | 841 | |
|---|
| | 842 | $LOG->info("Inserting Fingerprint Into Database."); |
|---|
| | 843 | my $fingerprint_id = undef; |
|---|
| | 844 | eval { |
|---|
| | 845 | $fingerprint_id = HoneyClient::Manager::Database::insert_fingerprint($fingerprint); |
|---|
| | 846 | }; |
|---|
| | 847 | if ($@ || ($fingerprint_id == 0) || !defined($fingerprint_id)) { |
|---|
| | 848 | $LOG->warn("Failure Inserting Fingerprint Object:\n$@"); |
|---|
| | 849 | } |
|---|
| | 850 | |
|---|
| | 851 | $LOG->info("Database Insert Successful."); |
|---|
| | 852 | } |
|---|
| | 853 | # Make sure VM is suspended. |
|---|
| 959 | | # Insert Compromised Fingerprint into DB. |
|---|
| 960 | | #if ($DB_ENABLE && ($clientDbId > 0)) { |
|---|
| 961 | | # #XXX: This should occurr as a resource is accessed and will be moved. Also should be in Browser code. |
|---|
| 962 | | # # Put Honeyclient Link History in database. |
|---|
| 963 | | # $LOG->info("Saving Url History to Database."); |
|---|
| 964 | | # $args{'agent_state'} = insert_url_history(agent_state => $args{'agent_state'}); |
|---|
| 965 | | # $globalAgentState = $args{'agent_state'}; |
|---|
| 966 | | # |
|---|
| 967 | | # # Remove the compromise time from the fingerprint. This is to be added to the Client Object |
|---|
| 968 | | # delete $fingerprint->{last_resource}; |
|---|
| 969 | | # my $compromise_time = HoneyClient::DB::Time->new(delete($fingerprint->{'compromise_time'})); |
|---|
| 970 | | # $LOG->info("Inserting Fingerprint Into Database."); |
|---|
| 971 | | # my $fp = HoneyClient::DB::Fingerprint->new($fingerprint); |
|---|
| 972 | | # my $fpId = $fp->insert(); |
|---|
| 973 | | # my $ctId = $compromise_time->insert(); |
|---|
| 974 | | # HoneyClient::DB::Client->update( |
|---|
| 975 | | # '-set' => { |
|---|
| 976 | | # status => $HoneyClient::DB::Client::STATUS_COMPROMISED, |
|---|
| 977 | | # fingerprint => $fpId, |
|---|
| 978 | | # compromise_time => $ctId, |
|---|
| 979 | | # }, |
|---|
| 980 | | # '-where' => { |
|---|
| 981 | | # id => $clientDbId, |
|---|
| 982 | | # } |
|---|
| 983 | | # ); |
|---|
| 984 | | # $LOG->info("Database Insert Successful."); |
|---|
| 985 | | #} |
|---|
| 1086 | | foreach my $i (keys %link_categories) { |
|---|
| 1087 | | my @url_history; |
|---|
| 1088 | | while (my ($url,$url_time) = each(%{$state->{$link_categories{$i}}})) { |
|---|
| 1089 | | # Don't insert already inserted URLs into DB. |
|---|
| 1090 | | if (!$url_time) { |
|---|
| 1091 | | next; |
|---|
| 1092 | | } |
|---|
| 1093 | | # Some ignored links are the result of invalid Urls. Preprocess to avoid errors. |
|---|
| 1094 | | my $url_obj = HoneyClient::DB::Url->new($url); |
|---|
| 1095 | | next if (!$url_obj); |
|---|
| 1096 | | my $u = HoneyClient::DB::Url::History->new({ |
|---|
| 1097 | | url => $url_obj, |
|---|
| 1098 | | visited => $url_time, |
|---|
| 1099 | | status => $i, |
|---|
| 1100 | | }); |
|---|
| 1101 | | push @url_history,$u; |
|---|
| 1102 | | # For all sucessfully inserted URLs, set their timestamps to 0. |
|---|
| 1103 | | $agent_state->{$agent_driver}->{$link_categories{$i}}->{$url} = 0; |
|---|
| 1104 | | } |
|---|
| 1105 | | |
|---|
| 1106 | | # Update the History item to reflect the Client it belongs to. |
|---|
| 1107 | | # get_col_name is used to get the foreign key column associated w/ the url_history array |
|---|
| 1108 | | HoneyClient::DB::Client->append_children( |
|---|
| 1109 | | '-parent_id' => $clientDbId, |
|---|
| 1110 | | 'url_history' => \@url_history, |
|---|
| 1111 | | ); |
|---|
| 1112 | | $LOG->info("Inserted Urls of type ".$link_categories{$i}); |
|---|
| 1113 | | } |
|---|
| 1114 | | |
|---|
| 1115 | | return encode_base64(nfreeze($agent_state)); |
|---|
| | 948 | # Set the client ID. |
|---|
| | 949 | $agent_state->{$agent_driver}->{'client_id'} = $args{'client_id'}; |
|---|
| | 950 | |
|---|
| | 951 | # XXX: Delete this, eventually. |
|---|
| | 952 | use Data::Dumper; |
|---|
| | 953 | $LOG->info("agent_state = " . Data::Dumper::Dumper($agent_state)); |
|---|
| | 954 | |
|---|
| | 955 | my $num_urls_inserted = HoneyClient::Manager::Database::insert_history_urls($agent_state->{$agent_driver}); |
|---|
| | 956 | $LOG->info($num_urls_inserted . " URL(s) Inserted."); |
|---|
| 1125 | | |
|---|
| 1126 | | #my $clientObj = HoneyClient::DB::Client->new({ |
|---|
| 1127 | | # system_id => $vmName, |
|---|
| 1128 | | # status => $HoneyClient::DB::Client::STATUS_RUNNING, |
|---|
| 1129 | | # # TODO: Collect host,application, and config through automation/config files |
|---|
| 1130 | | # host => { |
|---|
| 1131 | | # organization => 'MITRE', |
|---|
| 1132 | | # host_name => Sys::Hostname::Long::hostname_long, |
|---|
| 1133 | | # ip_address => Sys::HostIP->ip, |
|---|
| 1134 | | # }, |
|---|
| 1135 | | # client_app => { |
|---|
| 1136 | | # manufacturer => 'Microsoft', |
|---|
| 1137 | | # name => 'Internet Explorer', |
|---|
| 1138 | | # major_version => '6', |
|---|
| 1139 | | # }, |
|---|
| 1140 | | # config => { |
|---|
| 1141 | | # name => 'Default Windows XP SP2', |
|---|
| 1142 | | # os_name => 'Microsoft Windows', |
|---|
| 1143 | | # os_version => 'XP Professional', |
|---|
| 1144 | | # os_patches => [{ |
|---|
| 1145 | | # name => 'Service Pack 2', |
|---|
| 1146 | | # }], |
|---|
| 1147 | | # }, |
|---|
| 1148 | | # start_time => $dt->ymd('-').'T'.$dt->hms(':'), |
|---|
| 1149 | | #}); |
|---|
| 1150 | | #return $clientObj->insert(); |
|---|
| 1151 | | |
|---|