spior 0.1.5 → 0.2.8

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/workflows/rubocop-analysis.yml +47 -0
  4. data/.gitignore +1 -0
  5. data/CHANGELOG.md +21 -0
  6. data/Gemfile +5 -0
  7. data/README.md +27 -4
  8. data/Rakefile +21 -10
  9. data/lib/spior/dep.rb +59 -0
  10. data/lib/spior/helpers.rb +19 -19
  11. data/lib/spior/iptables/default.rb +19 -13
  12. data/lib/spior/iptables/root.rb +36 -41
  13. data/lib/spior/iptables/rules.rb +103 -0
  14. data/lib/spior/iptables/tor.rb +24 -26
  15. data/lib/spior/iptables.rb +3 -0
  16. data/lib/spior/menu.rb +16 -23
  17. data/lib/spior/msg.rb +22 -12
  18. data/lib/spior/options.rb +17 -20
  19. data/lib/spior/service/enable.rb +63 -0
  20. data/lib/spior/service/restart.rb +13 -0
  21. data/lib/spior/service/start.rb +14 -0
  22. data/lib/spior/service/stop.rb +12 -0
  23. data/lib/spior/service.rb +12 -0
  24. data/lib/spior/status.rb +32 -24
  25. data/lib/spior/tor/config.rb +100 -0
  26. data/lib/spior/tor/data.rb +53 -0
  27. data/lib/spior/tor/start.rb +59 -0
  28. data/lib/spior/tor/stop.rb +32 -0
  29. data/lib/spior/tor.rb +8 -2
  30. data/lib/spior/version.rb +3 -1
  31. data/lib/spior.rb +18 -23
  32. data/man/spior.1 +53 -0
  33. data/man/spior.1.html +122 -0
  34. data/man/spior.1.ronn +46 -0
  35. data/spior.gemspec +24 -21
  36. data/test/test_install.rb +2 -2
  37. data/test/test_options.rb +2 -0
  38. data.tar.gz.sig +0 -0
  39. metadata +67 -57
  40. metadata.gz.sig +0 -0
  41. data/lib/spior/clear.rb +0 -36
  42. data/lib/spior/copy.rb +0 -85
  43. data/lib/spior/install.rb +0 -33
  44. data/lib/spior/network.rb +0 -45
  45. data/lib/spior/persist.rb +0 -51
  46. data/lib/spior/tor/info.rb +0 -113
  47. data/lib/spior/tor/restart.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52ad58e21c256642931525e2625cb10e14a74ad17ea95825940b87f6d667fdac
4
- data.tar.gz: 143940314f5a3e3387f094cdeb6c371a0e34a2227b803f9cb0eaeebc8ebb512d
3
+ metadata.gz: 57323a089d6067de5bd331a14a2471bd5b5f266135d23fa08e54df4bd23dbb4f
4
+ data.tar.gz: 63b77a80284704c7798b954a3b121862d214c369fd1784e70470b9f34fa91783
5
5
  SHA512:
6
- metadata.gz: eadbf46e6b47eb820fbd88fd3d71c31183ca49a611ac0c6e0576724abc6357d6409fbf2edc9f69d38441889f262102af47f6fecadc2fe82bcbeea856d0557dc1
7
- data.tar.gz: f774d5a4bec3474eccaf71e8495fc813cf71681fe609e7f7d1b6bf8e386d46c525bb138b4538f23cb4634706a578cbca96bac80fc85bb37c9700c99aff984ef1
6
+ metadata.gz: 81fee9fa3884a93d3465c5344156a8daa142c5e6274041f349a8cdddb5c6d51b0d0b2a71f2c16a670058510f398be22e4300038fde61848e2cc756b1a9cd4459
7
+ data.tar.gz: f1e26ab4d457c04071fe43543e4c83c6f0cfbc76eb06616e7f8ef8bdb6aea9defb1bd23e07a12ba571e8566a02b2048688e68f9ba1a7f82bb5e00d665efc66fb
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,47 @@
1
+ # pulled from repo
2
+ name: "Rubocop"
3
+
4
+ on:
5
+ push:
6
+ branches: [ develop ]
7
+ pull_request:
8
+ # The branches below must be a subset of the branches above
9
+ branches: [ develop ]
10
+ schedule:
11
+ - cron: '42 4 * * 6'
12
+
13
+ jobs:
14
+ rubocop:
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ fail-fast: false
18
+
19
+ steps:
20
+ - name: Checkout repository
21
+ uses: actions/checkout@v1
22
+
23
+ # If running on a self-hosted runner, check it meets the requirements
24
+ # listed at https://github.com/ruby/setup-ruby#using-self-hosted-runners
25
+ - name: Set up Ruby
26
+ uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: 2.6
29
+
30
+ # This step is not necessary if you add the gem to your Gemfile
31
+ - name: Install Code Scanning integration
32
+ run: bundle add code-scanning-rubocop --skip-install
33
+
34
+ - name: Install dependencies
35
+ run: bundle install
36
+
37
+ - name: Rubocop run
38
+ run: |
39
+ bash -c "
40
+ bundle exec rubocop --require code_scanning --format CodeScanning::SarifFormatter -o rubocop.sarif
41
+ [[ $? -ne 2 ]]
42
+ "
43
+
44
+ - name: Upload Sarif output
45
+ uses: github/codeql-action/upload-sarif@v2
46
+ with:
47
+ sarif_file: rubocop.sarif
data/.gitignore CHANGED
@@ -38,6 +38,7 @@ build-iPhoneSimulator/
38
38
  /_yardoc/
