Cette page fourni quelques exemples de scripts bash utiles pour OSX (MacOSX).





0x01. dhclient.sh


 
#!/bin/bash
 
APP="$(basename $0)"
CNF="$(dirname $0)/../etc/dhclient.conf"
. "$CNF"
# /usr/local/sbin/dhclient
# > /usr/local/sbin/../etc/dhclient.conf
# = /usr/local/etc/dhclient.conf
#
# -- Content of dhclient.conf :
#
#  # See: /usr/local/sbin/dhclient
# 
#  # Determine how many tries before DHCP failure
#  N_TRIES=5
#
 
 
 
ifconfig=/sbin/ifconfig
ipconfig=/usr/sbin/ipconfig
networksetup=/usr/sbin/networksetup
arp=/usr/sbin/arp
 
e=0
[ $# -eq 1 ] && ifce=$1
[ $# -ne 1 ] && ifce="en0"  # default OSX BSD network interface
[ "$(id -u)" != "0" ] && e=1
$ifconfig $ifce|grep -q "$ifce" || e=1
 
 
if [ $e -eq 1 ]; then
  echo "
Usage: $APP <interface>
 
Script runs commands:
 ipconfig set \\$ifce dhcp
 ipconfig getpacket \\$ifce ; ipconfig getifaddr \\$ifce
 networksetup -setnetworkserviceenabled \\$ifce off
 networksetup -setnetworkserviceenabled \\$ifce on
 
 You need to be root.
"
[ "$(id -u)" != "0" ] && echo "You need to be root to use $APP.
"
  exit
fi
 
PID=$$
 
echo "Script using unix tools (ipconfig), April 5, 2012
Copyleft 2011-2012, script from www.secureinfo.eu
For more info, please see the BSD man page of ipconfig(1)
Retrieving IP address from DHCP server ..."
 
 
(
$arp -da -i $ifce
$ifconfig $ifce -alias 0.0.0.0
) >/dev/null 2>&1
 
$ipconfig set $ifce dhcp
[ "$ifce" = "en1" ] && $networksetup -setairportpower en1 on
sleep 2
 
printf "Get packet from : "
for i in $(seq 1 $N_TRIES) ; do
	e=1
	$ipconfig getpacket $ifce 2>&1|grep -i server_identifier|GREP_COLOR="1;29" grep --color=always -oE "(([0-9]){1,3}\\.){3}([0-9]){1,3}" && e=0
	[ $e -eq 0 ] && break
	sleep 1
done
 
 
tries=0 ; e=1 ; while test $e -ne 0
do
  null=$($ipconfig getifaddr $ifce >/dev/null 2>&1)
  $ifconfig $ifce|grep -q "inet " ; e=$?
  sleep .4
  tries=$((tries+1))
  [ $tries -gt $N_TRIES ] && exit 1
done
 
#/sbin/arpong $ifce >/dev/null 2>&1
#/sbin/arpong check
#$HOME/.local/scr/internet -q
 
echo
$ifconfig $ifce
echo
 




0x02. list-user.sh


 
dscacheutil -q user|egrep -B5 '/.?+sh'|grep name|awk '{ print $2 }'|sort -u
 




0x03. ios-data.sh


 
cd /private/var/db/lockdown/*
ls -l
 




0x04. msgbox


 
#!/bin/bash
 
 
if [ $# -eq 3 ]; then
cat << EOD |osascript
tell application "Finder"
  display dialog "$1" buttons {"$2","$3"}
end tell
EOD
fi
 
if [ $# -eq 2 ]; then
cat << EOD |osascript
tell application "Finder"
  display dialog "$1" buttons {"$2"}
end tell
EOD
fi
 
if [ $# -eq 1 ]; then
cat << EOD |osascript
tell application "Finder"
  display dialog "$1"
end tell
EOD
fi
 




0x05. macchanger


 
#!/bin/bash
 
 
APP="$(basename $0)"
 
 
airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport"
max_attempt=50
 
 
function _random_mac
{
  o[0]="0"; o[1]="1"
  o[2]="2"; o[3]="3"
  o[4]="4"; o[5]="5"
  o[6]="6"; o[7]="7"
  o[8]="8"; o[9]="9"
 
  o[10]="a"; o[11]="b"
  o[12]="c"; o[13]="d"
  o[14]="e"; o[15]="f"
 
  MAC="00:$((RANDOM%3))"${o[$((RANDOM%16))]}
  MAC=$MAC":"
  MAC=$MAC${o[$((RANDOM%16))]} ; MAC=$MAC${o[$((RANDOM%16))]} 
  MAC=$MAC":"
  MAC=$MAC${o[$((RANDOM%16))]} ; MAC=$MAC${o[$((RANDOM%16))]}
  MAC=$MAC":"
  MAC=$MAC${o[$((RANDOM%16))]} ; MAC=$MAC${o[$((RANDOM%16))]}
  MAC=$MAC":"
  MAC=$MAC${o[$((RANDOM%16))]} ; MAC=$MAC${o[$((RANDOM%16))]}
	echo $MAC
}
 
function change_ether()
{
	ifce=$1
	mac=$2
	ifconfig $ifce up
	for i in $(seq 1 $max_attempt)
	do
		ifconfig $ifce lladdr $mac
		ifconfig $ifce|grep ether|grep "$mac" >/dev/null 2>&1 && break
		$airport -z
		sleep .1
	done
}
 
function usage()
{
	echo "
Usage: $APP <interface> <mac address|random>
 
interface   : unix name of internface (en0, en1, ...)
mac address : in format : 00:12:34:5A:BC:DE
"
	exit 0
}
 
function error()
{
	echo "
Error : $1
"
	exit $2
}
 
 
if [ $# -eq 2 ]; then
	ifce="$1"
	mac="$2"
	ifconfig $ifce >/dev/null 2>&1|| error "interface not available" 1
 
	if [ "$mac" != "random" ]; then
		check_mac=$(echo "$mac"|grep -i -oE "(([0-9A-F]){2}:){5}(([0-9A-F]){2})")
		[ "$mac" != "$check_mac" ] && error "MAC address in bad format" 2
	else
		mac=$(_random_mac)
	fi
 
	mac1=$(ifconfig $ifce|grep ether|awk '{ print $2 }')
 
	echo
	echo "Old MAC address : $mac1"
	change_ether "$ifce" "$mac"
	echo "New MAC address : $mac"
	echo
else
	usage
fi
 




0x06. ramdisk


 
#!/bin/bash
APP="$(bansename $0)"
 
 
 
if [ $# -lt 1 -o $# -gt 2 ] ; then
  echo "
Usage  : $APP <size><unit> [mount point]
 
Script will create a ramdisk of given size
 
Exemple: $APP 1G 
"
  exit 0
fi
 
 
SIZE=$(echo $1 |grep -oE "^([0-9]){1,}")
UNIT=$(echo $1 |tr A-Z a-z |grep -oiE "([mgk])$")
MOUNT="RAM"
[ $# -eq 2 ] && MOUNT="$2"
 
# Size of ramdisk is set like this : 
#
#  2330860 * 512 = 1.191.400.320 Soit 1 GB.
#  NombreDeBlocs = (Taille du volume MB) * 2048
#
###
 
BLOC=512
case $UNIT in
  "k") MULTIPLE=1024        ;;
  "m") MULTIPLE=1048576     ;;
  "g") MULTIPLE=1073741824  ;;
esac
 
 
BLOCS=$((SIZE * MULTIPLE / BLOC))
echo "
Size:       $SIZE
# of blocs: $BLOCS
Multilple:  $MULTIPLE
"
diskutil erasevolume HFS+ "$MOUNT" `hdiutil attach -nomount ram://$BLOCS`
 




0x07. arpong


 
#!/bin/bash
 
 
 
#
# Variables
#
APP="$(basename $0)"
nmap="/opt/local/bin/nmap"           # port install nmap
ipcalc="/opt/local/bin/ipcalc"       # port install ipcalc
arping="/opt/local/bin/arping"       # port install arping
nmap_report="/var/run/nmap_$$.log"
arp_static="/var/run/sarpd.conf"
 
 
 
#
# Unload properly
#
trap unload STOP TERM
 
 
 
#
# Functions
#
error()
{
	echo "
[x] Error : $1
"
	exit 1
}
 
_flush_net()
{
	arp -da
	route -n flush
	dscacheutil -flushcache
}
 
check()
{
	printf "\\n$J[-] ARP table :\\n"
	arp -an|grep "permanent"
	printf "\\n$J[-] IP settings :\\n"
	ifconfig|egrep -A6 "en.:"|egrep "en.:|status|inet "
	printf "\\n$J[-] Route settings :\\n"
	netstat -rn -f inet |grep "en."|egrep -v "link"
	echo
	exit 0
}
 
_get_ifce()
{
  for ifce in $(ifconfig -l|egrep -o "en.") ; do
    e=1 ; ifconfig $ifce|grep -q "status: active" && e=0
		if [ $e -eq 0 ]; then
			echo "$ifce"
			break
		fi
  done
}
 
_ip2mac()
{
	ifce=$(_get_ifce)
	ip=$1
	arping -w 20000 -c 10 -p -i $ifce -d -r $ip|tail -n1
}
 
 
 
#
# Arguments
#
if [ $# -gt 3 ]; then
	echo "
Usage: $APP [ifce]
 
Set in loop mac address in ARP tables.
If interface arguments not defined, then
auto detect configured and active network interface
"
	exit 0
fi
 
sleep=1
[ ! -z "$1" ] && [ "$1" = "check" ] && check
[ ! -z "$1" ] && ifce=$1
[ ! -z "$2" ] && sleep=$2
 
[ -z "$ifce" ] && ifce=$(_get_ifce)
[ -z "$ifce" ] && ifce=$AIRPORT_BSD_IFCE  # /usr/common.sh
ifconfig $ifce|grep -q "status: inactive" && error "unable to get network device"
 
 
 
#
# Get network settings
#
me=$(ifconfig $ifce      |grep "inet "  |awk '{ print $2 }'|head -n1)
net=$(ifconfig $ifce     |grep "$me"    |egrep -o "0x(.?){8}")
net=$($ipcalc -n $me/$net |grep "Network"|awk {'print $2'}|\\
      grep -oE "(([0-9]){1,3}\\.){3}([0-9]){1,3}/([1-2]?[0-9])")
gw=$(netstat -rn|egrep -v "^Destination"|egrep "U.+G?.+r|default"|egrep -o "(([0-9]){1,3}\\.){3}([0-9]){1,3}"|head -n1)
mac_gw=$(_ip2mac $gw)
 
 
#
# Build static ARP table
#
printf "\\n[+] Scanning network ($ifce > $net)..."
$nmap -e $ifce -sn -n -T5 --send-eth --exclude $me $net -oX "$nmap_report" >/dev/null 2>&1
egrep 'state="up"|ipv4|mac' "$nmap_report"|\\
	cut -d'"' -f2|\\
	sed -E "s/(([0-9A-F]){2}:){5}(([0-9A-F]){2})/;\\0/g;s/up//g"|\\
	tr "\\n" "%"|\\
	sed 's/%;/;/g'|\\
	tr "%" "\\n"|\\
	tr ";" " "|\\
	egrep "."|\\
	awk '{ print $1"\\t"$2 }' |\\
	grep ':' > "$arp_static"
printf "\\b\\b\\b : \\n"
cat "$arp_static"
rm -f $nmap_report
 
 
#
# Create static ARP table
#
printf "\\n[+] Protecting ARP table ($gw > $mac_gw)\\n"
(
 _flush_net
 arp -f "$arp_static"
 route -n add -host $net $gw
 arp -S $gw $mac_gw blackhole pub only
 rm -f "$arp_static"
) >/dev/null 2>&1
sleep 1.5
arp -an|grep permanent
echo
 




0x08. seqA


 
#!/bin/bash
 
a[1]="a"
a[2]="b"
a[3]="c"
a[4]="d"
a[5]="e"
a[6]="f"
a[7]="g"
a[8]="h"
a[9]="i"
a[10]="j"
a[11]="k"
a[12]="l"
a[13]="m"
a[14]="n"
a[15]="o"
a[16]="p"
a[17]="q"
a[18]="r"
a[19]="s"
a[20]="t"
a[21]="u"
a[22]="v"
a[23]="w"
a[24]="x"
a[25]="y"
a[26]="z"
 
maj=0
if [ $# -gt 2 ]
then
  if [ "$1" = "-u" ]
  then
    maj=1
    shift
  fi
fi
 
if [ $# -eq 3 ]
then
  debut=$1
  pas=$2
  fin=$3
fi
 
if [ $# -eq 2 ]
then
  debut=$1
  fin=$2
  pas=1
fi
 
if [ $# -lt 2 ]
then
  echo "
Usage: $APP [-u] <start> [step] <end>
 
 -u  : affiche en majuscule
"
exit 0
fi
 
for l in $(seq $debut $pas $fin)
do
  [ $maj -eq 1 ] && echo ${a[$l]} |tr a-z A-Z
  [ $maj -eq 0 ] && echo ${a[$l]} |tr A-Z a-z
done
 




0x09. seqH


 
#!/bin/bash
APP=$(basename $0)
if [ $# -eq 3 ]
then
  debut=0x$1
  pas=0x$2
  fin=0x$3
fi
 
if [ $# -eq 2 ]
then
  debut=0x$1
  fin=0x$2
  pas=0x1
fi
 
if [ $# -lt 2 ]
then
  echo "
Usage: $APP <start> [step] <end>
 
Comptage hexadécimal
"
exit 0
fi
 
 
 
for l in $(jot - $debut $fin $pas)
do
  python -c "print hex($l)" |cut -d'x' -f2
done
 



   =>   Écrit par : Nicolas, le 10 décembre 2013


 
Mots clés :  
  bash 
  
  macos 
    >   Articles connexes :

Chiffrement multicouche



HTTP Download



Comment gagner du temps sur Internet



/tmp et /var/log en noexec sur macOS



2276930