kscript 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a72c639d7028f3032a3ab8d6508308cd174a1174f698d5099f86356b1bef9f31
4
- data.tar.gz: 898a2845e93a58bb1760a92850b091b84fcd374d92b138c13022e85dc6868877
3
+ metadata.gz: 812d19779e0fadd4fc72f94999c3d8776a2391147c467c14067d203050c94c01
4
+ data.tar.gz: adad5fb5f74798030650f6595f8e90fc86eacfb490a4b7bb5c2fefca1d4552d7
5
5
  SHA512:
6
- metadata.gz: 869a3dfa24680f0c6e95cbdb82f0d8e6f981f5868d5f5c30b0183b7d2490b64ee2ce670e08190f095eea1291f26c31293e215759ee9545eab2a1d602b36a9bba
7
- data.tar.gz: 5d24ef70c21851e42b337fd8e0222d50bb9f0def1e61a0121e2edaac8eb5fd16d4233adb8560e428eada8ee5e312a2c717f5ce30826c93891988861a38ee6260
6
+ metadata.gz: e2fff6ee0202f526fb11452d3848ce5d2c0f872bb6c457ebbf8dec4f55e45c0532fbd1b8daddc0d58133814c83553ef6a9f8e063c1ef4812bdf9bd4c869ec364
7
+ data.tar.gz: 6866406cb4c5fd9c74b079c636d778171099db330309f38e13dcfd85f2adbdcd3aa2d96a453b8ad46f46fc505353d4c175b0c40a41732743da7073ce386f2c43
data/lib/kscript/cli.rb CHANGED
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'kscript'
4
- require 'io/console'
5
4
  require 'kscript/banner'
6
- require 'thor'
7
5
 
8
6
  module Kscript
9
7
  class CLI < Thor
@@ -55,7 +55,7 @@ module Kscript
55
55
  @logger.send(level, entry.to_json)
56
56
  end
57
57
 
58
- # 极客风格终端输出
58
+ # 终端输出
59
59
  def klog(level, message, context = {})
60
60
  if human_output?
61
61
  puts "[#{level.to_s.upcase}] #{message} #{context.map { |k, v| "#{k}=#{v}" }.join(' ')}".strip
@@ -7,9 +7,6 @@
7
7
 
8
8
  require 'kscript'
9
9
 
10
- require 'http'
11
- require 'json'
12
-
13
10
  module Kscript
14
11
  class KkIpUtils < Base
15
12
  IP_API_BASE_URL = 'http://ip-api.com/json'
@@ -6,12 +6,6 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'http'
10
- require 'base64'
11
- require 'rexml/document'
12
- require 'json'
13
- require 'fileutils'
14
- require 'kscript/base'
15
9
 
16
10
  module Kscript
17
11
  class KkJenkinsUtils < Base
@@ -6,7 +6,6 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'securerandom'
10
9
 
11
10
  module Kscript
12
11
  class KkKibanaUtils < Base
@@ -6,7 +6,6 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'open3'
10
9
 
11
10
  module Kscript
12
11
  class KkOptimizeUtils < Base
@@ -6,8 +6,6 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'socket'
10
- require 'timeout'
11
9
 
12
10
  module Kscript
13
11
  class KkPortscanUtils < Base
@@ -17,10 +15,11 @@ module Kscript
17
15
  # @param host [String] target host to scan
18
16
  # @param ports [Array<Integer>] list of ports to scan
19
17
  # @param thread_count [Integer] number of concurrent threads
20
- def initialize(target = nil, ports = (1..1024), *_args, **opts)
18
+ def initialize(target = nil, ports = (1..1024), thread_count = 50, *_args, **opts)
21
19
  super(**opts.merge(service: 'kk_port_scanner'))
22
20
  @target = target
23
- @ports = ports.is_a?(Range) ? ports : (1..1024)
21
+ @ports = parse_ports(ports)
22
+ @thread_count = (opts[:thread_count] || thread_count || 50).to_i
24
23
  end
