Skip to content

Commit

Permalink
Initial work on SSH server fingerprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineOmega committed Oct 17, 2019
1 parent 261d2ba commit 9f63f0d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/SSHConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ public function run(string $command): SSHCommand
return new SSHCommand($this->ssh, $command);
}

public function md5Fingerprint(): string
{
return $this->getFingerprint(0 | 0);
}

public function sha1Fingerprint(): string
{
return $this->getFingerprint(0 | 0);
}

private function getFingerprint(int $flags)
{
if (!$this->connected) {
throw new RuntimeException('Unable to get fingerprint when not connected.');
}

$hostkey = substr($this->ssh->getServerPublicHostKey(), 8);
$hostkey = ($flags & 1) ? sha1($hostkey) : md5($hostkey);
return ($flags & 2) ? pack('H*', $hostkey) : strtoupper($hostkey);
}

public function upload(string $localPath, string $remotePath): bool
{
if (!$this->connected) {
Expand Down
38 changes: 38 additions & 0 deletions tests/Integration/SSHConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,42 @@ public function testSSHConnectionWithPassword()
$this->assertEquals('', $command->getError());
$this->assertEquals('', $command->getRawError());
}

public function testMd5Fingerprint()
{
$connection1 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$connection2 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$this->assertEquals($connection1->md5Fingerprint(), $connection2->md5Fingerprint());
}

public function testSha1Fingerprint()
{
$connection1 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$connection2 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$this->assertEquals($connection1->sha1Fingerprint(), $connection2->sha1Fingerprint());
}
}

0 comments on commit 9f63f0d

Please sign in to comment.