Module: PWN::AI

Defined in:
lib/pwn/ai.rb,
lib/pwn/ai/grok.rb,
lib/pwn/ai/agent.rb,
lib/pwn/ai/gemini.rb,
lib/pwn/ai/ollama.rb,
lib/pwn/ai/open_ai.rb,
lib/pwn/ai/red_team.rb,
lib/pwn/ai/agent/btc.rb,
lib/pwn/ai/anthropic.rb,
lib/pwn/ai/agent/gqrx.rb,
lib/pwn/ai/agent/loop.rb,
lib/pwn/ai/agent/sast.rb,
lib/pwn/ai/agent/swarm.rb,
lib/pwn/ai/open_web_ui.rb,
lib/pwn/ai/agent/policy.rb,
lib/pwn/ai/agent/result.rb,
lib/pwn/ai/agent/reward.rb,
lib/pwn/ai/agent/metrics.rb,
lib/pwn/ai/agent/reflect.rb,
lib/pwn/ai/agent/assembly.rb,
lib/pwn/ai/agent/dispatch.rb,
lib/pwn/ai/agent/learning.rb,
lib/pwn/ai/agent/mistakes.rb,
lib/pwn/ai/agent/registry.rb,
lib/pwn/ai/agent/vuln_gen.rb,
lib/pwn/ai/agent/burp_suite.rb,
lib/pwn/ai/agent/curriculum.rb,
lib/pwn/ai/agent/hacker_one.rb,
lib/pwn/ai/agent/tool_guard.rb,
lib/pwn/ai/red_team/jailbreak.rb,
lib/pwn/ai/agent/extrospection.rb,
lib/pwn/ai/agent/prompt_builder.rb,
lib/pwn/ai/agent/task_summarizer.rb,
lib/pwn/ai/red_team/overreliance.rb,
lib/pwn/ai/red_team/token_smuggling.rb,
lib/pwn/ai/agent/transparent_browser.rb,
lib/pwn/ai/red_team/excessive_agency.rb,
lib/pwn/ai/red_team/prompt_injection.rb,
lib/pwn/ai/red_team/test_case_engine.rb,
lib/pwn/ai/red_team/payload_splitting.rb,
lib/pwn/ai/agent/extrospection/osint/social.rb,
lib/pwn/ai/agent/extrospection/osint/bridges.rb,
lib/pwn/ai/red_team/insecure_output_handling.rb,
lib/pwn/ai/red_team/system_prompt_extraction.rb,
lib/pwn/ai/red_team/sensitive_information_disclosure.rb

Overview

This file, using the autoload directive loads SAST modules into memory only when they're needed. For more information, see: http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html

Defined Under Namespace

Modules: Agent, Anthropic, Gemini, Grok, Ollama, OpenAI, OpenWebUI, RedTeam

Constant Summary collapse

PLAN_USAGE_CACHE_TTL =
60
PLAN_USAGE_INFINITY =
"\u221E"
ENGINE_MODULES =
{
  openai: 'PWN::AI::OpenAI',
  grok: 'PWN::AI::Grok',
  ollama: 'PWN::AI::Ollama',
  openwebui: 'PWN::AI::OpenWebUI',
  anthropic: 'PWN::AI::Anthropic',
  gemini: 'PWN::AI::Gemini'
}.freeze

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. support@0dayinc.com



107
108
109
# File 'lib/pwn/ai.rb', line 107

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <support@0dayinc.com>\n"
end

.helpObject



111
112
113
# File 'lib/pwn/ai.rb', line 111

public_class_method def self.help
  constants.sort
end

.normalize_plan_usage(opts = {}) ⇒ Object

Supported Method Parameters

usage = PWN::AI.normalize_plan_usage( used: 'required - numeric used amount', limit: 'required - numeric limit / allowance', source: 'optional - endpoint / origin label', engine: 'optional - engine symbol' )



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pwn/ai.rb', line 35

public_class_method def self.normalize_plan_usage(opts = {})
  used = opts[:used]
  limit = opts[:limit]
  used_f = used&.to_f
  limit_f = limit&.to_f
  available = !used_f.nil? && !limit_f.nil? && limit_f.positive?
  percent = available ? ((used_f / limit_f) * 100.0).round : nil
  percent = 0 if percent && percent.negative?
  percent = 100 if percent && percent > 100
  {
    available: available,
    used: used_f,
    limit: limit_f,
    percent: percent,
    source: opts[:source],
    engine: opts[:engine]
  }
end

.plan_usage(opts = {}) ⇒ Object

Supported Method Parameters

usage = PWN::AI.plan_usage( engine: 'optional - engine symbol (defaults to PWN::Env[:active])', ttl: 'optional - cache seconds (default 60)', force: 'optional - bypass cache (default false)' )



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pwn/ai.rb', line 60

public_class_method def self.plan_usage(opts = {})
  engine = (opts[:engine] || PWN::Env.dig(:ai, :active)).to_s.downcase.to_sym
  ttl = (opts[:ttl] || PLAN_USAGE_CACHE_TTL).to_i
  ttl = PLAN_USAGE_CACHE_TTL if ttl <= 0
  cache = (@plan_usage_cache ||= {})
  now = Time.now.to_i
  hit = cache[engine]
  return hit[:value] if !opts[:force] && hit.is_a?(Hash) && (now - hit[:at].to_i) < ttl

  klass_name = ENGINE_MODULES[engine]
  value = { available: false, engine: engine }
  if klass_name
    begin
      klass = Object.const_get(klass_name)
      value = klass.get_plan_usage if klass.respond_to?(:get_plan_usage)
      value = { available: false, engine: engine } unless value.is_a?(Hash)
      value[:engine] ||= engine
    rescue StandardError
      value = { available: false, engine: engine }
    end
  end
  cache[engine] = { at: now, value: value }
  value
end

.plan_usage_glyph(opts = {}) ⇒ Object

Supported Method Parameters

glyph = PWN::AI.plan_usage_glyph( engine: 'optional - engine symbol (defaults to PWN::Env[:active])', usage: 'optional - precomputed usage hash' )

Returns "19%" when used/limit are known, otherwise the infinity glyph. Local engines (ollama / openwebui) always return infinity.



93
94
95
96
97
98
99
100
101
# File 'lib/pwn/ai.rb', line 93

public_class_method def self.plan_usage_glyph(opts = {})
  engine = (opts[:engine] || PWN::Env.dig(:ai, :active)).to_s.downcase.to_sym
  return PLAN_USAGE_INFINITY if %i[ollama openwebui].include?(engine)

  usage = opts[:usage] || plan_usage(engine: engine)
  return PLAN_USAGE_INFINITY unless usage.is_a?(Hash) && usage[:available] && !usage[:percent].nil?

  "#{usage[:percent].to_i}%"
end