Outils pour utilisateurs

Outils du site


blog

script - subshells sous-shells - comportement déroutant de bash qu'un débutant doit connaître

Exemple variable globale

Exemple bash de problème d'affectation de variable globale

test_gvar1.sh

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
 
echo $(func_1)
echo $VAR
$ ./test_gvar1.sh
0
Hello
0

Alors que

test_gvar2.sh

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
 
func_1
echo $VAR
$ ./test_gvar2.sh
0
Hello
4

Dans le 1er exemple la variable n'est pas affectée par la fonction. En effet avec $(nom_de_fonction) bash lance un autre bash et copie la fonction, puis retourne le résultat au bash père.

De cette manière il est impossible par exemple d'implémenter un compteur qui compterait le nombre de fois que la fonction est appelée.

Bien sûr il est possible à la place de faire :

echo "Fonction func appelée à $(date --rfc-3339=second)" >> /tmp/trace.log

Mais ce n'est pas terrible

Voici une solution :

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
echo "${ func_1; }"
echo $VAR

Autre solution : Utiliser les pointeurs en bash !

Voir : https://stackoverflow.com/questions/75493732/how-to-modify-a-global-variable-within-a-function-and-return-a-boolean-in-bash

#! /bin/bash
 
VAR=0
 
func_1() {
    declare -n -x VAR_ref=$1
    VAR_ref=4
}
 
echo $VAR
func_1 VAR
echo $VAR
Exemple de commande exit qui semble ignorée

test_exit1.sh

#! /bin/bash
 
funcA() {
        echo A
        echo $(funcB)
}
 
 
funcB() {
        echo B
        exit 13
}
 
 
funcA
 
echo Suite du script

Il est possible de remplacer echo $(funcB) par echo ${ funcB; } mais la sortie de la fonction n'est pas affichée sur la sortie standard

$ ./test_exit1.sh
A
B
Suite du script
$ echo $?
0

Alors que :

test_exit2.sh

#! /bin/bash
 
funcA() {
        echo A
        funcB
}
 
 
funcB() {
        echo B
        exit 13
}
 
 
funcA
 
echo Suite du script
$ ./test_exit2.sh
A
B
$ echo $?
13

Exemple solution contournement

Une solution simple consiste à ajouter un trap et de remplacer la commande exit par la commande kill $$

test_exit3.sh

#! /bin/bash
 
trap 'exit 13' SIGUSR1
 
funcA() {
        echo A
        echo $(funcB)
}
 
 
funcB() {
        echo B
        # exit 13
        kill -s SIGUSR1 $$
}
 
 
funcA
 
echo Suite du script
$ ./test_exit3.sh
A
B

Autres

Affecter une variable sans créer de sous-shell

echo 'hello' | { read msg; echo "$msg"; }
2026/04/25 11:16 · Jean-Baptiste

Go lang - compiler pour windows i386 32bits

Voir aussi :

sudo apt-get install gcc-multilib-i686-linux-gnu
 
export GOROOT=$HOME/opt/go-legacy-win7/
export PATH=$HOME/opt/go-legacy-win7/bin/:"$PATH"
 
env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ CGO_LDFLAGS="-lssp" go build -ldflags="-s -w" -o app1.exe main.go

Ou encore avec zig

env CGO_ENABLED=1 GOROOT=~/opt/go-legacy-win7/ GOOS=windows GOARCH=386 CC="zig cc -target i686-linux-musl" ~/opt/go-legacy-win7/bin/go build hello.go

FIXME: i686-linux-musl n'est pas cohérent avec GOOS

Test

mkdir -p ~/myapp/prefix
export WINEPREFIX=$HOME/myapp/prefix 
export WINEARCH=win32 
export WINEPATH=$HOME/myapp 
wineboot --init
 
env WINEARCH=win32 wine app1.exe
$ exiftool app1.exe 
ExifTool Version Number         : 13.50
File Name                       : app1.exe
Directory                       : .
File Size                       : 1266 kB
File Modification Date/Time     : 2026:04:23 19:18:49+02:00
File Access Date/Time           : 2026:04:23 19:19:08+02:00
File Inode Change Date/Time     : 2026:04:23 19:18:49+02:00
File Permissions                : -rwxrwxr-x
File Type                       : Win32 EXE
File Type Extension             : exe
MIME Type                       : application/octet-stream
Machine Type                    : Intel 386 or later, and compatibles
Time Stamp                      : 0000:00:00 00:00:00
Image File Characteristics      : Executable, No line numbers, No symbols, 32-bit, No debug
PE Type                         : PE32
Linker Version                  : 2.45
Code Size                       : 571904
Initialized Data Size           : 693248
Uninitialized Data Size         : 154624
Entry Point                     : 0x1460
OS Version                      : 6.1
Image Version                   : 1.0
Subsystem Version               : 6.1
Subsystem                       : Windows command line

FIXME

2026/04/23 19:32 · Jean-Baptiste

Réseau - Forger un paquet TCP UDP ou autre

Voir les outils :

To exclude packets 1, 5, 10 to 20 and 30 to 40 from the new file use:

editcap capture.pcapng exclude.pcapng 1 5 10-20 30-40

To select just packets 1, 5, 10 to 20 and 30 to 40 for the new file use:

editcap -r capture.pcapng select.pcapng 1 5 10-20 30-40

Source : https://www.wireshark.org/docs/man-pages/editcap.html

