Outils pour utilisateurs

Outils du site


blog

Proxy et apt-get

Pour définir une conf spécifique à apt-get (Adaptez et dé-commenter les lignes voulu)

/etc/apt/apt.conf.d/02proxy

#Acquire::http::Proxy "false";
#Acquire::ftp::Proxy "false";

#Acquire::http::Proxy "http://10.9.3.100:3128";
#Acquire::http::Proxy "http://utilisateur:motdepasse@example.com:port";

Mais nous préférerons à la place définir la conf avec les variables d’environnement

/etc/environment

# http_proxy=http://10.9.3.100:3128
# https_proxy=http://10.9.3.100:3128
ALL_PROXY=http://10.9.3.100:3128
NO_PROXY="localhost,127.0.0.1,.lan"

Notes : /etc/environment s'applique à tous les utilisateurs, à la place on peut utiliser le bashrc.

Où

http_proxy=http://10.9.3.100:3128 apt-get update && apt-get upgrade -y
2025/03/24 15:06

Protéger l’accès à vos applications web grâce à un mot de passe à usage unique OTP

Pour se faire nous utiliserons :

Un logiciel pour vous identifier en générant un mot de passe jetable. Sous Android :

  • andOTP
  • flauth
  • mOTP
  • SecureOTP
apt-get install python3-pip
adduser --group --system --disabled-password --home /opt/auth-otp app-otp
su - app-otp -s /bin/bash
python3 -m pip install --user pyotp
git clone https://github.com/newhouseb/simpleotp
cd simpleotp

main.py

-PORT = 8000
+PORT = 8001
-httpd = socketserver.TCPServer(("", PORT), AuthHandler)
+httpd = socketserver.TCPServer(("127.0.0.1", PORT), AuthHandler)

Après il faut lancer le script Python dans un tmux ou avec Systemd ou encore supervisord.

python3 main.py
python3 -c "import pyotp; print(pyotp.random_base32())" > .totp_secret
chmod 600 .totp_secret

Cong Nginx

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name cockpit.acme.fr;
 
    location /auth {                                                                                                                                                    
            proxy_pass http://127.0.0.1:8001; # This is the TOTP Server                                                                                                 
            proxy_set_header X-Original-URI $request_uri;                                                                                                               
    }                                                                                                                                                                   
 
    # This ensures that if the TOTP server returns 401 we redirect to login                                                                                             
    error_page 401 = @error401;                                                                                                                                         
    location @error401 {                                                                                                                                                
        return 302 /auth/login;                                                                                                                                         
    }                                                                                                                                                                                                                                                                                                                                        
 
    location / {                                                                                                                                                        
        auth_request /auth/check;                                                                                                                                       
 
        # Required to proxy the connection to Cockpit                                                                                                                   
        proxy_pass https://127.0.0.1:9090;                                                                                                                              
        proxy_set_header Host $host;                                                                                                                                    
        proxy_set_header X-Forwarded-Proto $scheme;                                                                                                                     
 
        # Required for web sockets to function                                                                                                                          
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
 
        # Pass ETag header from Cockpit to clients.
        # See: https://github.com/cockpit-project/cockpit/issues/5239
        gzip off;
    }
 
    ssl_certificate /etc/letsencrypt/live/cockpit.acme.fr/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cockpit.acme.fr/privkey.pem; # managed by Certbot
}
 
server {
    if ($host = cockpit.acme.fr) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
 
 
    listen         80;
    server_name    cockpit.acme.fr;
    return 404; # managed by Certbot
 
 
}

Conf andOTP

Créer un “details” de type “TOTP”, dans “secret”, saisissez le contenu du fichier .totp_secret

Service SystemD

/etc/systemd/system/simple-otp.service

[Unit]
Description=Simple OTP
Wants=network.target
After=network.target nginx.service
 
[Service]
WorkingDirectory=/opt/auth-otp/simpleotp
ExecStart=/usr/bin/python3 /opt/auth-otp/simpleotp/main.py
User=app-otp
Restart=always
 
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start simple-otp
systemctl status simple-otp
systemctl enable simple-otp
2025/03/24 15:06

Les Processus sous Linux

Voir aussi :

Tuer tous les processus d'un utilisateur

pkill -u plop

Appels système au noyaux et signaux

kill -l
man 7 signal
strace -p $PID
strace -f -e trace=file $CHEMIN_EXE 2>&1 | grep '/home/'

La table des processus

ps -ef | grep $PID
# ps -Aww -o pid,ppid,user,start,state,pri,pcpu,time:12,pmem,rsz:10,vsz:10,cmd

2em colone ⇒ PID 3em colone ⇒ PPID

Qui a lancé ce processus ? PPID ? Service ?

journalctl _PID=2636 -o json-pretty
Threads
# Classer par nombre de threads
ps -eL --no-headers -o nlwp,pid,lwp,comm,command | sort -u
 
# Avoir des information sur un thread
cat /proc/5990/task/5990/comm
Que fait TTY1 ?
peekfd -cn8 $(ps -t tty1 | grep -v login | sort -nr | head -1 | awk '{print $1}')
ProcFS

