Archives par mot-clé : script

Go get and Fork – script helper workaround

Why

If you write some Golang code, from my point of view, one of biggest pitfall of this language is dependency management.

At the beginning, get library directly from git repository seems a good idea, but between package rules, versions, your pull requests, it becomes a mess quickly.

Some sources on this problem:
Article: Forking Golang repositories on GitHub and managing the import path

StackOverflow : Using forked package import in Go

My workaround

Then, why not automate this procedure ?
In your .bashrc add:

function gofork() {
  if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
    echo 'Usage: gofork yourFork originalModule'
    echo 'Example: golang github.com/YourName/go-contrib github.com/heirko/go-contrib'
    return
  fi
   echo "Go get fork $1 and replace $2 in GOPATH: $GOPATH"
   go get $1
   go get $2
   currentDir=$PWD
   cd $GOPATH/src/$1
   remote1=$(git config --get remote.origin.url)
   cd $GOPATH/src/$2
   remote2=$(git config --get remote.origin.url)
   cd $currentDir
   rm -rf $GOPATH/src/$2
   mv $GOPATH/src/$1 $GOPATH/src/$2
   cd $GOPATH/src/$2
   git remote add their $remote2
   echo Now in $GOPATH/src/$2 origin remote is $remote1
   echo And in $GOPATH/src/$2 their remote is $remote2
   cd $currentDir
}

export -f gofork

This command do

  • a ‘go get’ on source and fork repositories
  • extract remote urls
  • replace source code by fork
  • add source remote to your fork repository in case you want pull from source

You can call this script from command line or directly from a script like this:

gofork github.com/heralight/baloo gopkg.in/h2non/baloo.v2

DEBIAN: Créer des paquets debian vides pour palier aux problèmes de dépendances

Comme beaucoup, je suis sous Ubuntu.

Cet OS est très bien conçu, par contre certains paquets ont quelques fois des dépendances un peu farfelues, voir qui vous bouffent des ressources inutilement.

Si comme moi, vous n’utilisez pas le bluetooth, le supprimer est difficile.
Il faudrait, entre autre, se priver de unity-desktop…

Bref, fin du Blabla…

Pour forcer la suppression d’un paquet sans désinstaller les paquets en dépendant:

$ sudo dpkg -r --force-depends package

Par exemple:

$ sudo dpkg -r --force-depends rfkill gnome-bluetooth bluez bluez-alsa gnome-orca network-manager-pptp network-manager-pptp-gnome speech-dispatcher indicator-bluetooth

Maintenant vous avez plein de paquets cassés… pas bien Alexandre!

Pour remédier à ce problème, une solution consiste à installer des paquets résolvant ces dépendances perdues, mais ces derniers seront vides, en gros des « dummy packages ».

Pour créer ces paquets, vous devrez par exemple utiliser equivs-control et equivs-build ou mon script.

La commande:

$ ./gen-dummy-package.sh --install|i [packageName]+
# Par exemple:
$ ./gen-dummy-package.sh -i rfkill nome-bluetooth bluez

Le « -i » va effectuer une installation du paquet nouvellement créé.

Le script complet:

Port Knocking – Sécurisez votre réseau – Tomato firmware

Si vous cherchez à faire du port knocking sur votre routeur asus avec un firmware Tomato, je vous conseil la lecture de ce blog.

Article très bien fait, mais je vais vous le résumé pour avoir un forward d’un port ssh d’une de vos machines internes vers le routeur puis votre box internet.
Pour plus de détails et d’information de débuggage, référez-vous à ce blog.

But

Machine1
Ssh Port 22
|
Routeur Tomato
28322 + Port Knocking
|
WAN

Howto

Dans Administration->Scripts->Firewall

Ajouter

KNOCK_STEP_TIMEOUT_SEC=25
KNOCK_WEB_ADMIN_TIMEOUT_SEC=300
KNOCK_INTERFACE="br0"
KNOCK_PORT1=28350
KNOCK_PORT2=27334
KNOCK_PORT3=29837
KNOCK_HONEY_PORT_SEQ=4049,28051,27024,28026,29074,15076


 
# Load iptables modules
modprobe xt_recent
 
# Knock chains
iptables -t nat -N knock2 2>/dev/null
iptables -t nat -F knock2
iptables -t nat -A knock2 -m recent --name knock1 --remove
iptables -t nat -A knock2 -m recent --name knock2 --set
iptables -t nat -A knock2 -j LOG --log-level info --log-prefix "IN KNOCK2: "
 
iptables -t nat -N knock3 2>/dev/null
iptables -t nat -F knock3
iptables -t nat -A knock3 -m recent --name knock2 --remove
iptables -t nat -A knock3 -m recent --name knock3 --set
iptables -t nat -A knock3 -j LOG --log-level info --log-prefix "IN KNOCK3: "
 
