Last active
September 25, 2024 09:50
-
-
Save s1037989/1f665f82aba93628db692636c4392e65 to your computer and use it in GitHub Desktop.
Process Trust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This demonstrates the concept for a program to only execute by a trusted caller | |
# This demonstration uses an extremely simple algorithm, but of course the idea is only to make it more annoyng for someone to spoof the caller | |
# as the attacker needs to know the algorithm | |
# And of course caller and callee need to implement the exact same algorithm pair | |
# Indeed, use RSA to make this idea more cryptographically secure | |
$ perl a 123 # "a" single argument is a filename | |
{ | |
"a" => { | |
"file" => 123, # message dumped to stderr for inspection | |
"validated" => 1 | |
} | |
} | |
eyJmaWxlIjoiMTIzIiwidmFsaWRhdGVkIjoxfQ==--23859--38 # signature and public key are random | |
$ perl a 123 | |
{ | |
"a" => { | |
"file" => 123, | |
"validated" => 1 | |
} | |
} | |
eyJmaWxlIjoiMTIzIiwidmFsaWRhdGVkIjoxfQ==--58563--86 # signature and public key are random | |
$ perl b eyJmaWxlIjoiMTIzIiwidmFsaWRhdGVkIjoxfQ==--58563--86 # arguments to b can be in the message itself or additional arguments | |
b processing validated file 123 | |
[123] processed | |
$ perl b $(perl a $RANDOM) | |
{ | |
"a" => { | |
"file" => 15946, # message dumped to stderr for inspection | |
"validated" => 1 | |
} | |
} | |
b processing validated file 15946 # message says the file is validated, and the caller is trusted | |
[15946] processed | |
$ perl b $(perl a $RANDOM)$RANDOM # simulate not having the correct public key | |
{ | |
"a" => { | |
"file" => 28490, # message dumped to stderr for inspection | |
"validated" => 1 | |
} | |
} | |
b untrusted caller, not processing file 28490 # message says the file is validated, but the caller is untrusted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.010; | |
use strict; | |
use warnings; | |
use Mojo::File qw(curfile); | |
use Mojo::JSON qw(j); | |
use Mojo::Util qw(b64_encode b64_decode dumper); | |
use lib curfile->sibling->to_string; | |
use Trust; | |
die "Usage: $0 secret length message\n" unless $#ARGV == 2; | |
my ($secret, $len, $message) = ($ARGV[0], $ARGV[1], j({file => $ARGV[2], validated => 1})); | |
warn dumper({$0 => j($message)}); | |
printf "%s--%s--%s\n", (b64_encode($message)=~s/\n$//r), sign_message($secret, $len, $message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.010; | |
use strict; | |
use warnings; | |
use Mojo::File qw(curfile); | |
use Mojo::JSON qw(j); | |
use Mojo::Util qw(b64_encode b64_decode); | |
use lib curfile->sibling->to_string; | |
use Trust; | |
my ($message, $signature, $public_key) = split /--/, $ARGV[0]; | |
my $json = j(b64_decode($message)); | |
die sprintf "$0 called by untrusted caller, not processing file %s\n", $json->{file} unless verify_message(b64_decode($message), $signature, $public_key); | |
printf "$0 processing validated file %s\n", $json->{file} if $json->{validated}; | |
printf "[$json->{file}] processed %s\n", join ' ', $ARGV[0], $ARGV[1]||''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
sub _message_sum { | |
my $message = shift; | |
my $message_sum = 0; | |
foreach my $char (split //, $message) { | |
$message_sum += ord($char); | |
} | |
# warn " $message_sum"; | |
return $message_sum; | |
} | |
sub generate_private_key { _message_sum(shift) + join '', map { int(rand(10)) } 1..(shift||2) } | |
sub generate_public_key { ($_[0] * 3) + 5 } | |
sub sign_message { local $_ = generate_private_key(shift, shift); (_message_sum(pop) * $_, generate_public_key($_)) } | |
sub unsign_message { _message_sum(shift) * ((shift() - 5) / 3) } | |
sub verify_message { unsign_message(@_[0, 2]) == $_[1] } | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment