Skip to content

Instantly share code, notes, and snippets.

View tg12's full-sized avatar
🏠
Working from home

tg12 tg12

🏠
Working from home
  • 36Hs6atY4XDwS8AK6qcSLtyVLCqX85fm1w
  • Earth
  • 20:25 (UTC +01:00)
  • LinkedIn in/jamessawyer12
View GitHub Profile
@tg12
tg12 / create_iptables_rules.py
Last active January 31, 2019 18:59
Create massive firewall with iptables
#!/usr/bin/env python
import re
import socket, struct
import datetime
ips = []
with open("merged-file3") as f:
for line in f:
@tg12
tg12 / merge_ips.py
Last active January 30, 2019 21:00
Start of a lovely script to create iptables
#!/usr/bin/env python
import re
import socket, struct
from ipwhois import IPWhois
from ipaddress import ip_network, ip_address
ips = []
with open("merged-file3") as f:
@tg12
tg12 / basic_iptables.sh
Last active January 9, 2019 20:12
Basic set of IP Table rules for Basic Functionality
# Remove previous rules
iptables -F
iptables -X
iptables -Z
# Set default policies
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -p tcp --dport 22 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
@tg12
tg12 / firehol_iptables.sh
Last active March 7, 2020 15:43
If you are a masochist, Create iptables from ALL Firehol lists
#this may take a while, run with no hup and monitor the log
rm blocklist-ipsets/ -r
git clone https://github.com/firehol/blocklist-ipsets.git
cd blocklist-ipsets/
#We just want the IP's
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" *.ipset > merged-file
#We just want the unique IP's across the board
sort -u merged-file > merged-file_output
#Just the unique ips, iptables
for IP in $(cat merged-file_output | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | awk '{print $1}' | sort | uniq); do echo "Banning $IP"; iptables -A INPUT -s $IP/32 -d 0/0 -j DROP; iptables -A INPUT -s $IP/32 -d 0/0 -j LOG --log-prefix 'firehol-iptables-rule-js'; done
@tg12
tg12 / sysadmin_script.sh
Last active March 7, 2020 15:39
Ultimate Sysadmin Script, run on fresh install of Ubuntu
#!/bin/bash
#If possible, add something in for choosing [1] Configure Basics [2] Configure Security [3] Configure VMware Tools [4] Configure All. This might require Perl.
#set -x
#read -p "Configure this server to be on 192.168.1.3/24?" yn
#while true; do
#case $yn in
# [Yy]* )
# echo "What IP address will be assigned to this server?"
@tg12
tg12 / ubuntu_privacy.txt
Created October 4, 2018 13:30
From NCSC Hardening Guide
Ubuntu Privacy
sudo systemctl stop apport.service
sudo systemctl disable apport.service
sudo systemctl mask apport.service
gsettings set com.ubuntu.update-notifier show-apport-crashes false
ubuntu-report -f send no
sudo systemctl stop whoopsie.service
sudo systemctl disable whoopsie.service
sudo systemctl mask whoopsie.service
import socket
class Server(object):
def __init__(self,host,port):
self._host = host
self._port = port
def __enter__(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
sock.bind((self._host,self._port))
@tg12
tg12 / Markov_transition.py
Created January 16, 2018 12:00
Markov transition matrix in Python
#the following code takes a list such as
#[1,1,2,6,8,5,5,7,8,8,1,1,4,5,5,0,0,0,1,1,4,4,5,1,3,3,4,5,4,1,1]
#with states labeled as successive integers starting with 0
#and returns a transition matrix, M,
#where M[i][j] is the probability of transitioning from i to j
def transition_matrix(transitions):
n = 1+ max(transitions) #number of states
M = [[0]*n for _ in range(n)]