Finger 79/tcp # McDonald, Dowd and Schuh Challenge: Part 2
Dave G. | November 13th, 2006 | Filed Under: Guests
Login: mdowd Name: Mark Dowd Directory: /guests/mdowd Shell: /bin/ash On since Tue Sep 26 21:55:00 CDT from ??? No Mail. Plan: ---------------------------------- Views expressed by guest bloggers not necessarily those held by Matasano Chargen.
UPDATED (11/14/06 10:33AM/Eastern): This contest is closed. We need to do some judging. I approved all of the comments and answers in the meantime.
UPDATED (11/13/06 7:12PM/Eastern): The prize remains unclaimed. Challenge #3 may be OS specific, but we know it works on at least one OS (Linux).
Obligatory book plug:
We recently finished a book called “The Art of Software Security Assessment: Identifying and Preventing Software Vulnerabilities”. It’s
all about finding security vulnerabilities in software, covering design
review through implementation review across a wide variety of
technologies, including Unix, Windows, network software, and the Web. It
will be published by Addison-Wesley on November 20th, who have provided
a sample chapter.
Note: In our previous post to this blog we indicated that the book would
be available on November 15th. The reason for the date change is that
there was evidently a problem with printing, which has delayed the book
by a few days. Sorry for the inconvenience!
But enough chat, on with the challenges!
—
Here’s the first one. Assume getoriginip() is only going to be 16 bytes maximum, and that you can provide a very long string to be placed in the
banner through the varargs. Also, assume that there isn’t a format string issue.
int banner(int origin, char *fmt, ...) { char buf[1024], *ptr = buf, *origin_string; size_t maxsize = sizeof(buf) - 1; va_list ap; ... switch(origin){ case LOCAL: origin_string = "Local Connection"; break; case REMOTE: origin_string = "Remote Connection"; break; default: origin_string = "Unknown Connection"; break; } sprintf(ptr, "|%s| ", origin_string); maxsize -= strlen(ptr); ptr += strlen(ptr); ... sprintf(ptr, "%s: ", get_origin_ip()); ptr += strlen(ptr); maxsize -= strlen(ptr); ... va_start(ap, fmt); vsnprintf(ptr, maxsize, fmt, ap); va_end(ap);
—
This next one contains some code to remove a variable from the program
environment, say to strip out LD_PRELOAD for a setuid program in ld.so.
Assume you can specify any environment you want, but this code will be
run after an execve() so it’s guaranteed to be somewhat well-formed. The
first argument, var, is a pointer to the name of the environment
variable to be stripped out:
static void _dl_unsetenv(const char *var, char **env) { char *ep; while ((ep = *env)) { const char *vp = var; while (*vp && *vp == *ep) { vp++; ep++; } if (*vp == '\\0' && *ep++ == '=') { char **P; for (P = env;; ++P) if (!(*P = *(P + 1))) break; } env++; } }
—
Lastly, let’s consider a program that is running with setuid root
privileges and writing to a sensitive file. The file is line-based and
contains username/password pairs of the form “username:password”. If
“password” is omitted, then that user has no password. Assume that when
the function is called, the users credentials that correspond to
“username” have already been established, and this function updates
their password. You can also assume that the file_entries list has been
filled out by reading in the information from the privileged
(non-readable) password file, and that the /data directory is not
writable by non-root users. Here’s the code:
#define TEMP_PASSWORD_FILE "/data/passwords.tmp" #define PASSWORD_FILE "/data/passwords" struct password_entry { unsigned char user[32]; unsigned char pass[32]; struct password_entry *next; }; struct password_entry *file_entries; int update_password(unsigned char *username, unsigned char *password) { int i, fd, rc = -1; struct password_entry *ent; if(!username || !password) return -1; for(i = 0; password[i]; i++) { if(!isalnum(password[i])) return -1; } block_signals(); umask(022); if((fd = open(TEMP_PASSWORD_FILE, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, S_IRUSR|S_IWUSER)) < 0) { unblock_signals(); return -1; } for(ent = file_entries; ent; ent = ent->next) { unsigned char buffer[256]; if(strcmp(ent->user, username) == 0) snprintf(buffer, sizeof(buffer), "%.32s:%.32s\\n", ent->user,password); else snprintf(buffer, sizeof(buffer), "%.32s:%.32s\\n", ent->user,ent->pass); if(write(fd, buffer, strlen(buffer)) < 0) goto epilog; } rc = rename(TEMP_PASSWORD_FILE, PASSWORD_FILE); epilog: close(fd); unlink(TEMP_PASSWORD_FILE); unblock_signals(); return rc; }


Add New Comment
Viewing 23 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks