-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fit remote code execution in history and template imports functionality #1625
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,14 @@ | |
|
||
$alertmsg = ''; // anything here pops up in an alert box | ||
|
||
if (!empty($_POST)) { | ||
if (!isset($_POST['token'])) { | ||
error_log('WARNING: A POST request detected with no csrf token found'); | ||
die('Authentication failed.'); | ||
} else if (!hash_equals(hash_hmac('sha256', '/letter.php.theform', $_SESSION['token']), $_POST['token'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
die('Authentication failed.'); | ||
} | ||
} | ||
// If the Generate button was clicked... | ||
if ($_POST['formaction']=="generate") { | ||
|
||
|
@@ -430,6 +438,7 @@ function insertAtCursor(myField, myValue) { | |
<form method='post' action='letter.php' id="theform" name="theform"> | ||
<input type="hidden" name="formaction" id="formaction" value=""> | ||
<input type='hidden' name='form_pid' value='<?php echo $pid ?>' /> | ||
<input type='hidden' name='token' value="<?php echo hash_hmac('sha256', '/letter.php.theform', $_SESSION['token']);?>" /> | ||
|
||
<center> | ||
<p> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
class CsrfToken { | ||
|
||
// Generate csrf token for authentication | ||
function generateCsrfToken() | ||
{ | ||
if (!extension_loaded('openssl')) { | ||
error_log("ERROR: openssl extension not enabled, needed for proper functioning of LibreHealth"); | ||
die("Error: Systems needs openssl."); | ||
} | ||
|
||
$csrfToken = bin2hex(openssl_random_pseudo_bytes(32)); | ||
|
||
if (empty($csrfToken)) { | ||
error_log("ERROR : Token generation failed"); | ||
die("Error : Unable to correctly generate token"); | ||
} | ||
|
||
return $csrfToken; | ||
} | ||
|
||
// Function to verify a csrf token | ||
function verifyCsrfToken($token) | ||
{ | ||
if (hash_equals($_SESSION['token'], $token)) { | ||
return true; | ||
} else { | ||
error_log("WARNING : Malicious attempt encountered"); | ||
return false; | ||
} | ||
} | ||
// Function to verify a csrf token using with second token | ||
function verifyCsrfTokenAndCompareHash($token, $secondToken) | ||
{ | ||
if (hash_equals(hash_hmac('sha256', $secondToken, $_SESSION['token']), $token) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're missing a closing parenthesis here too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its necessary to type cast the inputs before supplying to hash_hmac, interesting things might occur if that is not done ( type juggling vulnerability ), if $secondToken is a user controlled input, if an array is passed, hash_hmac will return null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks @naveen17797 I will add that too. |
||
return true; | ||
} else { | ||
error_log("WARNING : Malicious attempt encountered"); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,5 +55,4 @@ function image_has_right_size($size) { | |
return $size < 20971520; | ||
} | ||
|
||
|
||
?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,16 @@ | |
$sanitize_all_escapes=true; | ||
$fake_register_globals=false; | ||
require_once("../interface/globals.php"); | ||
require_once("../library/CsrfToken.php"); | ||
|
||
if (!empty($_POST)) { | ||
if (!isset($_POST['token'])) { | ||
error_log('WARNING: A POST request detected with no csrf token found'); | ||
die('Authentication failed.'); | ||
} else if (!(CsrfToken::verifyCsrfToken($_POST['token'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're missing a closing parenthesis here too |
||
die('Authentication failed.'); | ||
} | ||
} | ||
if($_POST['mode'] == 'get'){ | ||
echo file_get_contents($_POST['docid']); | ||
exit; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're missing a closing parenthesis here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @maggienegm. I am guessing you fixed in here PR #1634?? have not checked yet. If you did then I can just close this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @Ngai-E , I fixed the closing parenthesis issues there