Class: Fofa::API

Inherits:
Object
  • Object
show all
Defined in:
lib/fofa.rb

Instance Method Summary collapse

Constructor Details

#initialize(email, apikey, options = {}) ⇒ API

Returns a new instance of API.



9
10
11
12
13
14
# File 'lib/fofa.rb', line 9

def initialize(email, apikey, options={})
	@options = {debug:false}.merge options
	@api_server = ENV['FOFA_API_SERVER'] || 'https://fofa.so'
	@email = email || ENV['FOFA_EMAIL']
	@apikey = apikey || ENV['FOFA_KEY']
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


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
# File 'lib/fofa.rb', line 101

def import_service(file, options={})
  options = {port:80, split_size:100}.merge(options)
  url = "#{@api_server}/api/v1/import/services?key=#{@apikey}&email=#{@email}&port=#{options[:port]}"
  puts url if @options[:debug]
  uri = URI.parse(url)
  http = http_new(uri)

  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}:#{options[: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

#ip_tags(file) ⇒ Object

Import ip and get tags

Example:

>> Fofa::API.new(email,apikey).ip_tags('./ips.txt')
=> {"task_id"=>1}

Arguments:

file: (String) ip file


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
# File 'lib/fofa.rb', line 166

def ip_tags(file)
  url = "#{@api_server}/api/v1/ip_tags?key=#{@apikey}&email=#{@email}"
  puts url if @options[:debug]

  # Token used to terminate the file in the post body. Make sure it is not
  # present in the file you're uploading.
  # You might want to use `SecureRandom` class to generate this random strings
  boundary = "AaB03x"
  uri = URI.parse(url)

  post_body = []
  post_body << "--#{boundary}\r\n"
  post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{File.basename(file)}\"\r\n"
  post_body << "Content-Type: text/plain\r\n"
  post_body << "\r\n"
  post_body << File.read(file)
  post_body << "\r\n--#{boundary}--\r\n"

  http = http_new(uri)
  req = Net::HTTP::Post.new(uri.request_uri)
  req.body = post_body.join
  req["Content-Type"] = "multipart/form-data, boundary=#{boundary}"

  resp = http.request(req)
  puts resp if @options[:debug]
  JSON.parse(resp.body)
rescue => e
  {"error"=>"Error: #{e.to_s}"}
end

#search(query, options = {}) ⇒ Object

Arguments:

query: (String) fofa query string
options: (Hash) page(default 1, only fetch one page), size(defualt 100)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fofa.rb', line 29

def search(query, options={})
    options = {page:1, size:100, fields:'host'}.merge(options)

		url = "#{@api_server}/api/v1/search/all?key=#{@apikey}&email=#{@email}&page=#{options[:page]}&size=#{options[:size]}&fields=#{options[:fields]}"
		url += "&qbase64=#{Base64.encode64(query)}" unless options[:post]
    puts url if @options[:debug]
		uri = URI.parse(url)
    http = http_new(uri)

    if options[:post]
      req = Net::HTTP::Post.new(uri.request_uri)
      req.body = query
    else
      req = Net::HTTP::Get.new(uri.request_uri)
    end

    resp = http.request(req)
    JSON.parse(resp.body)
rescue => e
  $stderr.puts "[WARNING]: #{e.to_s}"
	{"error"=>"Error: #{e.to_s}"}
end

#search_all(query, options = {}) ⇒ 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']}

Example:

>> Fofa::API.new(email,apikey).search_all("domain==baidu.com", fields:'host,title,ip') {|results, page| ... }
=> {size:1, results:[['1.1.1.1:81', 'title', '1.1.1.1']]}

Arguments:

query: (String) fofa query string


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
# File 'lib/fofa.rb', line 64

def search_all(query, options={})
  all_size = options.delete :size if options.has_key?(:size)# do not pass to search
  if options.has_key?(:page_size)
    options[:size] = options.delete :page_size
  end
  all_res = []
  page = 0
  loop do
    page += 1
    res = search(query, {page:page}.merge(options))
    if !res['error'] && res['results'].size > 0
      all_res += res['results']
      yield(res['results'], page, res['size'].to_i) if block_given?

      if all_size && all_res.size > all_size.to_i
        all_res = all_res[0..all_size-1]
        break
      end

      break if all_res.size >= res['size'].to_i
    else
      raise res['errmsg'] if res['errmsg']
      break
    end
  end
  all_res
end

#stats(query, options = {}) ⇒ Object

Fetch fids from query

Example:

>> Fofa::API.new(email,apikey).stats("domain=baidu.com", fields:"fid")
=> ["Coremail"]

Arguments:

query: (String) fofa query string
options: (Hash) fields: can be return multiple fields, like fid,host and so on


144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fofa.rb', line 144

def stats(query, options={})
  options = {fields:"fid"}.merge(options)
  url = "#{@api_server}/api/v1/search/stats?key=#{@apikey}&email=#{@email}&qbase64=#{Base64.encode64(query)}"
  url += "&fields=#{options[:fields]}" if options[:fields]
  puts url if @options[:debug]
  uri = URI.parse(url)
  http = http_new(uri)
  req = Net::HTTP::Get.new(uri.request_uri)
  resp = http.request(req)
  JSON.parse(resp.body)
rescue => e
  {"error"=>"Error: #{e.to_s}"}
end

#tag_info(task_id) ⇒ Object

Check ip_tags’s task state, progress, and download_url

Example:

>> Fofa::API.new(email,apikey).tag_info(1)
=> {"state"=>"success", "progress"=>100, "download_url"=>"https://host/xxx/56af409c1c55762a7b9e9a39a801f80d.json"}

Arguments:

task_id: (Integer) task id


204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fofa.rb', line 204

def tag_info(task_id)
  url = "#{@api_server}/api/v1/ip_tags/info?key=#{@apikey}&email=#{@email}&task_id=#{task_id}"
  puts url if @options[:debug]
  uri = URI.parse(url)
  http = http_new(uri)
  req = Net::HTTP::Get.new(uri.request_uri)

  resp = http.request(req)
  JSON.parse(resp.body)
rescue => e
  $stderr.puts "[WARNING]: #{e.to_s}"
  {"error"=>"Error: #{e.to_s}"}
end