0

我正在尝试在 Ruby 中编写一个简单的模糊器,它将逐行从文本文件中获取值,并将这些值替换为用户从控制台输入的 GET URL 参数中的符号 $fuzz$。

我写过这样的代码:

require 'net/http'

uri = URI(gets())

Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri
  response = http.request request 
  puts response.body
end

我很困惑如何将用户在 GET 请求中从控制台给出的符号 $fuzz$ 替换为每行的文件内容。

就像文件的第一行是 ' 或 1=1 ,我想用文件中的这一行内容替换一些 GET 请求 /item.php?id=$fuzz$ 中写入的 $fuzz$ 符号,然后想要触发这些请求。

例如,我以 URL 形式控制台说 example.com/item.php?id=fuzz,现在我打开一个包含多行的文件。我想再次编辑从控制台获取的这个 URL,并将“fuzz”字逐一替换为文件行,并希望发送这些 URL 请求,并将文件内容替换为 GET 请求参数中的单词 fuzz,然后希望将所有响应存储在新文件中.

4

1 回答 1

0

你会想把它分成两个类

require 'net/http'

class FileIdExtractor
  def ids_from_file(path_to_file)
    file = File.open(path_to_file) or die "Unable to open file..."
    content_array=[]
    file.each_line {|line| contentArray.push line }
    content_array
  end
end

class UrlToFile
  def initialize(url)
    @url = url
  end

  def write_content_to_file_for_id(id, doc)
    local_filename = "/tmp/#{id}.html"
    File.open(local_filename, 'w') {|f| f.write(doc) }
  end

  def get_html_content_from_id(id)
    full_url = @url + id
    open(full_url).read
  end

end

# to run it, something like
puts "What is the URL. use format http://example.com/item.php?id="
url = gets
ids = FileIdExtractor.new.ids_from_file("your-file-path")
ids.each do |id|
  url = UrlToFile.new(url)
  doc = url.get_html_content_from_id(id)
  url.write_content_to_file_for_id(id, doc)
end
于 2012-01-23T18:05:33.410 回答