39
39
  /doc/
40
40
  /rdoc/
41
+ /html/
41
42
 
42
43
  ## Environment normalization:
43
44
  /.bundle/
data/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ ## 0.2.8, release 2022-09-16
2
+ * Spior used with `--clearnet` try to restore iptables rules found on your system, e.g: `/etc/iptables/iptables.rules` and `/etc/iptables/iptables.rules-backup` for Archlinux or use `Spior::Iptables::Default`.
3
+ * Stdout enhanced.
4
+ * Enhance `Spior::Dep` for install the dependencies.
5
+ * Make `Spior::Persist` work for Archlinux.
6
+ * Update `Spior::Menu`.
7
+ * Start documenting code.
8
+ * `spior --reload` make a new IP each time it called, `Spior::Service` was rewritten.
9
+ * Spior can be configured with `Spior::CONFIG` if used as library.
10
+ * Spior look options from the `/etc/tor/torrc` and use them if any.
11
+ * Add Rubocop style, fix ~300 code reports.
12
+ * Spior no longer backup/restore the file `/etc/tor/torrc`.
13
+ * Certificate update `certs/szorfein.pem`.
14
+
15
+ ## 0.1.6, release 2021-12-30
16
+ * Make it work for Voidlinux.
17
+ * Add a man page.
18
+ * Support init script (but not yet very well).
19
+ * Stop changing /etc/resolv.conf.
20
+ * Dependencies are checked before start anything. Spior exit(1) if fail.
21
+
1
22
  ## 0.1.5, release 2020-11-01
2
23
  * Simplify lib/spior/copy, lib/spior/clear