iptables -t nat -N knock_deny 2>/dev/null
iptables -t nat -F knock_deny
iptables -t nat -A knock_deny -m recent --name knock1 --remove
iptables -t nat -A knock_deny -m recent --name knock2 --remove
iptables -t nat -A knock_deny -m recent --name knock3 --remove
iptables -t nat -A knock_deny -j LOG --log-level warn --log-prefix "KNOCK DENIED: "
 
iptables -t nat -N knock_scanned 2>/dev/null
iptables -t nat -F knock_scanned
iptables -t nat -A knock_scanned -m recent --rcheck --name knock1 \
 --seconds $KNOCK_STEP_TIMEOUT_SEC -j knock_deny
iptables -t nat -A knock_scanned -m recent --rcheck --name knock2 \
 --seconds $KNOCK_STEP_TIMEOUT_SEC -j knock_deny
iptables -t nat -A knock_scanned -m recent --rcheck --name knock3 \
 --seconds $KNOCK_STEP_TIMEOUT_SEC -j knock_deny
 
# 1st knock: KNOCK_PORT1
iptables -t nat -A PREROUTING -i $KNOCK_INTERFACE -p tcp --dport $KNOCK_PORT1 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --set --name knock1
 
# 2nd knock: KNOCK_PORT2
iptables -t nat -A PREROUTING -i $KNOCK_INTERFACE -p tcp --dport $KNOCK_PORT2 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --rcheck --name knock1 --seconds $KNOCK_STEP_TIMEOUT_SEC -j knock2
 
# 3rd knock: KNOCK_PORT3
iptables -t nat -A PREROUTING -i $KNOCK_INTERFACE -p tcp --dport $KNOCK_PORT3 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --rcheck --name knock2 --seconds $KNOCK_STEP_TIMEOUT_SEC -j knock3
 
# To stop port-scans from randomly finding the sequence
iptables -t nat -A PREROUTING -i $KNOCK_INTERFACE -p tcp \
 -m multiport --destination-port $KNOCK_HONEY_PORT_SEQ \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN -j knock_scanned

###############################################################################
# Port forwards after knock
 
# forward port 3124 to ssh of 192.168.1.10:22
iptables -A wanin -i $KNOCK_INTERFACE -p tcp --dport 3124 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --rcheck --seconds $KNOCK_STEP_TIMEOUT_SEC --name knock3 \
 -j ACCEPT
 
iptables -t nat -A PREROUTING -i $KNOCK_INTERFACE -p tcp --dport 3124 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --rcheck --seconds $KNOCK_STEP_TIMEOUT_SEC --name knock3 \
 -j DNAT --to-destination 192.168.1.10:22
 
# FTP
iptables -A wanin -i $KNOCK_INTERFACE -p tcp --dport 21 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --update --seconds $KNOCK_WEB_ADMIN_TIMEOUT_SEC --name knock3 \
 -j ACCEPT
 
iptables -t nat -A PREROUTING -i $KNOCK_INTERFACE -p tcp --dport 21 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --update --seconds $KNOCK_WEB_ADMIN_TIMEOUT_SEC --name knock3 \
 -j DNAT --to-destination 192.168.1.10
 
###############################################################################
# Ports open from WAN to this router after knock
 

# SSH on local router (ssh port has been changed to 3123)
iptables -A INPUT -d 192.168.1.1 -p tcp --dport 3123 \
 -j ACCEPT
 
iptables -t nat -A PREROUTING -i $KNOCK_INTERFACE -p tcp --dport 3123 \
 -m state --state NEW --tcp-flags SYN,RST,ACK SYN \
 -m recent --rcheck --seconds $KNOCK_STEP_TIMEOUT_SEC --name knock3 \
 -j DNAT --to-destination 192.168.1.1

Adapter les paramètres suivant à vos besoins.

interface réseau recevant le flux d’ouverture
KNOCK_INTERFACE= »br0″
knock port 1
KNOCK_PORT1=28350
knock port 2
KNOCK_PORT2=27334
knock port 3
KNOCK_PORT3=29837
une séquence permettant de perturber les scanners de ports
KNOCK_HONEY_PORT_SEQ=4049,28051,27024,28026,29074,15076

pour tester:

// test - port fermé
~$ ssh [email protected] -p 3124
ssh: connect to host 192.168.1.1 port 3124: Connection refused

// ouverture par port knocking
~$ ssh [email protected] -p 28350
ssh: connect to host 192.168.1.1 port 28350: Connection refused
~$ ssh [email protected] -p 27334
ssh: connect to host 192.168.1.1 port 27334: Connection refused
~$ ssh [email protected] -p 29837
ssh: connect to host 192.168.1.1 port 29837: Connection refused

~$ ssh [email protected] -p 3124
[email protected]'s password: 
// OK -port ouvert !!
// Ctrl+c + attendre 30s

~$ ssh [email protected] -p 3124
ssh: connect to host 192.168.1.1 port 3124: Connection refused
// port fermé - timeout OK

[EDIT 2016] Actuellement je n’utilise plus le port knocking mais le « Single Packet authentication » car plus sécurisé, un article bientôt.