Voir egalement /proc/$PID/ Voir man proc ou man procfs

Cacher /proc et la table des processus aux utilusateurs :

Avec Grsecurity Ou en en remontant /proc linux-hide-processes-from-other-users hide-process-information-for-other-users hidepid

Modifier la ligne /proc dans /etc/fstab. Si aucune ligne ne correspond, ajouter la fstab

proc            /proc           proc    defaults,hidepid=2,gid=4        0       0

Pour appliquer la modif sans redemarrer :

mount -o remount /proc/

Audit process

Arrière plan

Using the Job Control of bash to send the process into the background:

  • Ctrl+Z to stop (pause) the program and get back to the shell.
  • bg to run it in the background.
  • disown -h [job-spec] where [job-spec] is the job number (like %1 for the first running job; find about your number with the jobs command) so that the job isn't killed when the terminal closes.

Source : how-do-i-put-an-already-running-process-under-nohup

info coreutils 'nohup invocation'

Voir :

  • nohup $EXE
  • $EXE &
  • ctrl + z
  • bg
  • fg
  • pstree
  • ps
  • setsid
  • runit / shpst
  • trap
# kill -l
18) SIGCONT 	19) SIGSTOP	

Exemple

Exemple
# jobs                                                                              
[1]+  Stoppé                 rsync -azS --partial /data/1/ $FIC root@192.168.205.2:/data/files/1/
[2]-  En cours d'exécution   rsync -azS --partial /data/2/ $FIC root@192.168.205.2:/data/files/2/ &  

jobs %%
jobs %1

Stopper le job1

kill -19 %1

Lancer le job2 une fois le job1 terminé

wait %2; kill -18 %1

http://linuxfr.org/users/srb/journaux/waitend-executer-une-commande-apres-une-autre-deja-lancee

sleep $(( 60 * 3 )) ; echo "Les œufs sont prêt"
sleep $(( 60 * 3 )) && echo "Les œufs sont prêt"
sleep $(( 60 * 3 )) &
wait $!
echo "Les œufs sont prêt"
sleep $(( 60 * 3 ))

[Ctrl] + [Z]

fg && echo "Les œufs sont prêt"

Autres

Voir atop, htop Voir cgroup

kthreadd

Voir https://www.it-connect.fr/les-processus-sous-linux

# ps -ef | head -40
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Mar15 ?        00:02:12 /sbin/init
root         2     0  0 Mar15 ?        00:00:00 [kthreadd]
root         3     2  0 Mar15 ?        19:02:47 [migration/0]
root         4     2  0 Mar15 ?        00:07:26 [ksoftirqd/0]
root         5     2  0 Mar15 ?        00:00:00 [stopper/0]
root         6     2  0 Mar15 ?        00:07:33 [watchdog/0]
root         7     2  0 Mar15 ?        18:46:13 [migration/1]
root         8     2  0 Mar15 ?        00:00:00 [stopper/1]
root         9     2  0 Mar15 ?        00:07:05 [ksoftirqd/1]
root        10     2  0 Mar15 ?        00:06:16 [watchdog/1]
root        11     2  0 Mar15 ?        18:55:58 [migration/2]
root        12     2  0 Mar15 ?        00:00:00 [stopper/2]
root        13     2  0 Mar15 ?        00:06:45 [ksoftirqd/2]
root        14     2  0 Mar15 ?        00:06:04 [watchdog/2]
root        15     2  0 Mar15 ?        19:15:39 [migration/3]
root        16     2  0 Mar15 ?        00:00:00 [stopper/3]
root        17     2  0 Mar15 ?        00:06:43 [ksoftirqd/3]
root        18     2  0 Mar15 ?        00:05:41 [watchdog/3]
root        19     2  0 Mar15 ?        18:54:50 [migration/4]
root        20     2  0 Mar15 ?        00:00:00 [stopper/4]
root        21     2  0 Mar15 ?        00:06:48 [ksoftirqd/4]
root        22     2  0 Mar15 ?        00:06:09 [watchdog/4]
root        23     2  0 Mar15 ?        18:51:16 [migration/5]
root        24     2  0 Mar15 ?        00:00:00 [stopper/5]
root        25     2  0 Mar15 ?        00:06:06 [ksoftirqd/5]
root        26     2  0 Mar15 ?        00:05:27 [watchdog/5]
root        27     2  0 Mar15 ?        18:59:33 [migration/6]
root        28     2  0 Mar15 ?        00:00:00 [stopper/6]
root        29     2  0 Mar15 ?        00:06:27 [ksoftirqd/6]
root        30     2  0 Mar15 ?        00:05:13 [watchdog/6]
root        31     2  0 Mar15 ?        19:15:16 [migration/7]
root        32     2  0 Mar15 ?        00:00:00 [stopper/7]
root        33     2  0 Mar15 ?        00:06:16 [ksoftirqd/7]
root        34     2  0 Mar15 ?        00:06:33 [watchdog/7]

