-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcntlm_setup.sh
236 lines (201 loc) · 6.69 KB
/
cntlm_setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/sh
# Function
HIGHLIGHT(){
exec >/dev/tty
echo "`tput bold``tput setf 1 `#############################################################` tput sgr0`"
exec 1>/dev/null 2>/dev/null
}
SPACE(){
exec >/dev/tty
echo ""
exec 1>/dev/null 2>/dev/null
}
PRINT_MESSAGE(){
exec >/dev/tty
echo "`tput bold``tput setf 1`[*]`tput sgr0` $1"
exec 1>/dev/null 2>/dev/null
}
PRINT_GOOD(){
exec >/dev/tty
echo "`tput bold``tput setf 2`[*]`tput sgr0` $1"
exec 1>/dev/null 2>/dev/null
}
PRINT_ERROR(){
exec >/dev/tty
echo "`tput bold``tput setf 4`[*]`tput sgr0` $1"
exec 1>/dev/null 2>/dev/null
}
PRINT_QUESTION(){
exec >/dev/tty
echo -n "`tput bold``tput setf 6`[*]`tput sgr0` $1"
exec 1>/dev/null 2>/dev/null
}
DISABLE_LOGGING () {
CNTLM_CONFIG_FILE=/etc/rsyslog.d/00-cntlm.conf
sudo mv ${CNTLM_CONFIG_FILE} ${CNTLM_CONFIG_FILE}.old
echo "# Suppress CNTLM info messages" | sudo tee -a ${CNTLM_CONFIG_FILE}
echo "if \$programname == 'cntlm' then stop" | sudo tee -a ${CNTLM_CONFIG_FILE}
sudo service rsyslog restart
}
ENABLE_PROXY_GLOBAL () {
#Add to /etc/environment
#http://askubuntu.com/questions/150210/how-do-i-set-systemwide-proxy-servers-in-xubuntu-lubuntu-or-ubuntu-studio
echo "http_proxy=http://localhost:3128
ftp_proxy=http://localhost:3128
https_proxy=http://localhost:3128
no_proxy="127.0.0.0/8,localhost"" | sudo tee -a /etc/environment
}
SETUP_EXISTING_SETTINGS() {
PRINT_MESSAGE "Existing Config"
USERNAME=$(sudo grep 'Username ' /etc/cntlm.conf | awk '{print $2}')
DOMAIN=$(sudo grep 'Domain ' /etc/cntlm.conf | awk '{print $2}')
EXISTING_PROXY=$(sudo grep -v '^[#|No]' /etc/cntlm.conf | grep "Proxy" | awk '{print $2}')
PROXY_IP=${EXISTING_PROXY%:*}
PROXY_PORT=${EXISTING_PROXY##*:}
}
SETUP_NEW_SETTINGS() {
# Proxy Details
PRINT_MESSAGE "Proxy Details"
PRINT_QUESTION "Enter Proxy Hostname/IP [ENTER]: "
read PROXY_IP
PRINT_QUESTION "Enter Proxy Port [ENTER]: "
read PROXY_PORT
# Check Proxy is valid
PROXYSRV_CHECK=$(echo | timeout 5 telnet ${PROXY_IP} ${PROXY_PORT} | grep -o "Connected")
if [ -z "$PROXYSRV_CHECK" ]; then
PRINT_ERROR "Unable to connect to proxy ${PROXY_IP}:${PROXY_PORT}"
exit 0
else
PRINT_GOOD "Proxy Server Valid!"
fi
HIGHLIGHT
# read domain details
PRINT_QUESTION "Enter Username [ENTER]: "
read USERNAME
PRINT_QUESTION "Enter Domain Name [ENTER]: "
read DOMAIN
PRINT_QUESTION "Enter Domain Password [ENTER]: "
NTLMV2_HASH=$(/usr/sbin/cntlm -u $USERNAME -d $DOMAIN -f -H | tail -1 | awk '{print $2}' )
SPACE
}
APPLY_SETTINGS() {
# Display back Domain Creds
HIGHLIGHT
PRINT_GOOD "Username - $USERNAME"
PRINT_GOOD "Domain - $DOMAIN"
PRINT_GOOD "Hash - $NTLMV2_HASH"
SPACE
PRINT_GOOD "Proxy IP - $PROXY_IP"
PRINT_GOOD "Proxy Port - $PROXY_PORT"
#Backup CNTLM Config
sudo cp /etc/cntlm.conf_orig /etc/cntlm.conf
sudo cp /etc/cntlm.conf /etc/cntlm.conf_orig
#Update CNTLM Config file
sudo sed -i 's/Username\stestuser/Username '$USERNAME'/g' /etc/cntlm.conf
sudo sed -i 's/Domain\s\scorp-uk/Domain '$DOMAIN'/g' /etc/cntlm.conf
sudo sed -i 's/Password\spassword/# Password password/g' /etc/cntlm.conf
sudo sed -i 's/# PassNTLMv2/PassNTLMv2/g' /etc/cntlm.conf
sudo sed -i 's/D5826E9C665C37C80B53397D5C07BBCB/'$NTLMV2_HASH'/g' /etc/cntlm.conf
sudo sed -i 's/Proxy\s\s10.0.0.41:8080/# Proxy 10.0.0.41:8080/g' /etc/cntlm.conf
sudo sed -i 's/Proxy\s\s10.0.0.42:8080/# Proxy 10.0.0.42:8080/g' /etc/cntlm.conf
sudo sed -i "/# List addresses you do not want to pass to parent proxies/ { s/# List addresses you do not want to pass to parent proxies/Proxy $PROXY_IP:$PROXY_PORT\n\n&/ }" /etc/cntlm.conf
# Try Creds
# http://www.leg.uct.ac.za/howtos/use-isa-proxies
HIGHLIGHT
PRINT_MESSAGE "Checking Proxy Settings"
PRINT_MESSAGE "Checking may take upto 1 minute"
# Check Proxy
PRINT_QUESTION "Enter Domain Password [ENTER]: "
PROXY_CHECK=$(sudo cntlm -M http://google.com/ | grep 'Wrong\|failed')
if ! [ -z "$PROXY_CHECK" ]; then
sudo cp /etc/cntlm.conf_orig /etc/cntlm.conf
SPACE
PRINT_ERROR "Config Failed"
exit 0
else
SPACE
PRINT_GOOD "Proxy Working!"
fi
#Add to .bashrc
echo "export http_proxy=http://localhost:3128
export ftp_proxy=http://localhost:3128
export https_proxy=http://localhost:3128" >> ~/.bashrc
# Restart service
sudo service cntlm restart
#Add proxy for sudoers
sudo sed -i "/Defaults\senv_reset/ { s/Defaults\senv_reset/\nDefaults env_keep = \"http_proxy ftp_proxy https_proxy no_proxy\"\n&/ }" /etc/sudoers
# Add Proxy for SVN
dpkg -s svn 2>&1 | grep -o Installed-Size
if [ $? != 0 ] ; then
sudo echo "http-proxy-host=localhost
http-proxy-port=3128" >> /etc/subversion/servers
fi
# Enable CNTLM globally
while true; do
PRINT_QUESTION "Do you want to enable CNTLM globally for all user and applications? [Default y]"
read -p "" yn
case $yn in
[Yy]* ) ENABLE_PROXY_GLOBAL; break;;
[Nn]* ) PRINT_MESSAGE "CNTLM not enabled globally";break;;
* ) PRINT_ERROR "Please answer yes or no.";;
esac
done
# Disable logging all websites visited in syslog
while true; do
PRINT_QUESTION "Do you want to disable CNTLM logging for all sites visited? [Default y]"
read -p "" yn
case $yn in
[Yy]* ) DISABLE_LOGGING; break;;
[Nn]* ) PRINT_MESSAGE "Logging not disabled";break;;
* ) PRINT_ERROR "Please answer yes or no.";;
esac
done
PRINT_MESSAGE "Logout to take affect"
PRINT_MESSAGE "Alternately: Run the following command in terminal: export http_proxy=http://localhost:3128"
}
CLEAR () {
# Enable output
exec >/dev/tty
# Clear Screen
clear
# Disable output
exec 1>/dev/null 2>/dev/null
# Kill Process using that port
sudo fuser -k 3128/tcp
# Info
PRINT_GOOD "Setting up NTLM to Proxy"
HIGHLIGHT
}
# Check CNTLM is Installed
dpkg -s cntlm 2>&1 | grep -o Installed-Size
if [ $? != 0 ] ; then
PRINT_ERROR "CNTLM not Installed"
PRINT_ERROR "Try - sudo apt-get install cntlm"
exit 0
fi
CLEAR
#Check for existing Config
EXISTING_CONFIG_CHECK=$(sudo grep 'Domain corp-uk' /etc/cntlm.conf)
if [ -n "${EXISTING_CONFIG_CHECK}"]; then
SETUP_EXISTING_SETTINGS
# Display back Domain Creds
HIGHLIGHT
PRINT_GOOD "Username - $USERNAME"
PRINT_GOOD "Domain - $DOMAIN"
SPACE
PRINT_GOOD "Proxy IP - $PROXY_IP"
PRINT_GOOD "Proxy Port - $PROXY_PORT"
# Enable CNTLM globally
while true; do
PRINT_QUESTION "Do you want to use the existing settings and only change the password? [Default y]"
read -p "" yn
case $yn in
[Yy]* ) CLEAR;PRINT_QUESTION "Enter Domain Password [ENTER]: ";NTLMV2_HASH=$(/usr/sbin/cntlm -u $USERNAME -d $DOMAIN -f -H | tail -1 | awk '{print $2}' );SPACE;APPLY_SETTINGS; break;;
[Nn]* ) CLEAR;SETUP_NEW_SETTINGS;APPLY_SETTINGS;break;;
* ) PRINT_ERROR "Please answer yes or no.";;
esac
done
else
SETUP_NEW_SETTINGS
APPLY_SETTINGS
fi