Script Kerberos Ktutil to make keytabs
Asked Answered
F

6

19

I want to make a script that will generate the a keytab using ktutil. When running the script I want to use [user]$ script.sh PASSWORD

#script.sh
echo "addent -password -p PRINCIPAL -k 1 -e aes256-cts-hmac-sha1-96" | ktutil

Ktutil than needs a password, here I want to use the PASSWORD argument from above. How would I pass the password arguement?

Flirt answered 26/5, 2016 at 7:31 Comment(0)
H
26

With GNU bash:

user="PRINCIPAL"
pass="topsecret"

printf "%b" "addent -password -p $user -k 1 -e aes256-cts-hmac-sha1-96\n$pass\nwrite_kt $user.keytab" | ktutil

printf "%b" "read_kt $user.keytab\nlist" | ktutil

Output:

slot KVNO Principal
---- ---- ---------------------------------------------------------------------
   1    1                          PRINCIPAL@YOURDOMAIN
Horsley answered 26/5, 2016 at 7:58 Comment(5)
This is not safe. Password will be visible.Effluent
Cannot read password while adding new entryErenow
@Horsley If there is \n in the environment variable (for example, in a password), the result will be incorrect.Urine
@Giorgos-Myrianthous Variables of the environment will not be visible. It is safe. Or what do you mean?Urine
@MaximMandrik environment variables will not be visible, but printf argument will.Disloyalty
M
5

I followed the accepted answer by Cyrus but kept hitting

"Cannot read password while adding new entry" as mentioned by Arrow_Raider

I was able to overcome the error by using the slightly similar result using the below.

   {
    echo "addent -password -p ${user} -k 1 -e RC4-HMAC"
    sleep 1
    echo "${pass}"
    sleep 1
    echo "write_kt my.keytab"
    } |
    ktutil
Memoirs answered 20/10, 2021 at 21:8 Comment(2)
This answer is underrated, and more simple than using expect. Nice work.Youngling
Previously I always used the printf "%b" "..\n..\n.." | ktutil, but on one machine I also got hit by addent: Cannot read password while adding new entry.Auramine
W
3

To create the multiple orgs keytabs and default hbase,pipe,hdfs keytab at the same time you can run the below script, which i have just created:

#!/bin/bash
read -p "Please enter space-delimited list of ORGS to create: " NEW_ORGS

clear
#echo "#################  CREATE KEYTABS  ############################"
#echo ""
kdestroy

for i in $NEW_ORGS
do
     printf "%b" "addent -password -p ${i} -k 1 -e aes256-cts-hmac-sha1-96\n${i}\nwrite_kt ${i}.keytab" | ktutil

     printf "%b" "read_kt ${i}.keytab\nlist" | ktutil

done
echo ""


if [ ! -e /home/eip/.keytabs/hbase.keytab ]
then
        printf "%b" "addent -password -p hbase -k 1 -e aes256-cts-hmac-sha1-96\nhbase\nwrite_kt hbase.keytab" | ktutil

        printf "%b" "read_kt hbase.keytab\nlist" | ktutil
fi

exit 0
Wiggs answered 18/7, 2017 at 9:26 Comment(1)
@Lokendra_Jain If there is \n in the environment variable (for example, in a password), the result will be incorrect.Urine
T
2

A version in Python

https://github.com/Tagar/stuff/blob/master/keytab.py

piping password to ktutil in shell is not secure as password will be visible in list of processes.

Since this Python scripts just interacts with ktutil using pexpect library, it's possible to implement the same as a pure shell script using expect.

Hope this helps.

Tincher answered 17/1, 2017 at 16:40 Comment(0)
E
2

enjoy

import os, getpass
from subprocess import run, PIPE
import sys
userndomain, passwd, enctype = 'username@DOMAIN', 'secret', 'arcfour-hmac-md5'
input_load = f"""add_entry -password -p {userndomain} -k 1 -e {enctype}
{passwd}
write_kt {user}.keytab
quit
"""
p = run(['ktutil'], stdout=PIPE, input=input_load, encoding='ascii')
Elutriate answered 27/11, 2020 at 18:6 Comment(0)
E
2

Use expect to keep the password out of the process list:

expect << EOF
    set timeout 10
    spawn /usr/bin/ktutil
    expect {
       "ktutil: " { send "addent -password -p $PRINCIPAL -k 1 -e $METHOD\r" }
       timeout { puts "Timeout waiting for ktutil prompt."; exit 1; }
    }
    expect {
       -re "Password for \\\\S+: " { send "$PASSWORD\r" }
       timeout { puts "Timeout waiting for password prompt."; exit 1; }
    }
    expect {
       "ktutil: " { send "wkt $KEYTAB_TMP\r" }
    }
    expect {
       "ktutil: " { send "q\r" }
    }
EOF

Or use a <<HERE document to provide stdin, but if addent fails to prompt, you may end up with the password in your stdout:

/usr/bin/ktutil <<EOF
addent -password -p $PRINCIPAL -k 1 -e $METHOD
$PASSWORD
wkt $KEYTAB_TMP
q
EOF
Etana answered 5/4, 2021 at 17:36 Comment(2)
Probably in the case of expect send: if there is \r in the environment variable (for example, in a password), the result will be incorrect. I like the second example, although also has its drawbacks if the ktutil program will change (but it is unlikely and the problem will be quickly detected, because it will be immediately visible). But in this example, it is possible to use \r and similar in passwords.Urine
The expect approach doesn't work if kubernetes runs this. It results in a keytab file that is 0 bytes. Not sure about the second.Erenow

© 2022 - 2024 — McMap. All rights reserved.