Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

knownhosts.py 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. # -*- test-case-name: twisted.conch.test.test_knownhosts -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. An implementation of the OpenSSH known_hosts database.
  6. @since: 8.2
  7. """
  8. import hmac
  9. import sys
  10. from binascii import Error as DecodeError, a2b_base64, b2a_base64
  11. from contextlib import closing
  12. from hashlib import sha1
  13. from zope.interface import implementer
  14. from twisted.conch.error import HostKeyChanged, InvalidEntry, UserRejectedKey
  15. from twisted.conch.interfaces import IKnownHostEntry
  16. from twisted.conch.ssh.keys import BadKeyError, FingerprintFormats, Key
  17. from twisted.internet import defer
  18. from twisted.logger import Logger
  19. from twisted.python.compat import nativeString
  20. from twisted.python.randbytes import secureRandom
  21. from twisted.python.util import FancyEqMixin
  22. log = Logger()
  23. def _b64encode(s):
  24. """
  25. Encode a binary string as base64 with no trailing newline.
  26. @param s: The string to encode.
  27. @type s: L{bytes}
  28. @return: The base64-encoded string.
  29. @rtype: L{bytes}
  30. """
  31. return b2a_base64(s).strip()
  32. def _extractCommon(string):
  33. """
  34. Extract common elements of base64 keys from an entry in a hosts file.
  35. @param string: A known hosts file entry (a single line).
  36. @type string: L{bytes}
  37. @return: a 4-tuple of hostname data (L{bytes}), ssh key type (L{bytes}), key
  38. (L{Key}), and comment (L{bytes} or L{None}). The hostname data is
  39. simply the beginning of the line up to the first occurrence of
  40. whitespace.
  41. @rtype: L{tuple}
  42. """
  43. elements = string.split(None, 2)
  44. if len(elements) != 3:
  45. raise InvalidEntry()
  46. hostnames, keyType, keyAndComment = elements
  47. splitkey = keyAndComment.split(None, 1)
  48. if len(splitkey) == 2:
  49. keyString, comment = splitkey
  50. comment = comment.rstrip(b"\n")
  51. else:
  52. keyString = splitkey[0]
  53. comment = None
  54. key = Key.fromString(a2b_base64(keyString))
  55. return hostnames, keyType, key, comment
  56. class _BaseEntry:
  57. """
  58. Abstract base of both hashed and non-hashed entry objects, since they
  59. represent keys and key types the same way.
  60. @ivar keyType: The type of the key; either ssh-dss or ssh-rsa.
  61. @type keyType: L{bytes}
  62. @ivar publicKey: The server public key indicated by this line.
  63. @type publicKey: L{twisted.conch.ssh.keys.Key}
  64. @ivar comment: Trailing garbage after the key line.
  65. @type comment: L{bytes}
  66. """
  67. def __init__(self, keyType, publicKey, comment):
  68. self.keyType = keyType
  69. self.publicKey = publicKey
  70. self.comment = comment
  71. def matchesKey(self, keyObject):
  72. """
  73. Check to see if this entry matches a given key object.
  74. @param keyObject: A public key object to check.
  75. @type keyObject: L{Key}
  76. @return: C{True} if this entry's key matches C{keyObject}, C{False}
  77. otherwise.
  78. @rtype: L{bool}
  79. """
  80. return self.publicKey == keyObject
  81. @implementer(IKnownHostEntry)
  82. class PlainEntry(_BaseEntry):
  83. """
  84. A L{PlainEntry} is a representation of a plain-text entry in a known_hosts
  85. file.
  86. @ivar _hostnames: the list of all host-names associated with this entry.
  87. @type _hostnames: L{list} of L{bytes}
  88. """
  89. def __init__(self, hostnames, keyType, publicKey, comment):
  90. self._hostnames = hostnames
  91. super().__init__(keyType, publicKey, comment)
  92. @classmethod
  93. def fromString(cls, string):
  94. """
  95. Parse a plain-text entry in a known_hosts file, and return a
  96. corresponding L{PlainEntry}.
  97. @param string: a space-separated string formatted like "hostname
  98. key-type base64-key-data comment".
  99. @type string: L{bytes}
  100. @raise DecodeError: if the key is not valid encoded as valid base64.
  101. @raise InvalidEntry: if the entry does not have the right number of
  102. elements and is therefore invalid.
  103. @raise BadKeyError: if the key, once decoded from base64, is not
  104. actually an SSH key.
  105. @return: an IKnownHostEntry representing the hostname and key in the
  106. input line.
  107. @rtype: L{PlainEntry}
  108. """
  109. hostnames, keyType, key, comment = _extractCommon(string)
  110. self = cls(hostnames.split(b","), keyType, key, comment)
  111. return self
  112. def matchesHost(self, hostname):
  113. """
  114. Check to see if this entry matches a given hostname.
  115. @param hostname: A hostname or IP address literal to check against this
  116. entry.
  117. @type hostname: L{bytes}
  118. @return: C{True} if this entry is for the given hostname or IP address,
  119. C{False} otherwise.
  120. @rtype: L{bool}
  121. """
  122. if isinstance(hostname, str):
  123. hostname = hostname.encode("utf-8")
  124. return hostname in self._hostnames
  125. def toString(self):
  126. """
  127. Implement L{IKnownHostEntry.toString} by recording the comma-separated
  128. hostnames, key type, and base-64 encoded key.
  129. @return: The string representation of this entry, with unhashed hostname
  130. information.
  131. @rtype: L{bytes}
  132. """
  133. fields = [
  134. b",".join(self._hostnames),
  135. self.keyType,
  136. _b64encode(self.publicKey.blob()),
  137. ]
  138. if self.comment is not None:
  139. fields.append(self.comment)
  140. return b" ".join(fields)
  141. @implementer(IKnownHostEntry)
  142. class UnparsedEntry:
  143. """
  144. L{UnparsedEntry} is an entry in a L{KnownHostsFile} which can't actually be
  145. parsed; therefore it matches no keys and no hosts.
  146. """
  147. def __init__(self, string):
  148. """
  149. Create an unparsed entry from a line in a known_hosts file which cannot
  150. otherwise be parsed.
  151. """
  152. self._string = string
  153. def matchesHost(self, hostname):
  154. """
  155. Always returns False.
  156. """
  157. return False
  158. def matchesKey(self, key):
  159. """
  160. Always returns False.
  161. """
  162. return False
  163. def toString(self):
  164. """
  165. Returns the input line, without its newline if one was given.
  166. @return: The string representation of this entry, almost exactly as was
  167. used to initialize this entry but without a trailing newline.
  168. @rtype: L{bytes}
  169. """
  170. return self._string.rstrip(b"\n")
  171. def _hmacedString(key, string):
  172. """
  173. Return the SHA-1 HMAC hash of the given key and string.
  174. @param key: The HMAC key.
  175. @type key: L{bytes}
  176. @param string: The string to be hashed.
  177. @type string: L{bytes}
  178. @return: The keyed hash value.
  179. @rtype: L{bytes}
  180. """
  181. hash = hmac.HMAC(key, digestmod=sha1)
  182. if isinstance(string, str):
  183. string = string.encode("utf-8")
  184. hash.update(string)
  185. return hash.digest()
  186. @implementer(IKnownHostEntry)
  187. class HashedEntry(_BaseEntry, FancyEqMixin):
  188. """
  189. A L{HashedEntry} is a representation of an entry in a known_hosts file
  190. where the hostname has been hashed and salted.
  191. @ivar _hostSalt: the salt to combine with a hostname for hashing.
  192. @ivar _hostHash: the hashed representation of the hostname.
  193. @cvar MAGIC: the 'hash magic' string used to identify a hashed line in a
  194. known_hosts file as opposed to a plaintext one.
  195. """
  196. MAGIC = b"|1|"
  197. compareAttributes = ("_hostSalt", "_hostHash", "keyType", "publicKey", "comment")
  198. def __init__(self, hostSalt, hostHash, keyType, publicKey, comment):
  199. self._hostSalt = hostSalt
  200. self._hostHash = hostHash
  201. super().__init__(keyType, publicKey, comment)
  202. @classmethod
  203. def fromString(cls, string):
  204. """
  205. Load a hashed entry from a string representing a line in a known_hosts
  206. file.
  207. @param string: A complete single line from a I{known_hosts} file,
  208. formatted as defined by OpenSSH.
  209. @type string: L{bytes}
  210. @raise DecodeError: if the key, the hostname, or the is not valid
  211. encoded as valid base64
  212. @raise InvalidEntry: if the entry does not have the right number of
  213. elements and is therefore invalid, or the host/hash portion contains
  214. more items than just the host and hash.
  215. @raise BadKeyError: if the key, once decoded from base64, is not
  216. actually an SSH key.
  217. @return: The newly created L{HashedEntry} instance, initialized with the
  218. information from C{string}.
  219. """
  220. stuff, keyType, key, comment = _extractCommon(string)
  221. saltAndHash = stuff[len(cls.MAGIC) :].split(b"|")
  222. if len(saltAndHash) != 2:
  223. raise InvalidEntry()
  224. hostSalt, hostHash = saltAndHash
  225. self = cls(a2b_base64(hostSalt), a2b_base64(hostHash), keyType, key, comment)
  226. return self
  227. def matchesHost(self, hostname):
  228. """
  229. Implement L{IKnownHostEntry.matchesHost} to compare the hash of the
  230. input to the stored hash.
  231. @param hostname: A hostname or IP address literal to check against this
  232. entry.
  233. @type hostname: L{bytes}
  234. @return: C{True} if this entry is for the given hostname or IP address,
  235. C{False} otherwise.
  236. @rtype: L{bool}
  237. """
  238. return hmac.compare_digest(
  239. _hmacedString(self._hostSalt, hostname), self._hostHash
  240. )
  241. def toString(self):
  242. """
  243. Implement L{IKnownHostEntry.toString} by base64-encoding the salt, host
  244. hash, and key.
  245. @return: The string representation of this entry, with the hostname part
  246. hashed.
  247. @rtype: L{bytes}
  248. """
  249. fields = [
  250. self.MAGIC
  251. + b"|".join([_b64encode(self._hostSalt), _b64encode(self._hostHash)]),
  252. self.keyType,
  253. _b64encode(self.publicKey.blob()),
  254. ]
  255. if self.comment is not None:
  256. fields.append(self.comment)
  257. return b" ".join(fields)
  258. class KnownHostsFile:
  259. """
  260. A structured representation of an OpenSSH-format ~/.ssh/known_hosts file.
  261. @ivar _added: A list of L{IKnownHostEntry} providers which have been added
  262. to this instance in memory but not yet saved.
  263. @ivar _clobber: A flag indicating whether the current contents of the save
  264. path will be disregarded and potentially overwritten or not. If
  265. C{True}, this will be done. If C{False}, entries in the save path will
  266. be read and new entries will be saved by appending rather than
  267. overwriting.
  268. @type _clobber: L{bool}
  269. @ivar _savePath: See C{savePath} parameter of L{__init__}.
  270. """
  271. def __init__(self, savePath):
  272. """
  273. Create a new, empty KnownHostsFile.
  274. Unless you want to erase the current contents of C{savePath}, you want
  275. to use L{KnownHostsFile.fromPath} instead.
  276. @param savePath: The L{FilePath} to which to save new entries.
  277. @type savePath: L{FilePath}
  278. """
  279. self._added = []
  280. self._savePath = savePath
  281. self._clobber = True
  282. @property
  283. def savePath(self):
  284. """
  285. @see: C{savePath} parameter of L{__init__}
  286. """
  287. return self._savePath
  288. def iterentries(self):
  289. """
  290. Iterate over the host entries in this file.
  291. @return: An iterable the elements of which provide L{IKnownHostEntry}.
  292. There is an element for each entry in the file as well as an element
  293. for each added but not yet saved entry.
  294. @rtype: iterable of L{IKnownHostEntry} providers
  295. """
  296. for entry in self._added:
  297. yield entry
  298. if self._clobber:
  299. return
  300. try:
  301. fp = self._savePath.open()
  302. except OSError:
  303. return
  304. with fp:
  305. for line in fp:
  306. try:
  307. if line.startswith(HashedEntry.MAGIC):
  308. entry = HashedEntry.fromString(line)
  309. else:
  310. entry = PlainEntry.fromString(line)
  311. except (DecodeError, InvalidEntry, BadKeyError):
  312. entry = UnparsedEntry(line)
  313. yield entry
  314. def hasHostKey(self, hostname, key):
  315. """
  316. Check for an entry with matching hostname and key.
  317. @param hostname: A hostname or IP address literal to check for.
  318. @type hostname: L{bytes}
  319. @param key: The public key to check for.
  320. @type key: L{Key}
  321. @return: C{True} if the given hostname and key are present in this file,
  322. C{False} if they are not.
  323. @rtype: L{bool}
  324. @raise HostKeyChanged: if the host key found for the given hostname
  325. does not match the given key.
  326. """
  327. for lineidx, entry in enumerate(self.iterentries(), -len(self._added)):
  328. if entry.matchesHost(hostname) and entry.keyType == key.sshType():
  329. if entry.matchesKey(key):
  330. return True
  331. else:
  332. # Notice that lineidx is 0-based but HostKeyChanged.lineno
  333. # is 1-based.
  334. if lineidx < 0:
  335. line = None
  336. path = None
  337. else:
  338. line = lineidx + 1
  339. path = self._savePath
  340. raise HostKeyChanged(entry, path, line)
  341. return False
  342. def verifyHostKey(self, ui, hostname, ip, key):
  343. """
  344. Verify the given host key for the given IP and host, asking for
  345. confirmation from, and notifying, the given UI about changes to this
  346. file.
  347. @param ui: The user interface to request an IP address from.
  348. @param hostname: The hostname that the user requested to connect to.
  349. @param ip: The string representation of the IP address that is actually
  350. being connected to.
  351. @param key: The public key of the server.
  352. @return: a L{Deferred} that fires with True when the key has been
  353. verified, or fires with an errback when the key either cannot be
  354. verified or has changed.
  355. @rtype: L{Deferred}
  356. """
  357. hhk = defer.execute(self.hasHostKey, hostname, key)
  358. def gotHasKey(result):
  359. if result:
  360. if not self.hasHostKey(ip, key):
  361. ui.warn(
  362. "Warning: Permanently added the %s host key for "
  363. "IP address '%s' to the list of known hosts."
  364. % (key.type(), nativeString(ip))
  365. )
  366. self.addHostKey(ip, key)
  367. self.save()
  368. return result
  369. else:
  370. def promptResponse(response):
  371. if response:
  372. self.addHostKey(hostname, key)
  373. self.addHostKey(ip, key)
  374. self.save()
  375. return response
  376. else:
  377. raise UserRejectedKey()
  378. keytype = key.type()
  379. if keytype == "EC":
  380. keytype = "ECDSA"
  381. prompt = (
  382. "The authenticity of host '%s (%s)' "
  383. "can't be established.\n"
  384. "%s key fingerprint is SHA256:%s.\n"
  385. "Are you sure you want to continue connecting (yes/no)? "
  386. % (
  387. nativeString(hostname),
  388. nativeString(ip),
  389. keytype,
  390. key.fingerprint(format=FingerprintFormats.SHA256_BASE64),
  391. )
  392. )
  393. proceed = ui.prompt(prompt.encode(sys.getdefaultencoding()))
  394. return proceed.addCallback(promptResponse)
  395. return hhk.addCallback(gotHasKey)
  396. def addHostKey(self, hostname, key):
  397. """
  398. Add a new L{HashedEntry} to the key database.
  399. Note that you still need to call L{KnownHostsFile.save} if you wish
  400. these changes to be persisted.
  401. @param hostname: A hostname or IP address literal to associate with the
  402. new entry.
  403. @type hostname: L{bytes}
  404. @param key: The public key to associate with the new entry.
  405. @type key: L{Key}
  406. @return: The L{HashedEntry} that was added.
  407. @rtype: L{HashedEntry}
  408. """
  409. salt = secureRandom(20)
  410. keyType = key.sshType()
  411. entry = HashedEntry(salt, _hmacedString(salt, hostname), keyType, key, None)
  412. self._added.append(entry)
  413. return entry
  414. def save(self):
  415. """
  416. Save this L{KnownHostsFile} to the path it was loaded from.
  417. """
  418. p = self._savePath.parent()
  419. if not p.isdir():
  420. p.makedirs()
  421. if self._clobber:
  422. mode = "wb"
  423. else:
  424. mode = "ab"
  425. with self._savePath.open(mode) as hostsFileObj:
  426. if self._added:
  427. hostsFileObj.write(
  428. b"\n".join([entry.toString() for entry in self._added]) + b"\n"
  429. )
  430. self._added = []
  431. self._clobber = False
  432. @classmethod
  433. def fromPath(cls, path):
  434. """
  435. Create a new L{KnownHostsFile}, potentially reading existing known
  436. hosts information from the given file.
  437. @param path: A path object to use for both reading contents from and
  438. later saving to. If no file exists at this path, it is not an
  439. error; a L{KnownHostsFile} with no entries is returned.
  440. @type path: L{FilePath}
  441. @return: A L{KnownHostsFile} initialized with entries from C{path}.
  442. @rtype: L{KnownHostsFile}
  443. """
  444. knownHosts = cls(path)
  445. knownHosts._clobber = False
  446. return knownHosts
  447. class ConsoleUI:
  448. """
  449. A UI object that can ask true/false questions and post notifications on the
  450. console, to be used during key verification.
  451. """
  452. def __init__(self, opener):
  453. """
  454. @param opener: A no-argument callable which should open a console
  455. binary-mode file-like object to be used for reading and writing.
  456. This initializes the C{opener} attribute.
  457. @type opener: callable taking no arguments and returning a read/write
  458. file-like object
  459. """
  460. self.opener = opener
  461. def prompt(self, text):
  462. """
  463. Write the given text as a prompt to the console output, then read a
  464. result from the console input.
  465. @param text: Something to present to a user to solicit a yes or no
  466. response.
  467. @type text: L{bytes}
  468. @return: a L{Deferred} which fires with L{True} when the user answers
  469. 'yes' and L{False} when the user answers 'no'. It may errback if
  470. there were any I/O errors.
  471. """
  472. d = defer.succeed(None)
  473. def body(ignored):
  474. with closing(self.opener()) as f:
  475. f.write(text)
  476. while True:
  477. answer = f.readline().strip().lower()
  478. if answer == b"yes":
  479. return True
  480. elif answer == b"no":
  481. return False
  482. else:
  483. f.write(b"Please type 'yes' or 'no': ")
  484. return d.addCallback(body)
  485. def warn(self, text):
  486. """
  487. Notify the user (non-interactively) of the provided text, by writing it
  488. to the console.
  489. @param text: Some information the user is to be made aware of.
  490. @type text: L{bytes}
  491. """
  492. try:
  493. with closing(self.opener()) as f:
  494. f.write(text)
  495. except Exception:
  496. log.failure("Failed to write to console")