source: pwntcha/trunk/src/phpbb.c @ 392

Last change on this file since 392 was 389, checked in by Sam Hocevar, 18 years ago
  • use OpenCV rather than Imlib2 if both are available.
  • cleaned up the slashdot code.
  • decode phpBB captchas.
  • added filter_contrast.
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1/*
2 * phpbb.c: decode phpBB captchas
3 * $Id: phpbb.c 389 2005-01-03 21:48:54Z 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
16#include "config.h"
17#include "common.h"
18
19/* Our macros */
20#define FONTNAME "share/font_phpbb.png"
21
22static struct image *find_glyphs(struct image *img);
23
24/* Global stuff */
25char *result;
26
27/* Main function */
28char *decode_phpbb(struct image *img)
29{
30    char all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
31    struct image *tmp1, *tmp2, *tmp3;
32    struct image *font = image_load(FONTNAME);
33    int x, y, i = 0;
34    int r, g, b;
35    int xmin, xmax, ymin, ymax, cur, offset = -1;
36    int distmin, distx, disty, distch;
37
38    /* phpBB captchas have 6 characters */
39    result = malloc(7 * sizeof(char));
40
41    if(!font)
42    {
43        fprintf(stderr, "cannot load font %s\n", FONTNAME);
44        exit(-1);
45    }
46
47    tmp1 = filter_smooth(img);
48    tmp2 = filter_equalize(tmp1, 128);
49    tmp3 = image_new(img->width, img->height);
50
51    for(x = 0; x < img->width; x++)
52        for(y = 0; y < img->height; y++)
53        {
54            getpixel(tmp2, x, y, &r, &g, &b);
55            if(r == 0 && offset == -1)
56                offset = x;
57            getpixel(img, x, y, &r, &g, &b);
58            setpixel(tmp3, x, y, 255, g, 255);
59        }
60
61    strcpy(result, "       ");
62
63    for(cur = 0; cur < 6; cur++)
64    {
65        /* Try to find 1st letter */
66        distmin = 999999999;
67        for(i = 0; i < 35; i++)
68        {
69            int localmin = 99999999, localx, localy;
70            xmin = i * 40;
71            ymin = 0;
72            xmax = i * 40 + 40;
73            ymax = 40;
74            for(y = 0; y < img->height - 40; y++)
75            {
76                x = offset - 3;
77                if(cur == 0)
78                    x -= 10;
79                if(x < 0)
80                    x = 0;
81                for(; x < offset + 3; x++)
82                {
83                    int z, t, dist;
84                    dist = 0;
85                    for(t = 0; t < ymax - ymin; t++)
86                        for(z = 0; z < xmax - xmin; z++)
87                        {
88                            int r2;
89                            getgray(font, xmin + z, ymin + t, &r);
90                            getgray(tmp2, x + z, y + t, &r2);
91                            dist += (r - r2) * (r - r2);
92                        }
93                    if(dist < localmin)
94                    {
95                        localmin = dist;
96                        localx = x;
97                        localy = y;
98                    }
99                }
100            }
101            if(localmin < distmin)
102            {
103                distmin = localmin;
104                distx = localx;
105                disty = localy;
106                distch = i;
107            }
108        }
109
110        /* Print min glyph (debug) */
111        xmin = distch * 40;
112        ymin = 0;
113        xmax = distch * 40 + 40;
114        ymax = 40;
115        for(y = 0; y < ymax - ymin; y++)
116            for(x = 0; x < xmax - xmin; x++)
117            {
118                int r2;
119                getpixel(font, xmin + x, ymin + y, &r2, &g, &b);
120                if(r2 > 128) continue;
121                getpixel(tmp3, distx + x, disty + y, &r, &g, &b);
122                setpixel(tmp3, distx + x, disty + y, r2, g, b);
123            }
124
125        offset = distx + xmax - xmin;
126        result[cur] = all[distch];
127    }
128
129    image_free(tmp1);
130    image_free(tmp2);
131    image_free(tmp3);
132    image_free(font);
133
134    return result;
135}
136
Note: See TracBrowser for help on using the repository browser.