3
24
  * Write iptables rules for --clearnet and --tor
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'code-scanning-rubocop'
data/README.md CHANGED
@@ -1,15 +1,36 @@
1
- # spior
2
- (Spider|Tor) A tool to make TOR your default gateway.
1
+ # Spior
2
+
3
+ <div align="center">
4
+ <br/>
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/spior.svg)](https://badge.fury.io/rb/spior)
7
+ ![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/szorfein/spior/Rubocop/develop)
8
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
9
+ ![GitHub](https://img.shields.io/github/license/szorfein/spior)
10
+
11
+ </div>
12
+
13
+
14
+ (Spider|Tor) A tool to redirect all your local traffic to the [Tor](https://www.torproject.org/) network.
3
15
 
4
16
  ## Install
5
17
  Spior is cryptographically signed, so add my public key (if you haven’t already) as a trusted certificate.
6
18
 
7
19
  $ gem cert --add <(curl -Ls https://raw.githubusercontent.com/szorfein/spior/master/certs/szorfein.pem)
8
20
 
9
- And install the gem
21
+ And install the gem:
10
22
 
11
23
  $ gem install spior -P MediumSecurity
12
24
 
25
+ Or user wide (Spior will use `sudo`, `doas` will be supported in next release)
26
+
27
+ $ gem install --user-install spior
28
+
29
+ ## Requirements
30
+ Spior use `iptables` and `tor`, which can be installed with (if your distro is supported):
31
+
32
+ $ spior --install
33
+
13
34
  ## Usage
14
35
 
15
36
  $ spior -h
@@ -38,4 +59,6 @@ For any questions, comments, feedback or issues, submit a [new issue](https://gi
38
59
 
39
60
  ### links
40
61
  + https://rubyreferences.github.io/rubyref
41
- + https://rubystyle.guide/
62
+ + https://rubystyle.guide/
63
+ + https://gitlab.torproject.org/legacy/trac/-/wikis/doc/TransparentProxy
64
+ + https://github.com/epidemics-scepticism/writing/blob/master/misconception.md
data/Rakefile CHANGED
@@ -1,20 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # https://github.com/seattlerb/minitest#running-your-tests-
2
- require "rake/testtask"
3
- require File.dirname(__FILE__) + "/lib/spior/version"
4
+ require 'rake/testtask'
5
+ require 'rdoc/task'
6
+ require File.dirname(__FILE__) + '/lib/spior/version'
7
+
8
+ # rake rdoc
9
+ Rake::RDocTask.new('rdoc') do |rdoc|
10
+ rdoc.title = 'spior'
11
+ rdoc.options << '--line-numbers'
12
+ rdoc.main = 'README.md'
13
+ rdoc.rdoc_files.include 'lib/**/*.rb', 'README.md'
14
+ end
4
15
 
5
16
  Rake::TestTask.new(:test) do |t|
6
- t.libs << "test"
7
- t.libs << "lib"
8
- t.test_files = FileList["test/test_*.rb"]
17
+ t.libs << 'test'
18
+ t.libs << 'lib'
19
+ t.test_files = FileList['test/test_*.rb']
9
20
  end
10
21
 
11
22
  namespace :gem do
12
- desc "build the gem"
23
+ desc 'build the gem'
13
24
  task :build do
14
- Dir["spior*.gem"].each {|f| File.unlink(f) }
15
- system("gem build spior.gemspec")
16
- system("gem install spior-#{Spior::VERSION}.gem -P MediumSecurity")
25
+ Dir['spior*.gem'].each { |f| File.unlink(f) }
26
+ system('gem build spior.gemspec')
27
+ system("gem install --user-install spior-#{Spior::VERSION}.gem -P MediumSecurity")
17
28
  end
18
29
  end
19
30
 
20
- task :default => :test
31
+ task default: :test
data/lib/spior/dep.rb ADDED
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nomansland'
4
+ require 'tty-which'
5
+
6
+ module Spior
7
+ # Dep: install all dependencies for Spior
8
+ module Dep
9
+ extend self
10
+
11
+ def looking
12
+ case Nomansland.distro?
13
+ when :archlinux
14
+ Msg.p 'Looking dependencies for Archlinux...'
15
+ installing_deps(%w[iptables tor])
16
+ when :debian
17
+ Msg.p 'Looking dependencies for Debian...'
18
+ installing_deps(%w[iptables tor])
19
+ when :gentoo
20
+ Msg.p 'Looking dependencies for Gentoo...'
21
+ installing_deps(%w[iptables tor])
22
+ when :void
23
+ Msg.p 'Looking dependencies for Voidlinux...'
24
+ installing_deps(%w[iptables tor])
25
+ else
26
+ Msg.report 'Install for your distro is not yet supported.'
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ def installing_deps(names)
33
+ names.map do |n|
34
+ install(n) unless search_dep(n)
35
+ end
36
+ Msg.p 'Dependencies are OK.'
37
+ end
38
+
39
+ def install(name)
40
+ Msg.p "Installing #{name}..."
41
+ case Nomansland.installer?
42
+ when :apt_get
43
+ Helpers::Exec.new('apt-get').run("install #{name}")
44
+ when :emerge
45
+ Helpers::Exec.new('emerge').run("-av #{name}")
46
+ when :pacman
47
+ Helpers::Exec.new('pacman').run("-S #{name}")
48
+ when :void
49
+ Helpers::Exec.new('xbps-install').run("-y #{name}")
50
+ when :yum
51
+ Helpers::Exec.new('yum').run("install #{name}")
52
+ end
53
+ end
54
+
55
+ def search_dep(name)
56
+ TTY::Which.exist?(name) ? true : false
57
+ end
58
+ end
59
+ end
data/lib/spior/helpers.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'tempfile'
3
5
  require 'open3'
@@ -10,8 +12,8 @@ module Helpers
10
12
  end
11
13
 
12
14
  def run(args)
13
- cmd = @search_uid == '0' ? @name : "sudo #{@name}"
14
- Open3.popen2e("#{cmd} #{args}") do |stdin, stdout_err, wait_thr|
15
+ cmd = (@search_uid == '0' ? @name : "sudo #{@name}")
16
+ Open3.popen2e("#{cmd} #{args}") do |_, stdout_err, wait_thr|
15
17
  while line = stdout_err.gets
16
18
  puts line
17
19
  end
@@ -38,27 +40,25 @@ module Helpers
38
40
  # * _string_ = string for the whole file
39
41
  # * _name_ = name of the file (e.g: resolv.conf)
40
42
  # * _dest_ = path (e.g: /etc)
41
- def initialize(string, name, dest = "/tmp")
43
+ def initialize(string, name, dest = '/tmp')
42
44
  @string = string
43
45
  @name = name
44
- @dest = dest + "/" + @name
46
+ @dest = "#{dest}/#{@name}"
45
47
  end
46
48
 
47
49
  # Method #add
48
50
  # Add the file at @dest
49
51
  def add
50
- @mv = Helpers::Exec.new("mv")
52
+ @mv = Helpers::Exec.new('mv')
51
53
  tmp = Tempfile.new(@name)
52
- File.open(tmp.path, 'w') do |file|
53
- file.puts @string
54
- end
54
+ File.write tmp.path, "#{@string}\n"
55
55
  puts "move #{tmp.path} to #{@dest}"
56
56
  @mv.run("#{tmp.path} #{@dest}")
57
57
  end
58
58
 
59
59
  def perm(user, perm)
60
- chown = Helpers::Exec.new("chown")
61
- chmod = Helpers::Exec.new("chmod")
60
+ chown = Helpers::Exec.new('chown')
61
+ chmod = Helpers::Exec.new('chmod')
62
62
  chown.run("#{user}:#{user} #{@dest}")
63
63
  chmod.run("#{perm} #{@dest}")
64
64
  end
@@ -88,30 +88,30 @@ module Helpers
88
88
  def initialize(string, name)
89
89
  super
90
90
  @systemd_dir = search_systemd_dir
91
- @dest = @systemd_dir + "/" + @name
91
+ @dest = "#{@systemd_dir}/#{@name}"
92
92
  end
93
93
 
94
94
  # Method #add
95
95
  # Create a temporary file and move
96
96
  # the service @name to the systemd directory
97
97
  def add
98
- @systemctl = Helpers::Exec.new("systemctl")
98
+ @systemctl = Helpers::Exec.new('systemctl')
99
99
  super
100
- @systemctl.run("daemon-reload")
100
+ @systemctl.run('daemon-reload')
101
101
  end
102
102
 
103
103
  private
104
+
104
105
  # Method search_systemd_dir
105
106
  # Search the current directory for systemd services
106
107
  # + Gentoo can install at /lib/systemd/system or /usr/lib/systemd/system
107
108
  def search_systemd_dir
108
- if Dir.exist? "/lib/systemd/system"
109
- "/lib/systemd/system"
110
- elsif Dir.exist? "/usr/lib/systemd/system"
111
- "/usr/lib/systemd/system"
109
+ if Dir.exist? '/lib/systemd/system'
110
+ '/lib/systemd/system'
111
+ elsif Dir.exist? '/usr/lib/systemd/system'
112
+ '/usr/lib/systemd/system'
112
113
  else
113
- raise "No directory systemd found"
114
- exit
114
+ raise 'No directory systemd found'
115
115
  end
116
116
  end
117
117
  end
@@ -1,37 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Spior
2
4
  module Iptables
5
+ # Default and generic Iptables rules when Tor is not used.
6
+ #
7
+ # Allowed ports:
8
+ # * Input 22: for ssh connection
3
9
  class Default < Iptables::Root
4
10
  private
5
-
11
+
6
12
  def input
7
13
  # SSH
8
- ipt "-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT"
14
+ ipt '-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT'
9
15
  # Allow loopback, rules
10
16
  ipt "-A INPUT -i #{@lo} -j ACCEPT"
11
17
  # Accept related
12
- ipt "-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT"
18
+ ipt '-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT'
13
19
  end
14
20
 
15
21
  def output
16
- ipt "-A OUTPUT -m conntrack --ctstate INVALID -j DROP"
17
- ipt "-A OUTPUT -m state --state ESTABLISHED -j ACCEPT"
22
+ ipt '-A OUTPUT -m conntrack --ctstate INVALID -j DROP'
23
+ ipt '-A OUTPUT -m state --state ESTABLISHED -j ACCEPT'
18
24
 
19
25
  # Allow SSH
20
- ipt "-A OUTPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT"
26
+ ipt '-A OUTPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT'
21
27
 
22
28
  # Allow Loopback
23
29
  ipt "-A OUTPUT -d #{@lo_addr}/8 -o #{@lo} -j ACCEPT"
24
30
 
25
31
  # Default
26
- ipt "-A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT"
32
+ ipt '-A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT'
27
33
  end
28
-
34
+
29
35
  def all
30
- ipt "-t filter -A OUTPUT -p udp -j ACCEPT"
31
- ipt "-t filter -A OUTPUT -p icmp -j REJECT"
32
- ipt "-P INPUT ACCEPT"
33
- ipt "-P FORWARD ACCEPT"
34
- ipt "-P OUTPUT ACCEPT"
36
+ ipt '-t filter -A OUTPUT -p udp -j ACCEPT'
37
+ ipt '-t filter -A OUTPUT -p icmp -j REJECT'
38
+ ipt '-P INPUT ACCEPT'
39
+ ipt '-P FORWARD ACCEPT'
40
+ ipt '-P OUTPUT ACCEPT'
35
41
  end
36
42
  end
37
43
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'interfacez'
2
4
 
3
5
  module Spior
@@ -6,11 +8,12 @@ module Spior
6
8
  def initialize
7
9
  @lo = Interfacez.loopback
8
10
  @lo_addr = Interfacez.ipv4_address_of(@lo)
9
- @i = Helpers::Exec.new("iptables")
10
- Spior::Copy.new.save
11
+ @i = Helpers::Exec.new('iptables')
12
+ @debug = false
11
13
  end
12
14
 
13
15
  def run!
16
+ stop!
14
17
  bogus_tcp_flags
15
18
  bad_packets
16
19
  spoofing
@@ -20,69 +23,61 @@ module Spior
20
23
  all
21
24
  end
22
25
 
23
- def restart!
24
- stop!
25
- run!
26
- end
27
-
28
26
  def stop!
29
- ipt "-F"
30
- ipt "-X"
31
- ipt "-t nat -F"
32
- ipt "-t nat -X"
33
- ipt "-t mangle -F"
34
- ipt "-t mangle -X"
27
+ Msg.p 'Clearing Iptables rules...'
28
+ ipt '-F'
29
+ ipt '-X'
30
+ ipt '-t nat -F'
31
+ ipt '-t nat -X'
32
+ ipt '-t mangle -F'
33
+ ipt '-t mangle -X'
35
34
  end
36
35
 
37
36
  private
38
37
 
39
38
  def ipt(line)
40
39
  @i.run("#{line}")
41
- puts "added - #{@i} #{line}"
40
+ puts "Added - iptables #{line}" if @debug
42
41
  end
43
42
 
44
- def redirect
45
- end
43
+ def redirect; end
46
44
 
47
- def input
48
- end
45
+ def input; end
49
46
 
50
- def output
51
- end
47
+ def output; end
52
48
 
53
- def all
54
- end
49
+ def all; end
55
50
 
56
51
  def bogus_tcp_flags
57
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP"
58
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP"
59
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP"
60
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP"
61
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP"
62
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP"
63
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ACK,FIN FIN -j DROP"
64
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP"
65
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ALL ALL -j DROP"
66
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP"
67
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP"
68
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP"
69
- ipt "-t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP"
52
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP'
53
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP'
54
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP'
55
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP'
56
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP'
57
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP'
58
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ACK,FIN FIN -j DROP'
59
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP'
60
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ALL ALL -j DROP'
61
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP'
62
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP'
63
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP'
64
+ ipt '-t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP'
70
65
  end
71
66
 
72
67
  def bad_packets
73
68
  # new packet not syn
74
- ipt "-t mangle -A PREROUTING -p tcp ! --syn -m conntrack --ctstate NEW -j DROP"
69
+ ipt '-t mangle -A PREROUTING -p tcp ! --syn -m conntrack --ctstate NEW -j DROP'
75
70
  # fragment packet
76
- ipt "-A INPUT -f -j DROP"
71
+ ipt '-A INPUT -f -j DROP'
77
72
  # XMAS
78
- ipt "-A INPUT -p tcp --tcp-flags ALL ALL -j DROP"
73
+ ipt '-A INPUT -p tcp --tcp-flags ALL ALL -j DROP'
79
74
  # null packet
80
- ipt "-A INPUT -p tcp --tcp-flags ALL NONE -j DROP"
75
+ ipt '-A INPUT -p tcp --tcp-flags ALL NONE -j DROP'
81
76
  end
82
77
 
83
78
  def spoofing
84
- subs=["224.0.0.0/3", "169.254.0.0/16", "172.16.0.0/12", "192.0.2.0/24", "0.0.0.0/8", "240.0.0.0/5"]
85
- subs.each do |sub|
79
+ subs = %w[224.0.0.0/3 169.254.0.0/16 172.16.0.0/12 192.0.2.0/24 0.0.0.0/8 240.0.0.0/5]
80
+ subs.map do |sub|
86
81
  ipt "-t mangle -A PREROUTING -s #{sub} -j DROP"
87
82
  end
88
83
  ipt "-t mangle -A PREROUTING -s #{@lo_addr}/8 ! -i #{@lo} -j DROP"
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+ require 'fileutils'
5
+ require 'nomansland'
6
+
7
+ module Spior
8
+ module Iptables
9
+ class Rules
10
+ def initialize
11
+ @tmp_iptables_rules = Tempfile.new('iptables_rules')
12
+ @tmp_spior_rules = Tempfile.new('spior_rules')
13
+ @rules_path = search_iptables_config
14
+ end
15
+
16
+ def backup
17
+ save_rules(@tmp_iptables_rules)
18
+ insert_comment(@tmp_spior_rules, @tmp_iptables_rules)
19
+ create_file(@tmp_spior_rules, @rules_path)
20
+ end
21
+
22
+ def restore
23
+ unless restoring_older_rules(@rules_path)
24
+ Msg.p 'Adding clearnet navigation...'
25
+ Iptables::Default.new.run!
26
+ end
27
+ end
28
+
29
+ protected
30
+
31
+ def save_rules(tmp_file)
32
+ Msg.p 'Saving Iptables rules...'
33
+ Helpers::Exec.new('iptables-save').run("> #{tmp_file.path}")
34
+ end
35
+
36
+ def insert_comment(spior_file, iptable_file)
37
+ outfile = File.open(spior_file.path, 'w')
38
+ outfile.puts '# Rules saved by Spior.'
39
+ outfile.puts(File.read(iptable_file.path))
40
+ outfile.close
41
+ end
42
+
43
+ def search_for_comment(filename)
44
+ File.open(filename) do |f|
45
+ f.each do |line|
46
+ return true if line.match(/saved by Spior/)
47
+ end
48
+ end
49
+ false
50
+ end
51
+
52
+ def move(src, dest)
53
+ if Process::Sys.getuid == '0'
54
+ FileUtils.mv(src, dest)
55
+ else
56
+ Helpers::Exec.new('mv').run("#{src} #{dest}")
57
+ end
58
+ end
59
+
60
+ def create_file(tmpfile, dest)
61
+ if File.exist? dest
62
+ if search_for_comment(dest)
63
+ Msg.p "Older Spior rules found #{dest}, erasing..."
64
+ else
65
+ Msg.p "File exist #{dest}, create backup #{dest}-backup..."
66
+ move(dest, "#{dest}-backup")
67
+ end
68
+ end
69
+ move(tmpfile.path, dest)
70
+ end
71
+
72
+ def restoring_older_rules(filename)
73
+ files = %W[#{filename} #{filename}-backup]
74
+ files.each do |f|
75
+ next unless File.exist? f
76
+
77
+ unless search_for_comment(f)
78
+ Iptables::Root.new.stop!
79
+ Msg.p "Found older rules #{f}, restoring..."
80
+ Helpers::Exec.new('iptables-restore').run(f)
81
+ return true
82
+ end
83
+ end
84
+ false
85
+ end
86
+
87
+ private
88
+
89
+ def search_iptables_config
90
+ case Nomansland.distro?
91
+ when :archlinux || :void
92
+ '/etc/iptables/iptables.rules'
93
+ when :debian
94
+ '/etc/iptables.up.rules'
95
+ when :gentoo
96
+ '/var/lib/iptables/rules-save'
97
+ else
98
+ Msg.report 'I don`t know where you distro save the rules for iptables yet'
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end