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

Last change on this file since 424 was 424, checked in by Sam Hocevar, 17 years ago
  • authimage support
  • broadened scode to trencaspammers
  • Property svn:keywords set to Id
File size: 2.2 KB
RevLine 
[424]1/*
2 * authimage.c: decode authimage captchas
3 * $Id: authimage.c 424 2005-01-05 17:08:50Z 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 "share/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        font = image_load(FONTNAME);
35        if(!font)
36        {
37            fprintf(stderr, "cannot load font %s\n", FONTNAME);
38            exit(-1);
39        }
40    }
41
42    /* authimage captchas have 6 characters */
43    result = malloc(7 * sizeof(char));
44    memset(result, '\0', 7);
45
46    /* half the captchas are inverse video; we set them back to normal */
47    tmp1 = filter_scale(img, 2.0);
48    getpixel(img, 0, 0, &r, &g, &b);
49    tmp2 = filter_equalize(tmp1, r * 3 / 4);
50    tmp3 = filter_smooth(tmp2);
51
52    for(i = 0; i < 6; i++)
53    {
54        int mindiff = INT_MAX, minch = -1, ch;
55        for(ch = 0; ch < 36; ch++)
56        {
57            int diff = 0;
58            for(y = 0; y < 7; y++)
59            {
60                for(x = 0; x < 5; x++)
61                {
62                    int newx, newy, r2;
63                    newx = 35.0 + (x + 6 * i) * 218.0 / 34.0 + y * 5.0 / 6.0 + 0.5;
64                    newy = 33.0 - (x + 6 * i) * 18.0 / 34.0 + y * 42.0 / 6.0 + 0.5;
65                    getpixel(tmp3, newx, newy, &r, &g, &b);
66                    getpixel(font, x + 6 * ch, y, &r2, &g, &b);
67                    r = (r < 220) ? 0 : 255;
68                    diff += (r - r2) * (r - r2);
69                }
70            }
71            if(diff < mindiff)
72            {
73                mindiff = diff;
74                minch = ch;
75            }
76        }
77        result[i] = all[minch];
78    }
79
80    image_free(tmp3);
81    image_free(tmp2);
82    image_free(tmp1);
83
84    return result;
85}
86
Note: See TracBrowser for help on using the repository browser.