tags: Filtri_Linux SED


L’esempio di file sul quale andremo a lavorare per gli esempi è il seguente:

nxc smb cicada.htb -u 'Guest' -p '' --rid-brute
SMB         10.129.231.149  445    CICADA-DC        [*] Windows Server 2022 Build 20348 x64 (name:CICADA-DC) (domain:cicada.htb) (signing:True) (SMBv1:None) (Null Auth:True)
SMB         10.129.231.149  445    CICADA-DC        [+] cicada.htb\Guest: 
SMB         10.129.231.149  445    CICADA-DC        498: CICADA\Enterprise Read-only Domain Controllers (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        500: CICADA\Administrator (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        501: CICADA\Guest (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        502: CICADA\krbtgt (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        512: CICADA\Domain Admins (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        513: CICADA\Domain Users (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        514: CICADA\Domain Guests (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        515: CICADA\Domain Computers (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        516: CICADA\Domain Controllers (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        517: CICADA\Cert Publishers (SidTypeAlias)
SMB         10.129.231.149  445    CICADA-DC        518: CICADA\Schema Admins (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        519: CICADA\Enterprise Admins (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        520: CICADA\Group Policy Creator Owners (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        521: CICADA\Read-only Domain Controllers (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        522: CICADA\Cloneable Domain Controllers (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        525: CICADA\Protected Users (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        526: CICADA\Key Admins (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        527: CICADA\Enterprise Key Admins (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        553: CICADA\RAS and IAS Servers (SidTypeAlias)
SMB         10.129.231.149  445    CICADA-DC        571: CICADA\Allowed RODC Password Replication Group (SidTypeAlias)
SMB         10.129.231.149  445    CICADA-DC        572: CICADA\Denied RODC Password Replication Group (SidTypeAlias)
SMB         10.129.231.149  445    CICADA-DC        1000: CICADA\CICADA-DC$ (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1101: CICADA\DnsAdmins (SidTypeAlias)
SMB         10.129.231.149  445    CICADA-DC        1102: CICADA\DnsUpdateProxy (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        1103: CICADA\Groups (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        1104: CICADA\john.smoulder (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1105: CICADA\sarah.dantelia (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1106: CICADA\michael.wrightson (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1108: CICADA\david.orelious (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1109: CICADA\Dev Support (SidTypeGroup)
SMB         10.129.231.149  445    CICADA-DC        1601: CICADA\emily.oscars (SidTypeUser)

Il Concetto Base

Se cut è “prendi una colonna”, SED è “trova questo testo e sostituiscilo con quello”.

sed 's/CERCA/SOSTITUISCI/g' file.txt

Leggi come: “Sostituisci (s/) globalmente (/g) ogni CERCA con SOSTITUISCI” praticamente analogo alla funzione di Vim :s/CERCA/SOSTITUISCI.

sed 's/CICADA/PANINO/g' e.txt 
 
SMB         10.129.231.149  445    PANINO-DC        [*] Windows Server 2022 Build 20348 x64 (name:PANINO-DC) (domain:cicada.htb) (signing:True) (SMBv1:None) (Null Auth:True)
SMB         10.129.231.149  445    PANINO-DC        [+] cicada.htb\Guest: 
SMB         10.129.231.149  445    PANINO-DC        498: PANINO\Enterprise Read-only Domain Controllers (SidTypeGroup)
SMB         10.129.231.149  445    PANINO-DC        500: PANINO\Administrator (SidTypeUser)
SMB         10.129.231.149  445    PANINO-DC        501: PANINO\Guest (SidTypeUser)
SMB         10.129.231.149  445    PANINO-DC        502: PANINO\krbtgt (SidTypeUser)
SMB         10.129.231.149  445    PANINO-DC        512: PANINO\Domain Admins (SidTypeGroup)
SMB         10.129.231.149  445    PANINO-DC        513: PANINO\Domain Users (SidTypeGroup)
SMB         10.129.231.149  445    PANINO-DC        514: PANINO\Domain Guests (SidTypeGroup)
SMB         10.129.231.149  445    PANINO-DC        515: PANINO\Domain Computers (SidTypeGroup)

Oppure se vuoi sostituire una stringa con il nulla puoi usare:

sed 's/CICADA\\//' prova.txt 
 
SMB         10.129.231.149  445    CICADA-DC        1000: CICADA-DC$ (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1104: john.smoulder (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1105: sarah.dantelia (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1106: michael.wrightson (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1108: david.orelious (SidTypeUser)
SMB         10.129.231.149  445    CICADA-DC        1601: emily.oscars (SidTypeUser)
 

Miscela Regex + SED

 
cat output.txt | sed 's/.*CICADA\\\([^ ]*\).*/\1/'
 
CICADA-DC$
john.smoulder
sarah.dantelia
michael.wrightson
david.orelious
emily.oscars
 

Separati gli elementi:

ParteCosa èCosa fa
s/SED”Sostituisci”
.*REGEX”Qualunque cosa (greedy)“
CICADA\\Testo + regexCerca letterale CICADA\
\(REGEXInizio gruppo di cattura
[^ ]*REGEX”Qualunque cosa TRANNE spazio”
\)REGEXFine gruppo di cattura
.*REGEX”Qualunque cosa”
/\1/SED”Sostituisci con il Gruppo 1”