Les modifications d'adresses de tcprewrite se font par plage. Pour changer l'adresse de destination

tcprewrite  --dstipmap=10.170.24.203/0:10.0.1.113/0 -C -i filter.cap -o filter-2.cap

le -C est là pour recalculer les checksums UDP ou TCP

2026/04/22 10:20 · Jean-Baptiste

Wine Err bcryptprimitives.dll not found - Compilation Croisée Go lang

Étapes à reproduire

Source : https://d3ext.github.io/posts/malware-dev-0/

main.go

package main
 
import (
        "fmt"
        "unsafe"
 
        "golang.org/x/sys/windows"
)
 
func main() {
        user32 := windows.NewLazyDLL("user32.dll")
        MessageBox := user32.NewProc("MessageBoxW")
 
        r, _, _ := MessageBox.Call(
                0,
                uintptr(unsafe.Pointer(windows.StringToUTF16Ptr("Hello World!"))),
                uintptr(unsafe.Pointer(windows.StringToUTF16Ptr("Example"))),
                windows.MB_OK,
        )
 
        fmt.Println("Return code:", r)
}
env GOARCH=amd64 GOOS=windows go build main.go

Erreur

$ wine main.exe 
it looks like wine32 is missing, you should install it.
multiarch needs to be enabled first.  as root, please
execute "dpkg --add-architecture i386 && apt-get update &&
apt-get install wine32:i386"
wine: created the configuration directory '/home/jibe/.wine'
0050:err:ole:StdMarshalImpl_MarshalInterface Failed to create ifstub, hr 0x80004002
0050:err:ole:CoMarshalInterface Failed to marshal the interface {6d5140c1-7436-11ce-8034-00aa006009fa}, hr 0x80004002
0050:err:ole:apartment_get_local_server_stream Failed: 0x80004002
0050:err:ole:start_rpcss Failed to open RpcSs service
0048:err:ole:StdMarshalImpl_MarshalInterface Failed to create ifstub, hr 0x80004002
0048:err:ole:CoMarshalInterface Failed to marshal the interface {6d5140c1-7436-11ce-8034-00aa006009fa}, hr 0x80004002
0048:err:ole:apartment_get_local_server_stream Failed: 0x80004002
wine: failed to open L"C:\\windows\\syswow64\\rundll32.exe": c0000135
wine: configuration in L"/home/jibe/.wine" has been updated.
fatal error: bcryptprimitives.dll not found
runtime: panic before malloc heap initialized

runtime stack:
runtime.throw({0x1400e5c03?, 0x21fd58?})
        /usr/lib/go-1.26/src/runtime/panic.go:1229 +0x4d fp=0x21fca8 sp=0x21fc78 pc=0x140076fad
runtime.loadOptionalSyscalls()
        /usr/lib/go-1.26/src/runtime/os_windows.go:267 +0x349 fp=0x21fd90 sp=0x21fca8 pc=0x140041229
runtime.osinit()
        /usr/lib/go-1.26/src/runtime/os_windows.go:464 +0x3d fp=0x21fe10 sp=0x21fd90 pc=0x14004193d
runtime.rt0_go()
        /usr/lib/go-1.26/src/runtime/asm_amd64.s:372 +0x13e fp=0x21fe18 sp=0x21fe10 pc=0x14007b2be
$ env WINEARCH=win64 wine main.exe 
wine: could not load kernel32.dll, status c0000135

$ mkdir -p ~/myapp/prefix
$ env LANG=C WINEPREFIX=$HOME/myapp/prefix WINEARCH=win32 wine main.exe 
Application could not be started, or no application associated with the specified file.
ShellExecuteEx failed: Bad EXE format for Z:\home\jibe\go\winapi\main.exe.

Solution 1

Voir :

Utiliser une ancienne version de Go ou une recompilée pour être compatible avec windows 7 Comme : https://github.com/thongtech/go-legacy-win7

export GOROOT=~/opt/go-legacy-win7/
alias go='~/opt/go-legacy-win7/bin/go'
 
go env |grep '/'
rm -rf ~/.cache/go-build
go clean -cache -modcache -fuzzcache
 
env GOARCH=amd64 GOOS=windows GOROOT=~/opt/go-legacy-win7/ ~/opt/go-legacy-win7/bin/go build main.go

Solution 2

Patcher le exe avec GoWin7Fixer.exe. Voir : https://github.com/stunndard/golangwin7patch

Solution 3

Mettre à jour Wine

Ou télécharger bcryptprimitives.dll et la déposer dans ~/.wine/drive_c/windows/system32/

Voir : https://packages.debian.org/trixie/amd64/libwine/filelist

2026/04/21 23:16 · Jean-Baptiste

Makefile et variables d'environnement

Makefile

include env
 
exe:
        go build -o app1.exe main.go
 
dll:
        go build -buildmode=c-shared -o app1.dll main.go
 
clean:
        rm -f app1.exe app1.dll app1.h call.exe
 
all:    exe dll
 
test:   exe dll
        go build call.go
        wine call.exe

env

cat env 
export GOOS=windows
export GOARCH=amd64
export CGO_ENABLED=1
export CXX=x86_64-w64-mingw32-g++
export CC=x86_64-w64-mingw32-gcc
export GOROOT=$(shell printenv HOME)/opt/go-legacy-win7/
export PATH=$(shell printenv HOME)/opt/go-legacy-win7/bin/:$(shell printenv PATH)
2026/04/21 22:06 · Jean-Baptiste
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki