-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Ollama Enum #21271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
h00die
wants to merge
5
commits into
rapid7:master
Choose a base branch
from
h00die:ollama_enum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ollama Enum #21271
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d5b4cae
ollama enum
h00die 15a50cc
Merge branch 'rapid7:master' into ollama_enum
h00die 02ada24
Update documentation/modules/auxiliary/scanner/http/ollama_info.md
h00die ac26329
Update modules/auxiliary/scanner/http/ollama_info.rb
h00die 05256aa
AI review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
documentation/modules/auxiliary/scanner/http/ollama_info.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| ## Vulnerable Application | ||
|
|
||
| This module identifies ollama instances and enumerates the LLM | ||
| models which have been loaded and are running. | ||
|
|
||
| ### Building Image | ||
|
|
||
| Write the following dockerfile. | ||
|
|
||
| ```dockerfile | ||
| FROM ollama/ollama | ||
|
|
||
| EXPOSE 11434 | ||
|
|
||
| VOLUME /root/.ollama | ||
|
|
||
| RUN /bin/ollama serve & \ | ||
| sleep 5 && \ | ||
| /bin/ollama pull llama3.2:1b && \ | ||
| /bin/ollama pull qwen3.5:0.8b && \ | ||
| /bin/ollama pull smollm:135m && \ | ||
| printf 'FROM smollm:135m\nSYSTEM "you are an AI assistant and this is your system prompt"\n' > /Modelfile && \ | ||
| /bin/ollama create my-model -f /Modelfile | ||
|
|
||
| RUN printf '#!/bin/bash\n/bin/ollama serve &\nsleep 3\ncurl -s http://localhost:11434/api/chat -d '"'"'{"model":"my-model","stream":false,"messages":[{"role":"user","content":"warmup"}]}'"'"'\nwait\n' > /start.sh && \ | ||
| chmod +x /start.sh | ||
|
|
||
| ENTRYPOINT [] | ||
| CMD ["/start.sh"] | ||
| ``` | ||
|
|
||
| Build and start it. | ||
|
|
||
| ``` | ||
| docker build -t my-ollama . | ||
| docker run -d -p 11434:11434 --name my-ollama my-ollama | ||
| ``` | ||
|
|
||
| ## Verification Steps | ||
|
|
||
| 1. Start the ollama docker | ||
| 2. Start msfconsole | ||
| 3. Do: `use auxiliary/scanner/http/ollama_info` | ||
| 4. Do: `set rhosts [IPs]` | ||
| 5. Do: `run` | ||
| 6. You should get information about the models in teh ollama instance | ||
|
|
||
| ## Options | ||
|
|
||
| ## Scenarios | ||
|
|
||
| ### Docker image | ||
|
|
||
| ``` | ||
| msf > use auxiliary/scanner/http/ollama_info | ||
| msf auxiliary(scanner/http/ollama_info) > set rhosts 127.0.0.1 | ||
| rhosts => 127.0.0.1 | ||
| msf auxiliary(scanner/http/ollama_info) > run | ||
| [*] Checking 127.0.0.1 | ||
| [*] Found model: my-model:latest | ||
| [*] Found model: smollm:135m | ||
| [*] Found model: qwen3.5:0.8b | ||
| [*] Found model: llama3.2:1b | ||
| [*] 127.0.0.1 Ollama Models | ||
| ======================= | ||
|
|
||
| Name Release Status Size Parameter Size Temperature System Prompt | ||
| ---- ------- ------ ---- -------------- ----------- ------------- | ||
| llama3.2 1b Installed 1.23 GB 1.2B N/A N/A | ||
| my-model latest Running 130.77 MB 134.52M 0.2 you are an AI assistant and this is your system prompt | ||
| qwen3.5 0.8b Installed 988.05 MB 873.44M 1 N/A | ||
| smollm 135m Installed 87.49 MB 134.52M 0.2 N/A | ||
|
|
||
| [*] Scanned 1 of 1 hosts (100% complete) | ||
| [*] Auxiliary module execution completed | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| ## | ||
| # This module requires Metasploit: https://metasploit.com/download | ||
| # Current source: https://github.com/rapid7/metasploit-framework | ||
| ## | ||
|
|
||
| class MetasploitModule < Msf::Auxiliary | ||
| include Msf::Exploit::Remote::HttpClient | ||
| include Msf::Auxiliary::Scanner | ||
|
|
||
| def initialize(info = {}) | ||
| super( | ||
| update_info( | ||
| info, | ||
| 'Name' => 'Ollama Scanner', | ||
| 'Description' => %q{ | ||
| This module identifies ollama instances and enumerates the LLM | ||
| models which have been loaded and are running. | ||
| }, | ||
| 'License' => MSF_LICENSE, | ||
| 'Author' => [ | ||
| 'h00die' | ||
| ], | ||
| 'References' => [ | ||
| ['URL', 'https://ollama.readthedocs.io/en/api/'] | ||
| ], | ||
| 'Notes' => { | ||
| 'Stability' => [CRASH_SAFE], | ||
| 'Reliability' => [], | ||
| 'SideEffects' => [] | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| register_options( | ||
| [ | ||
| Opt::RPORT(11434), | ||
| OptString.new('TARGETURI', [true, 'Base URI', '/']), | ||
| ] | ||
| ) | ||
| end | ||
|
|
||
| def humanize(bytes) | ||
| units = ['B', 'KB', 'MB', 'GB', 'TB'] | ||
| i = (Math.log2(bytes) / 10).to_i | ||
|
h00die marked this conversation as resolved.
Outdated
|
||
| '%.2f %s' % [bytes.to_f / (1024**i), units[i]] | ||
| end | ||
|
|
||
| def ollama? | ||
| res = send_request_cgi({ 'uri' => normalize_uri(datastore['TARGETURI']) }) | ||
|
|
||
| return res.body == 'Ollama is running' if res && res.code == 200 | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def generate | ||
| res = send_request_cgi({ 'uri' => normalize_uri(datastore['TARGETURI'], 'api', 'generate') }) | ||
|
|
||
| return res.get_json_document if res && res.code == 200 | ||
|
|
||
| nil | ||
| end | ||
|
|
||
|
h00die marked this conversation as resolved.
Outdated
|
||
| def list_local_models | ||
| res = send_request_cgi({ 'uri' => normalize_uri(datastore['TARGETURI'], 'api', 'tags') }) | ||
|
|
||
| return res.get_json_document if res && res.code == 200 | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def list_running_models | ||
| res = send_request_cgi({ 'uri' => normalize_uri(datastore['TARGETURI'], 'api', 'ps') }) | ||
|
|
||
| return res.get_json_document if res && res.code == 200 | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def get_model_info(model) | ||
| post_data = { | ||
| 'model' => model | ||
| } | ||
| post_json = JSON.generate(post_data) | ||
| res = send_request_cgi({ | ||
| 'method' => 'POST', | ||
| 'ctype' => 'application/json', | ||
| 'data' => post_json, | ||
| 'uri' => normalize_uri(target_uri.path, 'api', 'show') | ||
| }) | ||
|
|
||
| return res.get_json_document if res && res.code == 200 | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def get_temperature(details) | ||
| unless details.nil? || details['parameters'].nil? | ||
| details['parameters'].each_line do |line| | ||
| next unless line.start_with?('temperature') | ||
|
|
||
| return line.split[1] | ||
| end | ||
| end | ||
| 'N/A' | ||
| end | ||
|
|
||
| def get_system_prompt(details) | ||
| unless details.nil? || details['modelfile'].nil? | ||
| details['modelfile'].each_line do |line| | ||
| next unless line.start_with?('SYSTEM ') | ||
|
|
||
| return line.split('SYSTEM ')[1] | ||
| end | ||
| end | ||
| 'N/A' | ||
| end | ||
|
|
||
| def run_host(ip) | ||
| vprint_status("Checking #{ip}") | ||
| unless ollama? | ||
| vprint_error('Ollama instance not found') | ||
| return | ||
| end | ||
| models_table = Rex::Text::Table.new( | ||
| 'Header' => "#{ip} Ollama Models", | ||
| 'Indent' => 2, | ||
| 'Columns' => [ | ||
| 'Name', | ||
| 'Release', | ||
| 'Status', | ||
| 'Size', | ||
| 'Parameter Size', | ||
| 'Temperature', | ||
| 'System Prompt' | ||
| ] | ||
| ) | ||
| running = [] | ||
| local_models = list_running_models | ||
| local_models['models'].each do |model| | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. local_models can be nil, we either need to add nil checks for all the results from the helper functions or I think I'd prefer raising early so we know something hasn't gone quite right
h00die marked this conversation as resolved.
Outdated
h00die marked this conversation as resolved.
Outdated
|
||
| vprint_status(" Found model: #{model['name']}") | ||
| details = get_model_info(model['name']) | ||
| temperature = get_temperature(details) | ||
| system_prompt = get_system_prompt(details) | ||
|
|
||
| models_table << [ | ||
| model['name'].split(':')[0], | ||
| model['name'].split(':')[1], | ||
| 'Running', | ||
| humanize(model['size']), | ||
| details.dig('details', 'parameter_size'), | ||
| temperature, | ||
| system_prompt | ||
| ] | ||
| running << model['name'] | ||
| end | ||
| local_models = list_local_models | ||
| local_models['models'].each do |model| | ||
|
h00die marked this conversation as resolved.
Outdated
h00die marked this conversation as resolved.
Outdated
|
||
| next if running.include?(model['name']) | ||
|
|
||
| vprint_status(" Found model: #{model['name']}") | ||
| details = get_model_info(model['name']) | ||
| temperature = get_temperature(details) | ||
| system_prompt = get_system_prompt(details) | ||
|
|
||
| models_table << [ | ||
| model['name'].split(':')[0], | ||
| model['name'].split(':')[1], | ||
| 'Installed', | ||
| humanize(model['size']), | ||
| details.dig('details', 'parameter_size'), | ||
| temperature, | ||
| system_prompt | ||
| ] | ||
| end | ||
|
|
||
| print_status(models_table.to_s) | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.