Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 392bcb06fe | |||
| ce8e316a59 | |||
| f24c2bbdc4 | |||
| c549d97884 | |||
| 61a36a11c0 | |||
| 66c94e096b | |||
| 779c76dbde |
39
0eval.yml
39
0eval.yml
@@ -1,39 +0,0 @@
|
|||||||
---
|
|
||||||
- hosts: b1
|
|
||||||
user: root
|
|
||||||
gather_facts: false
|
|
||||||
vars_prompt:
|
|
||||||
- name: script
|
|
||||||
prompt: "script? "
|
|
||||||
private: false
|
|
||||||
- name: result_file
|
|
||||||
prompt: "result file ? "
|
|
||||||
private: false
|
|
||||||
|
|
||||||
pre_tasks:
|
|
||||||
- name: insert header
|
|
||||||
lineinfile:
|
|
||||||
path: "{{ result_file }}"
|
|
||||||
line: "machine\tuser\tquestions.."
|
|
||||||
state: present
|
|
||||||
create: true
|
|
||||||
delegate_to: localhost
|
|
||||||
|
|
||||||
tasks:
|
|
||||||
- name: copy script
|
|
||||||
copy:
|
|
||||||
src: "{{ script }}"
|
|
||||||
dest: /root
|
|
||||||
mode: 0700
|
|
||||||
|
|
||||||
- name: exec script
|
|
||||||
command: "/root/{{ script }}"
|
|
||||||
register: result
|
|
||||||
|
|
||||||
post_tasks:
|
|
||||||
- name: get result
|
|
||||||
lineinfile:
|
|
||||||
path: "{{ result_file }}"
|
|
||||||
line: "{{ result.stdout }}"
|
|
||||||
create: true
|
|
||||||
delegate_to: localhost
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
## Sur votre windows dans un terminal
|
|
||||||
ssh-keygen
|
|
||||||
scp -P #machine .ssh\.id_rsa.pub epsi@cours.opendoor.fr:
|
|
||||||
|
|
||||||
## Sur le container connecté en tant qu'utilisateur epsi
|
|
||||||
|
|
||||||
mkdir -pm 0700 ~/.ssh
|
|
||||||
cat id_rsa.pub >> .ssh/authorized_keys
|
|
||||||
chmod 600 .ssh/authorized_keys
|
|
||||||
|
|
||||||
sudo vim /etc/ssh/sshd_config
|
|
||||||
|
|
||||||
...
|
|
||||||
PermitRootLogin without-password
|
|
||||||
...
|
|
||||||
AllowGroups wheel root
|
|
||||||
AllowUsers root@100.0.0.1
|
|
||||||
PasswordAuthentication no
|
|
||||||
|
|
||||||
sudo systemctl restart sshd
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
better run eval playbook with -f 1 to avoid result overwriting
|
|
||||||
@@ -6,21 +6,21 @@ Renforcer la sécurité des accès
|
|||||||
|
|
||||||
## Prérequis:
|
## Prérequis:
|
||||||
|
|
||||||
Le compte epsi doit avoir été créé (tp prise en main) et disposer d'un mot de passe
|
Le compte formation doit avoir été créé (tp prise en main) et disposer d'un mot de passe
|
||||||
|
|
||||||
|
|
||||||
## Authentification par clé
|
## Authentification par clé
|
||||||
|
|
||||||
On va faire en sorte que la connexion ssh en tant qu'utilisateur epsi
|
On va faire en sorte que la connexion ssh en tant qu'utilisateur formation
|
||||||
se fasse par clé publique et non pas par mot de passe.
|
se fasse par clé publique et non pas par mot de passe.
|
||||||
|
|
||||||
_sur votre machine_: utiliser la commande ssh-keygen (ou le programme puttygen) si votre windows n'est pas équipé d'un client ssh natif pour générer une paire de clé publique / clé privée.
|
_sur votre machine_: utiliser la commande ssh-keygen (ou le programme puttygen) si votre windows n'est pas équipé d'un client ssh natif pour générer une paire de clé publique / clé privée.
|
||||||
|
|
||||||
_sur le container_: il faut rajouter la clé publique (le contenu du fichier id_rsa.pub se trouvant sur *votre* machine) dans le fichier *~epsi/.ssh/authorized_keys*
|
_sur le container_: il faut rajouter la clé publique (le contenu du fichier id_rsa.pub se trouvant sur *votre* machine) dans le fichier *~formation/.ssh/authorized_keys*
|
||||||
|
|
||||||
Il sera peut-être nécessaire de créer le répertoire ~epsi/.ssh
|
Il sera peut-être nécessaire de créer le répertoire ~formation/.ssh
|
||||||
|
|
||||||
Attention au permissions: ce répertoire doit appartenir à epsi et avoir les permissions 700 le fichier authorized_keys doit appartenir à epsi et avoir les permissions 600
|
Attention au permissions: ce répertoire doit appartenir à formation et avoir les permissions 700 le fichier authorized_keys doit appartenir à formation et avoir les permissions 600
|
||||||
|
|
||||||
### Validation:
|
### Validation:
|
||||||
|
|
||||||
@@ -41,6 +41,6 @@ Une fois que l'authentification par clé fonctionne, modifiez la configuration d
|
|||||||
|
|
||||||
### Validation:
|
### Validation:
|
||||||
|
|
||||||
Vous devez toujours pouvoir vous connecter en tant qu'utilisateur epsi
|
Vous devez toujours pouvoir vous connecter en tant qu'utilisateur formation
|
||||||
|
|
||||||
La connexion en root doit être refusée
|
La connexion en root doit être refusée
|
||||||
12
eval.sh
12
eval.sh
@@ -1,12 +0,0 @@
|
|||||||
#! /bin/bash
|
|
||||||
# this script is meant to be run by ansible
|
|
||||||
|
|
||||||
source functions.sh
|
|
||||||
# check that alias has been tried
|
|
||||||
addHeader
|
|
||||||
fileMustContain ~epsi/.ssh/authorized_keys 'ssh-rsa'
|
|
||||||
fileMustContain ~epsi/.ssh/authorized_keys 'tom@workine'
|
|
||||||
fileMustContain /etc/ssh/sshd_config ' Allow(Groups|Users)'
|
|
||||||
fileMustContain /etc/ssh/sshd_config "^PermitRootLogin no"
|
|
||||||
fileMustContain /etc/ssh/sshd_config "^PasswordAuthentication no"
|
|
||||||
|
|
||||||
36
eval.yml
36
eval.yml
@@ -1,36 +0,0 @@
|
|||||||
---
|
|
||||||
- hosts:
|
|
||||||
- b2
|
|
||||||
gather_facts: false
|
|
||||||
vars:
|
|
||||||
result_file: "tp_ssh.csv"
|
|
||||||
|
|
||||||
pre_tasks:
|
|
||||||
- name: insert header
|
|
||||||
lineinfile:
|
|
||||||
path: "{{ result_file }}"
|
|
||||||
line: "machine\tuser\tauth keys etu\tauth key tom\tallow groups/user\tpermit root login\tpassword auth"
|
|
||||||
state: present
|
|
||||||
create: true
|
|
||||||
delegate_to: localhost
|
|
||||||
|
|
||||||
tasks:
|
|
||||||
- name: copy script
|
|
||||||
copy:
|
|
||||||
src: "{{ item }}"
|
|
||||||
dest: /root
|
|
||||||
mode: 0700
|
|
||||||
loop:
|
|
||||||
- functions.sh
|
|
||||||
- eval.sh
|
|
||||||
|
|
||||||
- name: exec script
|
|
||||||
command: /root/eval.sh
|
|
||||||
register: result
|
|
||||||
|
|
||||||
- name: get result
|
|
||||||
lineinfile:
|
|
||||||
path: "{{ result_file }}"
|
|
||||||
line: "{{ result.stdout }}"
|
|
||||||
create: true
|
|
||||||
delegate_to: localhost
|
|
||||||
163
functions.sh
163
functions.sh
@@ -1,163 +0,0 @@
|
|||||||
#! /bin/bash
|
|
||||||
|
|
||||||
#ok if given program returns given code
|
|
||||||
# arg1: program to run
|
|
||||||
# arg2: expected return code (default 0)
|
|
||||||
function exitCodeOk {
|
|
||||||
program=$1
|
|
||||||
code=${2:=0}
|
|
||||||
$program &> /dev/null
|
|
||||||
if [ $? -eq $code ] ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
#Ok if file size > X
|
|
||||||
# arg1: file
|
|
||||||
# arg2: min size
|
|
||||||
function fileBiggerThan {
|
|
||||||
if [ ! -f $1 ] ; then echo -ne "0\t" ; return 0 ; fi
|
|
||||||
filesize=$(stat --format "%s" $1)
|
|
||||||
if [ $filesize -lt $2 ] ; then echo -ne "0\t" ; else echo -ne "1\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
#Ok if file exists
|
|
||||||
# arg1: file to check
|
|
||||||
function fileMustExist {
|
|
||||||
if [ -f ${1} ] ; then echo -ne "1\t"
|
|
||||||
else echo -ne "0\t"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
#Ok if file DOESNOT exists
|
|
||||||
# arg1: file to check
|
|
||||||
function fileMustNOTExist {
|
|
||||||
if [ -f ${1} ] ; then echo -ne "0\t"
|
|
||||||
else echo -ne "1\t"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
#Ok if dir exists
|
|
||||||
# arg1: dir to check
|
|
||||||
function dirMustExist {
|
|
||||||
if [ -d ${1} ] ; then echo -ne "1\t"
|
|
||||||
else echo -ne "0\t"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
# Ok if given dir DOESNOT exists
|
|
||||||
# arg1: dir
|
|
||||||
function dirMustNOTExist {
|
|
||||||
if [ -d ${1} ] ; then echo -ne "0\t"
|
|
||||||
else echo -ne "1\t"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Ok if file contains at least one occurence of pattern
|
|
||||||
# arg1: file
|
|
||||||
# arg2: pattern
|
|
||||||
function fileMustContain {
|
|
||||||
file=$1
|
|
||||||
shift
|
|
||||||
string=$@
|
|
||||||
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi
|
|
||||||
if ( grep -i -qE "${string}" $file ) ;
|
|
||||||
then echo -ne "1\t"
|
|
||||||
else
|
|
||||||
echo -ne "0\t"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Return num of occurences of pattern
|
|
||||||
# arg1: file
|
|
||||||
# arg2: pattern
|
|
||||||
function fileCountPattern {
|
|
||||||
file=$1
|
|
||||||
shift
|
|
||||||
string=$@
|
|
||||||
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi
|
|
||||||
num=$(grep -ciE "${string}" $file )
|
|
||||||
echo -ne "$num\t"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Ok if file contains N pattern
|
|
||||||
# arg1: file
|
|
||||||
# arg2: num of expected item
|
|
||||||
# arg3: pattern
|
|
||||||
function fileMustContainNItem {
|
|
||||||
file=$1
|
|
||||||
n=$2
|
|
||||||
shift
|
|
||||||
string=$@
|
|
||||||
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi
|
|
||||||
num=$(grep -ciE "${string}" $file )
|
|
||||||
if [ $num -eq $1 ] ;
|
|
||||||
then echo -ne "1\t"
|
|
||||||
else
|
|
||||||
echo -ne "0\t"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Ok if file DOESNOT contain pattern
|
|
||||||
# Arg1: file
|
|
||||||
# ArgN: pattern
|
|
||||||
function fileMustNOTContain {
|
|
||||||
file=$1
|
|
||||||
shift
|
|
||||||
string=$@
|
|
||||||
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ; fi
|
|
||||||
if ( grep -i -qE "${string}" $file ) ;
|
|
||||||
then echo -ne "0\t"
|
|
||||||
else
|
|
||||||
echo -ne "1\t"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# insert hostname at beginning of result line
|
|
||||||
function addHeader {
|
|
||||||
test -f /etc/motd && user="$(cat /etc/motd | sed 's/\n//')\t"
|
|
||||||
echo -ne "${user}$(hostname -s)\t"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Ok if given package is installed
|
|
||||||
# arg1: pkg to check
|
|
||||||
function pkgInstalled {
|
|
||||||
pkglist=/tmp/pkg.list
|
|
||||||
test -f $pkglist || rpm -qa > $pkglist
|
|
||||||
if ( grep -qi $1 $pkglist ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# ok if given process is running
|
|
||||||
# arg1: process to check
|
|
||||||
function processIsRunning {
|
|
||||||
if ( pgrep -f $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
#Ok if given command returns 0
|
|
||||||
# arg1: command to be run
|
|
||||||
function commandIsWorking {
|
|
||||||
command=$1
|
|
||||||
if ( $command &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# ok if given pattern is detected on command's stdout or stderr
|
|
||||||
# arg1: command
|
|
||||||
# arg2: pattern
|
|
||||||
function commandStdoutPattern {
|
|
||||||
command=$1
|
|
||||||
shift
|
|
||||||
pattern=$@
|
|
||||||
if ( $command 2>&1 | grep -qiE "${pattern}" ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# ok if given service is enabled
|
|
||||||
# arg1: service
|
|
||||||
function serviceIsEnabled {
|
|
||||||
service=$1
|
|
||||||
if ( systemctl is-enabled $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# ok if given service is active
|
|
||||||
# arg1: service
|
|
||||||
function serviceIsActive {
|
|
||||||
service=$1
|
|
||||||
if ( systemctl is-active $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user