source: pwntcha/trunk/src/authimage.c @ 438

Last change on this file since 438 was 430, checked in by Sam Hocevar, 16 years ago
  • support for -s / --share
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1/*
2 * authimage.c: decode authimage captchas
3 * $Id: authimage.c 430 2005-01-06 00:51:07Z sam $
4 *
5 * Copyright: (c) 2005 Sam Hocevar <sam@zoy.org>
6 *   This program is free software; you can redistribute it and/or
7 *   modify it under the terms of the Do What The Fuck You Want To
8 *   Public License as published by Banlu Kemiyatorn. See
9 *   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <limits.h>
16#include <math.h>
17
18#include "config.h"
19#include "common.h"
20
21#define FONTNAME "font_authimage.png"
22static struct image *font = NULL;
23
24/* Main function */
25char *decode_authimage(struct image *img)
26{
27    char *all = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
28    char *result;
29    struct image *tmp1, *tmp2, *tmp3;
30    int x, y, r, g, b, i;
31
32    if(!font)
33    {
34        char fontname[BUFSIZ];
35        sprintf(fontname, "%s/%s", share, FONTNAME);
36        font = image_load(fontname);
37        if(!font)
38        {
39            fprintf(stderr, "cannot load font %s\n", fontname);
40            exit(-1);
41        }
42    }
43
44    /* authimage captchas have 6 characters */
45    result = malloc(7 * sizeof(char));
46    memset(result, '\0', 7);
47
48    /* half the captchas are inverse video; we set them back to normal */
49    tmp1 = filter_scale(img, 2.0);
50    getpixel(img, 0, 0, &r, &g, &b);
51    tmp2 = filter_equalize(tmp1, r * 3 / 4);
52    tmp3 = filter_smooth(tmp2);
53
54    for(i = 0; i < 6; i++)
55    {
56        int mindiff = INT_MAX, minch = -1, ch;
57        for(ch = 0; ch < 36; ch++)
58        {
59            int diff = 0;
60            for(y = 0; y < 7; y++)
61            {
62                for(x = 0; x < 5; x++)
63                {
64                    int newx, newy, r2;
65                    newx = 35.0 + (x + 6 * i) * 218.0 / 34.0 + y * 5.0 / 6.0 + 0.5;
66                    newy = 33.0 - (x + 6 * i) * 18.0 / 34.0 + y * 42.0 / 6.0 + 0.5;
67                    getpixel(tmp3, newx, newy, &r, &g, &b);
68                    getpixel(font, x + 6 * ch, y, &r2, &g, &b);
69                    r = (r < 220) ? 0 : 255;
70                    diff += (r - r2) * (r - r2);
71                }
72            }
73            if(diff < mindiff)
74            {
75                mindiff = diff;
76                minch = ch;
77            }
78        }
79        result[i] = all[minch];
80    }
81
82    image_free(tmp3);
83    image_free(tmp2);
84    image_free(tmp1);
85
86    return result;
87}
88
Note: See TracBrowser for help on using the repository browser.