为什么我的代码输出enumaration值而不是字符串?

时间:2019-04-08 23:42:43

标签: ruby

我正在为CTF用ruby编写基于字典的攻击程序,但是我的输出显示枚举值而不是字符串。我已经尝试过将输出的变量显式转换为字符串,但是没有任何改变。

require 'net/http'

def checkUsage()
  if ARGV.length != 1
    return false
  end
  return true
end


def generateUsername()

  wordArray = Array.new
  wordlist = File.open("words.txt", "r")

  for word in wordlist
    wordArray.push(word)
  end

 return wordArray.repeated_permutation(7).to_s

end

def generatePassword()

  wordArray = Array.new
  wordlist = File.open("words.txt", "r")

  for word in wordlist
    wordArray.push(word)
  end

  return wordArray.repeated_permutation(7).to_s

end

def requestAuthentication()

  if(!checkUsage())
    puts("Usage: frsDic <wordlist>")
    return false
  end

  uri = URI("http://challenges.laptophackingcoffee.org:3199/secret.php")
  req = Net::HTTP::Get.new(uri)

  loop do

    username = generateUsername()
    password = generatePassword()

    if req.basic_auth username, password
      puts"Username found: " + username
      puts"Password found: " + password
      break
    else
      puts"Username failed: " + username
      puts"Password failed: " + password
    end
  end
end


requestAuthentication()

输出:


#<Enumerator:0x000055a491c74ad0>
#<Enumerator:0x000055a491c74828>
#<Enumerator:0x000055a491c74ad0>
#<Enumerator:0x000055a491c74828>

我原本希望打印出蛮力发现的用户名/密码的字符串,但它只打印枚举值。

1 个答案:

答案 0 :(得分:1)

如果您不提供块,则方法repeated_permutation将返回一个Enumerator。如果您想遍历所有排列,则可以直接将一个块传递给它:

wordArray.repeated_permutation(7) { |permutation| puts permutation }

或者您可以将枚举器传递到某个地方,然后在其上调用.each

word_enumerator = wordArray.repeated_permutation(7)
word_enumerator.each { |permutation| puts permutation }