25
24
 
26
25
  def run
@@ -31,23 +30,62 @@ module Kscript
31
30
 
32
31
  # Execute port scanning using multiple threads
33
32
  def scan
34
- msg = "Scanning #{@target} ports #{@ports}"
33
+ msg = "Scanning #{@target} ports #{@ports} with concurrency=#{@thread_count}"
35
34
  if human_output?
36
35
  puts msg
37
36
  else
38
37
  logger.kinfo(msg)
39
38
  end
40
- @ports.each do |port|
41
- Socket.tcp(@target, port, connect_timeout: 0.5) do |_sock|
42
- if human_output?
43
- puts "Port #{port} is open"
44
- else
45
- logger.kinfo('Port open', port: port)
46
- logger.kinfo("Port #{port} is open")
39
+ queue = Queue.new
40
+ @ports.each { |port| queue << port }
41
+ threads = []
42
+ @thread_count.times do
43
+ threads << Thread.new do
44
+ until queue.empty?
45
+ port = nil
46
+ begin
47
+ port = queue.pop(true)
48
+ rescue ThreadError
49
+ break
50
+ end
51
+ begin
52
+ Socket.tcp(@target, port, connect_timeout: 0.5) do |_sock|
53
+ if human_output?
54
+ puts "Port #{port} is open"
55
+ else
56
+ logger.kinfo('Port open', port: port)
57
+ logger.kinfo("Port #{port} is open")
58
+ end
59
+ end
60
+ rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError
61
+ # closed or filtered
62
+ end
47
63
  end
48
64
  end
49
- rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError
50
- # closed or filtered
65
+ end
66
+ threads.each(&:join)
67
+ end
68
+
69
+ # 支持多种端口参数格式: 22,80,443 或 1..1024
70
+ def parse_ports(ports)
71
+ return ports.to_a if ports.is_a?(Range)
72
+
73
+ if ports.is_a?(String)
74
+ if ports.include?(',')
75
+ ports.split(',').map(&:to_i)
76
+ elsif ports.include?('..')
77
+ begin
78
+ eval(ports).to_a
79
+ rescue StandardError
80
+ (1..1024).to_a
81
+ end
82
+ else
83
+ [ports.to_i]
84
+ end
85
+ elsif ports.is_a?(Array)
86
+ ports.map(&:to_i)
87
+ else
88
+ (1..1024).to_a
51
89
  end
52
90
  end
53
91
 
@@ -56,11 +94,11 @@ module Kscript
56
94
  end
57
95
 
58
96
  def self.arguments
59
- '<target_host>'
97
+ '<target_host> [ports] [thread_count]'
60
98
  end
61
99
 
62
100
  def self.usage
63
- "kscript portscan 192.168.1.1\nkscript portscan example.com --ports=22,80,443"
101
+ "kscript portscan 192.168.1.1\nkscript portscan example.com 22,80,443 100\nkscript portscan 192.168.1.1 1..1024 200"
64
102
  end
65
103
 
66
104
  def self.group
@@ -6,7 +6,6 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'json'
10
9
 
11
10
  module Kscript
12
11
  class KkProjscanUtils < Base
@@ -6,7 +6,6 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'nokogiri'
10
9
 
11
10
  module Kscript
12
11
  class KkShUtils < Base
@@ -6,7 +6,6 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  require 'kscript'
9
- require 'bcrypt'
10
9
 
11
10
  module Kscript
12
11
  class KkWgPassUtils < Base
data/lib/kscript/utils.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'yaml'
4
- require 'fileutils'
5
-
6
3
  module Kscript
7
4
  module Utils
8
5
  class Config
@@ -6,5 +6,5 @@
6
6
  # https://opensource.org/licenses/MIT
7
7
 
8
8
  module Kscript
9
- VERSION = '1.0.1'
9
+ VERSION = '1.0.2'
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kscript
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kk