Skip to content

Commit

Permalink
Added the ronin-web xml command (closes #88).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Dec 6, 2023
1 parent 1abe643 commit fce5f66
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Commands:
spider
user-agent
wordlist
xml
```

Open the `ronin-web` Ruby REPL:
Expand Down
1 change: 1 addition & 0 deletions gemspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ generated_files:
- man/ronin-web-session-cookie.1
- man/ronin-web-user-agent.1
- man/ronin-web-wordlist.1
- man/ronin-web-xml.1

dependencies:
nokogiri: ~> 1.4
Expand Down
48 changes: 3 additions & 45 deletions lib/ronin/web/cli/commands/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with ronin-web. If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/web/cli/command'
require 'ronin/web/cli/commands/xml'
require 'ronin/support/network/http'

require 'nokogiri'
Expand Down Expand Up @@ -55,19 +55,10 @@ module Commands
# URL | FILE The URL or FILE to search
# [XPATH | CSS-path] The XPath or CSS-path query
#
class Html < Command
class Html < Xml

usage '[options] {URL | FILE} [XPATH | CSS-path]'

option :xpath, short: '-X',
value: {
type: String,
usage: 'XPATH'
},
desc: 'XPath query' do |xpath|
@query = xpath
end

option :css_path, short: '-C',
value: {
type: String,
Expand All @@ -77,9 +68,6 @@ class Html < Command
@query = css_path
end

option :text, short: '-t',
desc: 'Prints the inner-text'

option :meta_tags, short: '-M',
desc: 'Searches for all <meta ...> tags' do
@query = '//meta'
Expand Down Expand Up @@ -120,9 +108,6 @@ class Html < Command
@query = '//a/@href | //link/@href | //script/@src | //form/@action'
end

option :first, short: '-F',
desc: 'Only print the first match'

argument :source, required: true,
usage: 'URL | FILE',
desc: 'The URL or FILE to search'
Expand Down Expand Up @@ -155,34 +140,7 @@ def run(source,query=@query)
exit(-1)
end

doc = parse(read(source))
nodes = if options[:first] then doc.at(query)
else doc.search(query)
end

if options[:text]
puts nodes.inner_text
else
puts nodes
end
end

#
# Reads a URI or file.
#
# @param [String] source
# The URI or file path.
#
# @return [File, String]
# The contents of the URI or file.
#
def read(source)
if source.start_with?('https://') ||
source.start_with?('http://')
Support::Network::HTTP.get_body(source)
else
File.new(source)
end
super(source,query)
end

#
Expand Down
149 changes: 149 additions & 0 deletions lib/ronin/web/cli/commands/xml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# frozen_string_literal: true
#
# ronin-web - A collection of useful web helper methods and commands.
#
# Copyright (c) 2006-2023 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# ronin-web is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-web is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ronin-web. If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/web/cli/command'
require 'ronin/support/network/http'

require 'nokogiri'

module Ronin
module Web
class CLI
module Commands
#
# Performs XPath queries on a URL or XML file.
#
# ## Usage
#
# ronin-web xml [options] {URL | FILE} [XPATH]
#
# ## Options
#
# -X, --xpath XPATH XPath query
# -t, --text Prints the inner-text
# -F, --first Only print the first match
# -h, --help Print help information
#
# ## Arguments
#
# URL | FILE The URL or FILE to search
# XPATH The XPath query
#
# @since 2.0.0
#
class Xml < Command

usage '[options] {URL | FILE} [XPATH]'

option :xpath, short: '-X',
value: {
type: String,
usage: 'XPATH'
},
desc: 'XPath query' do |xpath|
@query = xpath
end

option :first, short: '-F',
desc: 'Only print the first match'

option :text, short: '-t',
desc: 'Prints the elements inner text'

argument :source, required: true,
usage: 'URL | FILE',
desc: 'The URL or FILE to search'

argument :query, required: false,
usage: 'XPATH',
desc: 'The XPath query'

description 'Performs XPath queries on a URL or HTML file'

man_page 'ronin-web-xml.1'

# The XPath expression.
#
# @return [String, nil]
attr_reader :query

#
# Runs the `ronin-web xml` command.
#
# @param [String] source
# The `URL` or `FILE` argument.
#
# @param [String, nil] query
# The optional XPath argument.
#
def run(source,query=@query)
unless query
print_error "must specify --xpath or an XPath argument"
exit(-1)
end

doc = parse(read(source))
nodes = if options[:first] then doc.at(query)
else doc.search(query)
end

if options[:text]
puts nodes.inner_text
else
puts nodes
end
end

#
# Reads a URI or file.
#
# @param [String] source
# The URI or file path.
#
# @return [File, String]
# The contents of the URI or file.
#
def read(source)
if source.start_with?('https://') ||
source.start_with?('http://')
Support::Network::HTTP.get_body(source)
else
File.new(source)
end
end

#
# Parses the HTML source code.
#
# @param [String] html
# The raw unparsed XML.
#
# @return [Nokogiri::XML::Document]
# The parsed XML document.
#
def parse(html)
Nokogiri::XML(html)
end

end
end
end
end
end
9 changes: 6 additions & 3 deletions man/ronin-web-html.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Performs XPath/CSS-path queries on a URL or HTML file.
`-C`, `--css-path` *CSS-path*
: The CSS-path query to perform.

`-F`, `--first`
: Only print the first match.

`-t`, `--text`
: Prints the inner-text of the matching elements.

Expand Down Expand Up @@ -61,13 +64,13 @@ Performs XPath/CSS-path queries on a URL or HTML file.
`-u`, `--urls`
: Dumps all URLs in the page.

`-F`, `--first`
: Only print the first match.

`-h`, `--help`
: Print help information.

## AUTHOR

Postmodern <[email protected]>

## SEE ALSO

[ronin-web-xml](ronin-web-xml.1.md)
46 changes: 46 additions & 0 deletions man/ronin-web-xml.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ronin-web-xml 1 "2022-01-01" Ronin "User Manuals"

## NAME

ronin-web-xml - Performs XPath queries on a URL or XML file

## SYNOPSIS

`ronin-web xml` [*options*] {*URL* \| *FILE*} [*XPATH*]

## DESCRIPTION

Performs XPath queries on a URL or HTML file.

## ARGUMENTS

*URL*
: The `http://` or `https://` URL to fetch and parse.

*FILE*
: The local XML file to parse.

*XPATH*
: The XPath query expression.

## OPTIONS

`-X`, `--xpath` *XPATH*
: The XPath query to perform.

`-F`, `--first`
: Only print the first match.

`-t`, `--text`
: Prints the inner-text of the matching elements.

`-h`, `--help`
: Print help information.

## AUTHOR

Postmodern <[email protected]>

## SEE ALSO

[ronin-web-html](ronin-web-html.1.md)
2 changes: 1 addition & 1 deletion man/ronin-web.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Postmodern <[email protected]>

## SEE ALSO

[ronin-web-diff](ronin-web-diff.1.md) [ronin-web-html](ronin-web-html.1.md) [ronin-web-server](ronin-web-server.1.md) [ronin-web-spider](ronin-web-spider.1.md) [ronin-web-proxy](ronin-web-proxy.1.md)
[ronin-web-diff](ronin-web-diff.1.md) [ronin-web-html](ronin-web-html.1.md) [ronin-web-server](ronin-web-server.1.md) [ronin-web-spider](ronin-web-spider.1.md) [ronin-web-proxy](ronin-web-proxy.1.md) [ronin-web-xml](ronin-web-xml.1.md)
Loading

0 comments on commit fce5f66

Please sign in to comment.