an image of some pajamas!

pajamas..

php and javascript advanced md5 authentication system

The connexion between "pajamas", which is an acronym for "php and javascript advanced md5 authentication system" and an actual pair of "Pyjamas" is that when you feel secure, you sleep better. Of course the words sound identical, too.

pajamas began as an attempt to create a more secure login using client-side hashing, which is one-way encryption, and as a demonstration, mainly for other web-coders, to two enormous security holes in all-too-common existence..

The first exists when folk use "public" browsers. Often the username and password are stored on the machine, and can be re-used, even by accident, by other members of the public. Clearly this presents a problem, and one which, for some reason, most people like to forget and/or ignore. Probably, like me, they realized it would need to be done with JavaScript, and then ran in the opposite direction.

The second hole is more obvious, and that is the plain text password travelling freely across the wires. This one has received more worldwide attention, but it's still ignored in most php web applications. There are literally hundreds of articles out there describing how to store a user's password using all manner of weird and wonderful algorithms, to protect it from "unauthorised database access", or "unauthorised server access", and yet still expecting the password to arrive in plain text. GUYS!

If your database isn't secure you are in trouble. Same for your filesystem. These are places over which you have control. The place where you certainly don't have control, ever, is the internet. And the moment that packet of data leaves the user's presumably safe environment it's fair game. Its contents could be stored on any one of the many nodes between server and client, dubious proxy "servers" could scan it, on poorly configured servers (most) passwords will show up on other server's referrer logs, etc. There it is, your plain-text password, travelling around the internet in a bundle of other useful plain text information, like the URL of the so-called secure login page, probably your username. No! This is insane!