Nous pouvons constater immédiatement quatre choses :

  1. Les processus '[migration/n]', contrairement aux processus classiques, on leurs noms entre des crochets []
  2. Le process père est [kthreadd] (PPID 2)
  3. Nous avons 8 processus : migration/0..migration/7. Soit un par core.
  4. Ces processus sont démarrés depuis le démarrage du système d'exploitation soit le 15 mars.
$ LANG=C man ps
"Sometimes the process args will be unavailable; when this happens, ps will instead print the executable name in brackets"

Les zombies

Tuer les zombies !

Source : https://www.linuxjournal.com/content/how-kill-zombie-processes-linux

Also known as “defunct” or “dead” process – In simple words, a Zombie process is one that is dead but is present in the system’s process table. Ideally, it should have been cleaned from the process table once it completed its job/execution but for some reason, its parent process didn’t clean it up properly after the execution.

In a just (Linux) world, a process notifies its parent process once it has completed its execution and has exited. Then the parent process would remove the process from process table. At this step, if the parent process is unable to read the process status from its child (the completed process), it won’t be able to remove the process from memory and thus the process being dead still continues to exist in the process table – hence, called a Zombie!

In order to kill a Zombie process, we need to identify it first. The following command can be used to find zombie processes:

ps aux | egrep "Z|defunct"

Z in the STAT column and/or [defunct] in the last (COMMAND) column of the output would identify a Zombie process.

Now practically you can’t kill a Zombie because it is already dead! What can be done is to notify its parent process explicitly so that it can retry to read the child (dead) process’s status and eventually clean them from the process table. This can be done by sending a SIGCHLD signal to the parent process. The following command can be used to find the parent process ID (PID):

ps -o ppid= <Child PID>

Once you have the Zombie’s parent process ID, you can use the following command to send a SIGCHLD signal to the parent process:

kill -s SIGCHLD <Parent PID>

However, if this does not help clearing out the Zombie process, you will have to kill or restart its parent process OR in case of a huge surge in Zombie processes causing or heading towards system outage, you will have no choice but to go for a system reboot. The following command can be used to kill its parent process:

kill -9 <Parent PID>

Note that killing a parent process will affect all of its child processes, so a quick double check will be helpful to be safe. Alternatively, if few lying zombie processes are not consuming much CPU/Memory, it’s better to kill the parent process or reboot the system in the next scheduled system maintenance.

Strace

Voir : https://aurelien-riv.github.io/php/2019/12/28/what-process-waiting-for.html

Exemple :

strace -e trace=network -f iceweasel http://acme.com 2>&1 | grep '8.8.8.8'
 
strace -ttfv -s128 -o /var/tmp/ping-$(hostname)-$(date +%Y.%m.%d-%H.%M).out ping -c1 acme.com
 
strace -f -p $(pidof mariadbd) --trace=sendto,recvfrom -s 2048 2>&1 | grep -i update
 
vimdiff <(cat plop.log | sed -e 's/^\[pid [0-9]\+\] //') <(cat plop.err | sed -e 's/^\[pid [0-9]\+\] //')
2025/03/24 15:06

Process audit quand strace n'est pas là

Voir :

  • /proc/[PID]/
  • procmap
lsof -p 33279
ps -efL | grep plop |wc -l
ss -tnp | grep plop
tcpdump -A -s0 host 192.168.100.5 and port 21
dmesg
pmap 33279
strace -r -o trace.log sudo echo hi

Pour voir les E/S sur les FD

peekfd -n -8 -d -c 32167 0 1 2

Notes strace

A debugging utility for Linux, monitors system calls used by a program and all received signals

Voir aussi :

  • eBPF – Linux kernel tracing backend providing a set of features similar to DTrace[28] since kernel version 4.9
  • ftrace – a tracing framework for the Linux kernel, capable of tracing scheduling events, interrupts, memory-mapped I/O, CPU power state transitions, etc.
  • ltrace – a Linux debugging utility, displays the calls a userland application makes to shared libraries
  • SystemTap – a scripting language and utility used for instrumenting Linux installations

-s strsize Specifies the maximum string size to print (the default is 32). Note that filenames are not considered strings and are always printed in full.

Activer / interdire strace
    kernel.yama.ptrace_scope = 0: all processes can be debugged, as long as they have same uid. This is the classical way of how ptracing worked.
    kernel.yama.ptrace_scope = 1: only a parent process can be debugged.
    kernel.yama.ptrace_scope = 2: Only admin can use ptrace, as it required CAP_SYS_PTRACE capability.
    kernel.yama.ptrace_scope = 3: No processes may be traced with ptrace. Once set, a reboot is needed to enable ptracing again.
# LANG=C strace -p 1171
strace: attach: ptrace(PTRACE_ATTACH, 1171): Operation not permitted

# echo 0 > /proc/sys/kernel/yama/ptrace_scope
# LANG=C strace -p 1171
strace -T -tt -f -v -q -s 4096 -o /tmp/nonworking_cp-strace.out <cp command>  &> /tmp/nonworking_cp.out
 
strace -fvttTyyx -s 1024 -o /tmp/strace.out <command> &> /tmp/df.out 

Autre

Auditer les signaux / les kills
Comparaison entre différents environnements

Voir procenv

2025/03/24 15:06
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki