Class: Fofa::API
- Inherits:
-
Object
- Object
- Fofa::API
- Defined in:
- lib/fofa.rb
Instance Method Summary collapse
-
#import_service(file, options = {}) ⇒ Object
Import asset into fofa.
-
#initialize(email, apikey, options = {}) ⇒ API
constructor
A new instance of API.
-
#search(query, options = {}) ⇒ Object
Search page results from fofa.
-
#search_all(query) ⇒ Object
Search all results from fofa.
Constructor Details
#initialize(email, apikey, options = {}) ⇒ API
Returns a new instance of API.
8 9 10 11 12 13 |
# File 'lib/fofa.rb', line 8 def initialize(email, apikey, ={}) @options = {debug:false}.merge @api_server = ENV['FOFA_API_SERVER'] || 'https://fofa.so' @email = email @apikey = apikey end |
Instance Method Details
#import_service(file, options = {}) ⇒ Object
Import asset into fofa
Example:
>> Fofa::API.new(email,apikey).import("./http80.txt")
=> {size:1, results:['1.1.1.1:80']}
Arguments:
file: (String) assets file
options: (Hash) page
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 |
# File 'lib/fofa.rb', line 73 def import_service(file, ={}) = {port:80, split_size:100}.merge() url = "#{@api_server}/api/v1/import/services?key=#{@apikey}&email=#{@email}&port=#{[:port]}" puts url if @options[:debug] uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true end http.set_debug_output $stderr if @options[:debug] File.open(file) do |f| results = [] f.each_line.lazy.each_with_index do |line, i| line = line.strip if m = /Discovered open port (?<port>.*?)\/tcp on (?<host>.*?)$/.match(line) hostinfo = "#{m[:host]}:#{m[:port]}" elsif line.include?(':') hostinfo = line else hostinfo = "#{line}:#{[:port]}" end results << line if i % split_size == 0 req = Net::HTTP::Post.new(uri.request_uri) req.body = results.join("\n") resp = http.request(req) puts resp if @options[:debug] results = [] end end end rescue => e {"error"=>"Error: #{e.to_s}"} end |
#search(query, options = {}) ⇒ Object
Search page results from fofa
Example:
>> Fofa::API.new(email,apikey).search("host==baidu.com")
=> {size:1, results:['1.1.1.1:80']}
Arguments:
query: (String) fofa query string
options: (Hash) page(default 1, only fetch one page), size(defualt 100)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fofa.rb', line 24 def search(query, ={}) = {page:1,size:100}.merge() url = "#{@api_server}/api/v1/search/all?key=#{@apikey}&email=#{@email}&q=#{URI.escape(query)}&page=#{[:page]}&size=#{[:size]}" puts url if @options[:debug] uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true end req = Net::HTTP::Get.new(uri.request_uri) resp = http.request(req) JSON.parse(resp.body) rescue => e {"error"=>"Error: #{e.to_s}"} end |
#search_all(query) ⇒ Object
Search all results from fofa
Example:
>> Fofa::API.new(email,apikey).search_all("domain==baidu.com") {|results, page| ... }
=> {size:1, results:['1.1.1.1:80']}
Arguments:
query: (String) fofa query string
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/fofa.rb', line 48 def search_all(query) all_res = [] page = 0 loop do page += 1 res = search(query, {page:page}) if !res['error'] && res['results'].size > 0 all_res += res['results'] yield(res['results'], page) if block_given? else break end end all_res end |