pajamas takes an entirely different approach. The password is securely hashed before being sent over the wires. Now, not only is interception no longer a problem (it's impossible to retrieve the password from the hash in the given time-frame, even a very much longer time-frame), but public browsers can't save or cache it, either*, being a one-shot mish-mash of your password and some random generated string. Each time you login, it's completely different.

With pajamas, the only places the password ever exists in the clear are in your presumably secure server environment, and the presumably secure user's brain. This password is only protecting access to this server's content; ergo, breaching the web server breaches the user's "protected" data, anyway. To my reckoning, it would be considerably more difficult to compromise a modern web server and get root, than it is to search some log for the phrase "password".

Which strategy is best? You decide.

Since its birth back in 2004, pajamas spent quite a long time lying fairly dormant, yet working away quietly in the background as a highly useful authentication script. More recently, pajamas has grown into a rather neat modular authentication system, and the old "pj" module has essentially become one of its plug-ins. There's also a "plain" plugin that retains many of the good features of pj, but without the client-side hashing, for situations where JavaScript isn't available (On The Moon, maybe!). pj's client-side hashing is made possible with the excellent JavaScript functions provided by Paul Johnston's javascript MD5 code.

You can enjoy my wee "protected" image gallery, and try-out pajamas at the same time, here.
If you'd like to ask questions, give feedback, enlighten me, etc, you can do that at the bottom.

The default module on corz.org uses HMAC-SHA256 over HTTPS. The browser signs a fresh server challenge using Web Crypto. See the SHA256 setup example.

There is also a sha1 pajamas plug-in called "shaggie", which is currently available only inside my other packages (e.g. the distro machine), feel free to download and play around with it; get back to me if you find any issues, thanks.

Here's the current pajamas code..
(of course, most of the good stuff is inside the modules!)
  1. <?php // ۞// text { encoding:utf-8 ; bom:no ; linebreaks:unix ; tabs:4sp ; }
  2. if (realpath ($_SERVER['SCRIPT_FILENAME'] ) == realpath ( __FILE__ )) {
  3. @include $_SERVER['DOCUMENT_ROOT'].'/inc/source-dump.php';
  4. source_dump(__FILE__); }
  5. /*
  6. pajamas
  7. modular authentication system
  8. For full details, usage instructions, etc, see the accomanying readme.
  9. If you you got this without a readme, check out the link below.
  10. If you need help, mail me @ corz.org, or if you think a solution to your
  11. issue would be valuable to others, drop a comment on the pajamas page..
  12. https://corz.org/server/security/pajamas.php
  13. Have fun!
  14. ;o)
  15. ps.. Big thanks to PCheese for all the help with the OOP.
  16. d00D! I get it now! And I hope you like where I took it. ;o)
  17. (c) 2004->tomorrow! ~ corz.org ;o)
  18. Please view the license for this free software, here:
  19. https://corz.org/free-scripts-licence.nfo
  20. */
  21. /*
  22. pajamas authentication..
  23. */
  24. class pajamas {
  25. /*
  26. version
  27. Currently, we have the bare bones of the thing, and it works great!
  28. There may yet be bugs, and all bug reports are wholeheartedly welcomed.
  29. */
  30. var $_version = '0.3';
  31. /* public properties.. */
  32. /*
  33. default module
  34. (string) name of module file (minus .php extension)
  35. current choices are 'plain' or 'pj' (the best)
  36. If, for some insane reason, you don't have access to a JavaScript-
  37. capable browser, use 'plain', otherwise, use 'pj'.
  38. the plain module can be configured in exactly the same way as
  39. the pajamas module, and has many of its features, too; sessions, IP
  40. check, time-out, etc., the only difference being that the password is
  41. sent over the wire in plain text.
  42. Though unlike HTTP basic authentication, which send the password with
  43. every single request, with the plain module, the password travels over
  44. the wires one time only.
  45. */
  46. var $_default_module = 'sha256';
  47. /*
  48. password
  49. (string) default: 'password';
  50. a quick and dirty way to store your password..
  51. or you could keep it in a database, or include from another file,.
  52. include '/some/other/place/config.php'; and set it externally..
  53. $auth->_login_password = 'MyPassword';
  54. Passwords are case-sensitive.
  55. */
  56. var $_login_password = 'password';
  57. /*
  58. IP address check?
  59. boolean (true/false) default: true;
  60. normally, we check the IP address of the authorising browser. However, if the user
  61. is behind a proxy farm (very unusual), this will break his session, as his IP will
  62. change with (possibly) each request. If you have users behind proxy farms, (or you
  63. are) set this to false, or else advise them to use *yet another* proxy (two proxies).
  64. */
  65. var $_check_ip = true;
  66. /*
  67. do time-out?
  68. boolean default: true;
  69. we can specify a time-out for the session. if you set this to false,
  70. the session is live until the client's browser is quit, or they log out.
  71. */
  72. var $_do_time_out = true;
  73. /*
  74. time-out
  75. integer (in minutes) default: 60;
  76. an hour is reasonable, anything goes. the demo uses 0.5 (30 seconds)
  77. */
  78. var $_session_time = 60;
  79. /*
  80. big luser
  81. integer (max failed attempts) default: 10;
  82. they tried and tried, but it just isn't happening. Or else they are taking the p*ss.
  83. A script perhaps, some brute force. Whatever, it would probably be best for everyone
  84. if we halted them in their tracks after how many failed login attempts?
  85. */
  86. var $_big_luser = 10;
  87. /*
  88. kick bad users?
  89. boolean (true/false) default: false;
  90. optionally we can prevent even correct logins from browsers that repeatedly sent bad logins..
  91. If you set this to true, after($_big_luser) failed login attempts, the property
  92. "$auth->_bad_user" will be set to true. Now, even a correct login will fail to authenticate.
  93. You can check for bad users, and then do what you like with them..
  94. if ($auth->_bad_user) { die('go away!');
  95. If you leave kick_bad_users set to false, a correct login will override all previous bad logins.
  96. The idea is, someone may be attempting to login from your terminal, and fail, so they receive
  97. a message informing them of the futility of it, *hopefully* they will stop now. If the *real*
  98. admin comes along, he should be able to log straight in, and shouldn't have the inconvenience
  99. of restarting the browser just because some twat was fooling around. But you can disable this
  100. behaviour by simply setting this to true.
  101. */
  102. var $_kick_bad_users = false;
  103. /*
  104. show error messages?
  105. boolean (true/false) default: true;
  106. pj generates some messages for the various error conditions, you can use these however
  107. you like, and latest message is always in "_auth_message"
  108. If you like, you can have pj display these messages just above the login form,
  109. so the user is aware that their password was incorrect, or whatever..
  110. */
  111. var $_do_messages = true;
  112. /*
  113. create containing forms?
  114. boolean (true/false) default: true;
  115. If you are already inside a form, set this to false to avoid nesting forms,
  116. which will break xhtml valiadation, among other things (including the md5)..
  117. $auth->_createForms = false;
  118. Remember you can also pass "true" to your form input function, to have a
  119. simple, div-less output, like this..
  120. echo $auth->getLoginForm(true);
  121. */
  122. var $_createForms = true;
  123. /*
  124. autocomplete="off"
  125. boolean (true/false) default: true; (doesn't validate, but hey!)
  126. a good, mostly supported proprietary Internet Explorer property.
  127. This will break strict xhtml validation (which is annoying), but you may feel
  128. that it's worth it. With this set to true, browsers will not annoy you to try
  129. and save the password (which, at least with the 'pj' module, is a one-shot
  130. mish-mash that will become useless the instant you logout).
  131. It will only break your xhtml validation until you login, of course.
  132. Set this to true to add 'autocomplete="off"' to your password field. TADA!
  133. One of the rare occasions where Internet Explorer leads the way! If you are
  134. obsessed with strict xhtml 1.0 validation, screw your users and set this to
  135. false.
  136. btw: if you known an xhtml-friendly way to do this, MAIL ME! ;o)
  137. */
  138. var $_no_autocomplete = true;
  139. /*
  140. code loaction.
  141. (string) default: '';
  142. Some modules may require included code.
  143. In "pj", this sets the default location of javascript MD5 functions file and
  144. will be used to create the script tag that includes the JavaScript MD5
  145. functions on your page, like this..
  146. echo $auth->getAuthCode();
  147. You can override the location by setting this..
  148. $auth->_code_location = 'inc/pajamas/modules/client-side/sha256.js';
  149. *before* you echo the code. relative or absolute paths are fine, just like a
  150. regular javascript include.
  151. */
  152. var $_code_location = '';
  153. /*
  154. a simple error catcher.
  155. boolean (true/false) default: true;
  156. session errors of level "E_NOTICE" (type 8) will be caught and stored,
  157. rather than spew onto your page. use..
  158. echo $auth->getErrors();
  159. to see them, or pipe that somewhere else. essentially this saves having
  160. your pages all messed up when you are testing your pj implementation on
  161. a server with error level E_ALL set (all development servers, yeah?),
  162. mainly for "session already started" type errors. It was annoying!
  163. we throw errors into a global variable because a) I use this system for
  164. errors anyway, (my debug script throws me a pop-up if it finds anything in
  165. $GLOBALS['errors'], and b) variables inside error handlers are a disaster!
  166. */
  167. var $_error_catcher = true;
  168. /*
  169. Private properties.
  170. DO NOT CHANGE THESE.
  171. You shouldn't change anything above either;
  172. set it externally like so..
  173. $auth->_do_time_out = false;
  174. */
  175. var $_auth_message = '';
  176. var $_auth_module = ''; // a pointer to *real* auth class
  177. var $_bad_user = false;
  178. var $_default_uid = 'pajamas';
  179. var $_is_authenticated = null; // start out in unknown (unset) state
  180. var $_module_loaded = false;
  181. var $_modules_path = 'modules/';
  182. var $_interface = 'advanced'; // 'advanced' or 'simple'
  183. // constructor..
  184. function __construct ($uniqueid='') {
  185. if (!empty($uniqueid) and ctype_alnum($uniqueid)) {
  186. $this->_unique_id = $uniqueid;
  187. } else {
  188. $this->_unique_id = $this->_default_uid;
  189. }
  190. if ($this->_error_catcher) {
  191. $this->startErrorHandler();
  192. }
  193. }
  194. function authModule() {
  195. include_once $this->_modules_path.$this->_default_module.'.php';
  196. $this->_auth_module = new authModule($this->_unique_id);
  197. // setup its preferences..
  198. // (we could perhaps check module's capabilities and only set those)
  199. if ($this->_interface != 'simple') {
  200. $this->_auth_module->_login_password = $this->_login_password;
  201. $this->_auth_module->_check_ip = $this->_check_ip;
  202. $this->_auth_module->_do_time_out = $this->_do_time_out;
  203. $this->_auth_module->_session_time = $this->_session_time;
  204. $this->_auth_module->_big_luser = $this->_big_luser;
  205. $this->_auth_module->_kick_bad_users = $this->_kick_bad_users;
  206. $this->_auth_module->_do_messages = $this->_do_messages;
  207. $this->_auth_module->_createForms = $this->_createForms;
  208. $this->_auth_module->_no_autocomplete = $this->_no_autocomplete;
  209. }
  210. $this->_module_loaded = true;
  211. }
  212. function auth_user() {
  213. if (!$this->_module_loaded) { $this->authModule(); }
  214. if ($this->_is_authenticated) { return true; }
  215. $auth_status = $this->_auth_module->auth_user();
  216. $this->_auth_message = $this->_auth_module->_auth_message;
  217. if ($auth_status) {
  218. $this->_is_authenticated = true;
  219. return true;
  220. } else {
  221. $this->_is_authenticated = false;
  222. return false;
  223. }
  224. }
  225. function getAuthCode() {
  226. // this will need to load before authentication,
  227. // so we set it here, and now overriding works as expected.
  228. if ($this->_code_location !== '') { $this->_auth_module->_code_location = $this->_code_location; }
  229. $html = $this->_auth_module->getAuthCode();
  230. if (!empty($html)) { return $html; }
  231. else { return ''; }
  232. }
  233. function getLoginForm($simple=false) {
  234. return $this->_auth_module->getLoginForm($simple);
  235. }
  236. function getLogoutButton($simple=false) {
  237. return $this->_auth_module->getLogoutButton($simple);
  238. }
  239. function getSelf() {
  240. return $_SERVER['SCRIPT_NAME'];
  241. }
  242. function getBadUser() {
  243. return $this->_auth_module->_bad_user;
  244. }
  245. function remainingTime() {
  246. if ($this->_do_time_out) {
  247. $now = explode(' ',microtime());
  248. $time = $now[1].substr($now[0],2,2);
  249. settype($time, 'double'); // 100th/second..
  250. return (($this->_session_time * 6000) - ($time - $_SESSION['auth'.$this->_unique_id]['login_at'])) / 100;
  251. } else { return false; }
  252. }
  253. /*
  254. a simple session error catcher..
  255. */
  256. function getErrors() {
  257. if (!empty($GLOBALS['errors']['pajamas'])) {
  258. return $GLOBALS['errors']['pajamas'];
  259. }
  260. }
  261. function startErrorHandler() {
  262. set_error_handler(array($this, 'handle_error'));
  263. }
  264. function handle_error($type, $string, $file, $line, $vars = null) {
  265. switch (TRUE) {
  266. case ($type == 8 and stristr($string, 'session')): // doesn't look so clever with only one case!
  267. if (empty($GLOBALS['errors']['pajamas'])) { $GLOBALS['errors']['pajamas'] = ''; }
  268. $GLOBALS['errors']['pajamas'] .= 'NOTICE! session error on line '.$line.' of '.$file.': '.$string;
  269. return true;
  270. break; // *ahem*
  271. default:
  272. return false; // better let php handle this one.
  273. }
  274. }
  275. } // end class pajamas()
  276. class pajamasSimple extends pajamas {
  277. function __construct($uniqueid='') {
  278. if (!empty($uniqueid) and strstr($uniqueid, '-')) {
  279. $simple_prefs = explode('-', $uniqueid);
  280. $uniqueid = trim($simple_prefs[0]);
  281. if (!empty($simple_prefs[1])) { $my_module = trim($simple_prefs[1]); }
  282. }
  283. parent::__construct($uniqueid);
  284. $this->_interface = 'simple';
  285. if (!empty($my_module)) { $this->_default_module = $my_module; }
  286. if (!$this->_module_loaded) { $this->authModule(); }
  287. if (!empty($this->_auth_module->_code_location)) {
  288. $this->_code_location = $this->_auth_module->_code_location;
  289. }
  290. echo $this->getAuthCode();
  291. if ($this->_auth_module->auth_user()) {
  292. echo $this->_auth_module->getLogoutButton();
  293. } else {
  294. echo $this->_auth_module->getLoginForm();
  295. }
  296. }
  297. }
  298. /*
  299. version history
  300. 0.3
  301. fixed bug, not passing variables through to getLogoutButton() and getLoginForm()
  302. 0.2
  303. first public release of pajamas engine
  304. 0.1
  305. basic authentication module loader. almost works.
  306. */
  307. ?>

have fun!

;o)


references:
At least, this is the expected behaviour - currently, as far as I know, Opera saves the *typed* password, rather than the *sent* password, effectively defeating all forms of client-side password hashing strategy, dudes! - I've figured out a way around this, by the way, which will hopefully hit the code stage for my upcoming "shaggie" pajamas module. Take it easy!

Welcome to the comments facility!


corz - 17.04.05 10:41 pm

I decided to do a page where folk can leave wisdom, ask questions, correct me, etc.

and here you are!

;o)


stev0 - 08.05.05 4:43 pm

Very nice job, I will use it on my site.


next comments (5 pages)

First, confirm that you are human by entering the code you see..

(if you find the code difficult to decipher, click it for a new one!)


gd verification image

 

Welcome to corz.org!

I'm always messing around with the back-end.. See a bug? Wait a minute and try again. Still see a bug? Mail Me!