Resive world

Come world to Record life


  • Home

  • Tags

  • Categories

  • Archives

  • Sitemap

  • Search

网页的防采集系统-token和referer

Posted on 2015-12-07 | In Web

今天在写模拟登陆的时候遇到了一点问题,一个是在post数据中有许多随机串,让人摸不着头脑;另一个问题是明明已经post了正确的数据,然而还是莫名其妙的无法登陆。倒腾了半天终于发现了这原来是很多网站为了防止一些攻击所进行的安全保护措施,分别是token 和 referer防护。

Token

Token实际上就是一个随机串,在含有input表单的页面内以一个‘hidden’表单实现。通常是以以下的形式:

1
2
<input type='hidden' name="sand" value="1449492667"/>
<input type='hidden' name="token" value="9e9f4a4fbab97b3e17c299768f55ee99"/>

这个信息会在post常规数据的时候被连带着post给服务器,服务器会判断token值是否合法来确定是否对我们提交的信息进行处理。因此我们如果要模拟登陆,就要连带这把这些信息post过去。

那么网站这么设计有什么用呢?(很明显不是用来防止模拟登陆的)

1)防止表单重复提交

服务器端第一次验证相同过后,会将session中的Token值更新下,若用户重复提交,第二次的验证判断将失败,因为用户提交的表单中的Token没变,但服务器端session中Token已经改变了。但是当多页面多请求时,必须采用多Token同时生成的方法,这样占用更多资源,执行效率会降低。因此,也可用cookie存储验证信息的方法来代替session Token。比如,应对“重复提交”时,当第一次提交后便把已经提交的信息写到cookie中,当第二次提交时,由于cookie已经有提交记录,因此第二次提交会失败。

2)anti csrf攻击(跨站点请求伪造)。

如果应用于“anti csrf攻击”,则服务器端会对Token值进行验证,判断是否和session中的Token值相等,若相等,则可以证明请求有效,不是伪造的。

Referer

首先referer其实应该拼成referrer,这是一个古老的问题了,不解释。

然后referer的作用其实非常的纯粹,就是告诉服务器这个链接是从哪个站点链接而来的。很多站点会偷偷进行http-referer检查,来保证链接的来源合法,一定程度上起到了安全上的作用(其实主要是为了防止CSRF攻击)。

示例

模拟登陆一个破解md5的网页,用正则获取token值,并且加入referer头进行登陆。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import urllib,urllib2,cookielib,json,re
from bs4 import BeautifulSoup
def md5Decode(cyphertext):
path1='http://www.md5.com.cn/'
path2='http://www.md5.com.cn/md5reverse'
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders=[('User-agent','Opera/9.23'),('Connection','keep-alive')]
urllib2.install_opener(opener)
html1=urllib2.urlopen(path1).read()
sand=re.findall(r'(?<=name=\"sand\" value=\").*?(?=\"/>)',html1)[0]
token=re.findall(r'(?<=name=\"token\" value=\").*?(?=\"/>)',html1)[0]
post_data=urllib.urlencode({'md':cyphertext,'sand':sand,'token':token,'submit':'MD5 Crack'})
req=urllib2.Request(path2,post_data)
opener.addheaders=[('Referer',path1),('Origin',path1)]
urllib2.install_opener(opener)
response=urllib2.urlopen(req)
html=response.read()
soup=BeautifulSoup(html,'lxml')
print re.findall(r'(?<=Result:</label><span class=\"res green\">).*?(?=</span>)',html)[0]
md5Decode('cca9cc444e64c8116a30a00559c042b4')

利用文件头标志判断文件类型

Posted on 2015-12-01 | In Others

简介

我们都知道,文件的扩展名是用来识别文件类型的。通过给他指定扩展名,我们可以告诉自己,也告诉操作系统我们想用什么方式打开这个文件。比如我么会把.jpg的文件默认用图片显示软件打开,.zip 文件会默认用解压软件打开等等。

然而,扩展名完全是可以随便改改的。我们可以给文件设置一个任意的扩展名,当然也可以不设置扩展名。这样一来我们就不能了解到这个文件究竟是做什么的,究竟是个什么样的文件。我们或许也会疑惑,为什么一个软件,比如视频播放器,就能用正确的方式打开.mp4 .rmvb .wmv 等等的视频?

事实上,所有的文件都是以二进制的形式进行存储的,本质上没有差别。之所以使用的方法不同,只是因为我们理解他的方式不同。在每一个文件(包括图片,视频或其他的非ASCII文件)的开头(十六进制表示)实际上都有一片区域来显示这个文件的实际用法,这就是文件头标志。

Linux下我们可以用file命令直接查看文件的实际格式,但是他本质上也是利用文件头标志来进行文件类型判断的。下面就简要介绍下手动判断文件真实类型的方法。

表格

扩展名 文件头标识(HEX) 文件描述
123 00 00 1A 00 05 10 04 Lotus 1-2-3 spreadsheet (v9) file
3gg; 3gp; 3g2 00 00 00 nn 66 74 79 70 33 67 70 3rd Generation Partnership Project 3GPP (nn=0x14)   and 3GPP2 (nn=0x20) multimedia files
7z 37 7A BC AF 27 1C 7-ZIP compressed file
aba 00 01 42 41 Palm Address Book Archive file
abi 41 4F 4C 49 4E 44 45 58 AOL address book index file
aby; idx 41 4F 4C 44 42 AOL database files: address book (ABY) and user   configuration data (MAIN.IDX)
accdb 00 01 00 00 53 74 61 6E 64 61 72 64 20 41 43 45 20   44 42 Microsoft Access 2007 file
ACM 4D 5A MS audio compression manager driver
ADF 44 4F 53 Amiga disk file
adx 03 00 00 00 41 50 50 52 Lotus Approach ADX file
AIFF 46 4F 52 4D 00 Audio Interchange File
ain 21 12 AIN Compressed Archive File
ami 5B 76 65 72 5D Lotus Ami Pro
amr 23 21 41 4D 52 Adaptive Multi-Rate ACELP (Algebraic Code Excited   Linear Prediction) Codec, commonly audio format with GSM cell phones
ANI 52 49 46 46
API 4D 5A 90 00 03 00 00 00 Acrobat plug-in
arc 1A 0x LH archive file, old version(where x = 0x2, 0x3,   0x4, 0x8 or 0x9 for types 1-5, respectively)
arc 41 72 43 01 FreeArc compressed file
arj 60 EA ARJ Compressed Archive
ARJ 60 EA 27
ART 4A 47 03 0E 00 00 00 AOL ART file
ART 4A 47 04 0E 00 00 00 AOL ART file
asf 30 26 B2 75 8E 66 CF 11 Windows Media
asf; wma; wmv 30 26 B2 75 8E 66 CF 11 A6 D9 00 AA 00 62 CE 6C Microsoft Windows Media Audio/Video File(Advanced   Streaming Format)
asx 3C Advanced Stream redirector file
au 2E 73 6E 64 SoundMachine Audio File NeXT/Sun Microsystems μ-Law audio file
avi 41 56 49 20 Audio Video Interleave (AVI)
AX 4D 5A Library cache file
AX 4D 5A 90 00 03 00 00 00 DirectShow filter
bag 41 4F 4C 20 46 65 65 64 62 61 67 AOL and AIM buddy list file
BAS 20 20 20
bin 42 4C 49 32 32 33 51 Thomson Speedtouch series WLAN router firmware
bmp 42 4D Windows Bitmap
BMP 42 4D 3E
bz; bz2 42 5A 68 BZIP Archive
BZ2; TAR.BZ2; TBZ2; TB2 42 5A 68 bzip2 compressed archive
CAB 49 53 63
CAB; HDR 49 53 63 28 Install Shield v5.x or 6.x compressed file
CAB 4D 53 43 46 Microsoft CAB File Format
cat 30 Microsoft security catalog file
CBD 43 42 46 49 4C 45 WordPerfect dictionary file (unconfirmed)
CCD 5B 43 6C
cdr CDR Corel Draw
CDR 45 4C 49 54 45 20 43 6F 6D 6D 61 6E 64 65 72 20 Elite Plus Commander saved game file
CDR, DVF 4D 53 5F 56 4F 49 43 45 Sony Compressed Voice File
CHI; CHM 49 54 53 46 Microsoft Compiled HTML Help File
CHM 49 54 53
CLB 43 4D 58 31 Corel Binary metafile
CLB 43 4F 4D 2B COM+ Catalog file
cnt 3A 42 61 73 65
COM, DLL, DRV, EXE, PIF, QTS, QTX, SYS 4D 5A Windows/DOS executable file
COM 4D 5A EE
COM E9 3B 03
CPE 46 41 58 43 4F 56 45 52 2D 56 45 52 Microsoft Fax Cover Sheet
CPL 4D 5A Control panel application
CPT 43 50 54 37 46 49 4C 45 Corel Photopaint file
CPT 43 50 54 46 49 4C 45 Corel Photopaint file
CPX 5B 57 69
cru; crush 43 52 55 53 48 CRUSH Archive File
CRU 43 52 55 53 48 20 76 Crush compressed archive
CRW 49 49 1A 00 00 00 48 45 41 50 43 43 44 52 02 00 Canon digital camera RAW file
CTF 43 61 74 61 6C 6F 67 20 33 2E 30 30 00 WhereIsIt Catalog file
CUR 00 00 02 00 01 00 20 20 Windows cursor file
dat 03 MapInfo Native Data Format
dat 1A 52 54 53 20 43 4F 4D 50 52 45 53 53 45 44 20 49   4D 41 47 45 20 56 31 2E 30 1A Runtime Software disk image
dat 41 56 47 36 5F 49 6E 74 65 67 72 69 74 79 5F 44 61   74 61 62 61 73 65 AVG6 Integrity database file
DAT 43 52 45 47 Windows 9x registry hive
DAT 43 6C 69 65 6E 74 20 55 72 6C 43 61 63 68 65 20 4D   4D 46 20 56 65 72 20 IE History DAT file
DAT 45 52 46 53 53 41 56 45 44 41 54 41 46 49 4C 45 Kroll EasyRecovery Saved Recovery State file
DAT 49 6E 6E 6F 20 53 65 74 75 70 20 55 6E 69 6E 73 74   61 6C 6C 20 4C 6F 67 20 28 62 29 Inno Setup Uninstall Log file
db 00 06 15 61 00 00 00 02 00 00 04 D2 00 00 10 00 Netscape Navigator (v4) database file
DB 44 42 46 48 Palm Zire photo database
db 08 dBASE IV or dBFast configuration file
db3 03 dBASE III file
db4 04 dBASE IV data file
dba 00 01 42 44 Palm DateBook Archive file
dbx CF AD 12 FE
dbx CF AD 12 FE C5 FD 74 6F Outlook Express
dci 3C 21 64 6F 63 74 79 70 AOL HTML mail file
dcx 3A DE 68 B1 DCX Graphic File
DDB 00 01 00
dib 42 4D device-independent bitmap image
DLL 4D 5A 90
DMP 4D 44 4D 50 93 A7 Windows minidump file
DMS 44 4D 53 21 Amiga DiskMasher compressed archive
doc 0D 44 4F 43 DeskMate Document file
doc 12 34 56 78 90 FF MS Word 6.0
doc 31 BE 00 00 00 AB 00 00 MS Word for DOS 6.0
doc 7F FE 34 0A MS Word
dot; ppt; xla; ppa; pps; pot; msi; sdw; db D0 CF 11 E0 MS Office/OLE2
doc; dot; xls; xlt; xla; ppt; apr ;ppa; pps; pot; msi;   sdw; db D0 CF 11 E0 A1 B1 1A E1 MS Compound Document v1 or Lotus Approach APR file
DPL 4D 5A 50
DRV 4D 5A 16
drw 07 A common signature and file extension for many   drawing programs.
drw 01 FF 02 04 03 02 Micrografx vector graphic file
ds4 4D 47 58 20 69 74 70 64 Micrografix Designer 4
DSN 4D 56 CD Stomper Pro label file
dsp 23 20 4D 69 63 72 6F 73 6F 66 74 20 44 65 76 6 56C   6F 70 65 72 20 53 74 75 64 69 6F Microsoft Developer Studio project file
dss 02 64 73 73 Digital Speech Standard (Olympus, Grundig, &   Phillips)
dtd 07 64 74 32 64 64 74 64 DesignTools 2D Design file
dtd 3C 21 45 4E 54 49 54 59 XML DTD
DVR 44 56 44 DVR-Studio stream file
dwg 41 43 31
Enn (where nn are numbers) 45 56 46 EnCase evidence file
ECO 2A 50 52
elf 7F 45 4C 46 01 01 01 00 ELF Executable
emf 01 00 00 00 58 00 00 00 Extended (Enhanced) Windows Metafile Format, printer   spool file
eml 44 65 6C 69 76 65 72 79 2D 64 61 74 65 3A Email
EML 46 72 6F 6D 20 20 20 A commmon file extension for e-mail files.   Signatures shown here are for Netscape, Eudora, and a generic signature,   respectively. EML is also used by Outlook Express and QuickMail.
EML 46 72 6F 6D 20 3F 3F 3F A commmon file extension for e-mail files.   Signatures shown here are for Netscape, Eudora, and a generic signature,   respectively. EML is also used by Outlook Express and QuickMail.
EML 46 72 6F 6D 3A 20 A commmon file extension for e-mail files.   Signatures shown here are for Netscape, Eudora, and a generic signature,   respectively. EML is also used by Outlook Express and QuickMail.
EML 52 65 63
enc 00 5C 41 B1 FF Mujahideen Secrets 2 encrypted file
enl [32 byte offset] 40 40 40 20 00 00 40 40 40 40 EndNote Library File
eps 25 21 50 53 Adobe EPS File
eps 25 21 50 53 2D 41 64 6F 62 65 Postscript
eps 25 21 50 53 2D 41 64 6F 62 65 2D 33 2E 30 20 45 50   53 46 2D 33 20 30 Adobe encapsulated PostScript file (If this   signature is not at the immediate beginning of the file, it will occur early in   the file, commonly at byte offset 30)
EPS C5 D0 D3
eth 1A 35 01 00 GN Nettest WinPharoah capture file
evt 30 00 00 00 4C 66 4C 65 Windows Event Viewer file
evt 03 00 00 00 C4 66 C4 56
EVTX 45 6C 66 46 69 6C 65 00 Windows Vista event log file
exe; dll; drv; vxd; sys; ocx; vbx 4D 5A Win32 Executable
exe; dll; drv; vxd; sys; ocx; vbx 4D 5A Win16 Executable
exe; com; 386; ax; acm; sys; dll; drv; flt; fon; ocx;   scr; lrc; vxd; cpl; x32 4D 5A Executable File
EXE, DLL, OCX, OLB, IMM, IME 4D 5A 90
fli 00 11 AF FLIC Animation file
flt 00 01 01 OpenFlight 3D file
FLT 4D 5A 90 00 03 00 00 00 Audition graphic filter file (Adobe)
FLV 46 4C 56 01 Flash video file
fm 3C 4D 61 6B 65 72 46 69 6C 65 20 Adobe FrameMaker file
fm3 00 00 1A 00 07 80 01 00 Lotus 123 v3 FMT file
fmt 20 00 68 00 20 0 Lotus 123 v4 FMT file
FNT 43 48 41
FON 4D 5A Font file
GBC 87 F5 3E
gid 3F 5F 03 00 Windows Help Index File
GID 4C 4E 02 00 Windows Help index file
GIF 47 49 46 38
gif 47 49 46 38 37 61 Graphics interchange format file (GIF 87A)
gif 47 49 46 38 39 61 Graphics interchange format file (GIF89A)
GTD 7B 50 72
GX2 47 58 32 Show Partner graphics file (not confirmed)
gz; tar; tgz 1F 8B Gzip Archive File
gz; tgz 1F 8B 08 GZ Compressed File
hap 91 33 48 46 HAP Archive File
HDMP 4D 44 4D 50 93 A7 Windows heap dump file
hdr 23 3F 52 41 44 49 41 4E 43 45 0A adiance High Dynamic Range image file
HLP 3F 5F 03
hlp 3F 5F 03 00 Windows Help file
HLP 4C 4E 02 00 Windows Help file
hlp [7 byte offset] 00 00 FF FF FF FF Windows Help file
hqx 28 54 68 69 73 20 66 69 6C 65 Macintosh BinHex 4 Compressed Archive
hqx 28 54 68 69 73 20 66 69 6C 65 20 6D 75 73 74 20 62   65 20 63 6F 6E 76 65 72 74 65 64 20 77 69 74 68 20 42 69 6E 48 65 78 20 Macintosh BinHex 4 Compressed Archive
HTM 3C 21 44
htm; html 3C 21 44 4F 43 54 HyperText Markup Language 3
htm; html 3C 48 54 4D 4C 3E HyperText Markup Language 2
htm; html 3C 68 74 6D 6C 3E HyperText Markup Language 1
html 68 74 6D 6C 3E HTML
ico 00 00 01 00 00 Icon File
ico 00 00 01 00 01 00 20 20 Icon File
IFF 46 4F 52 4D
IFO 44 56 44 DVD info file
IME 4D 5A 90
img 00 01 00 08 00 01 00 01 01 Ventura Publisher/GEM VDI Image Format Bitmap file
IMG 00 FF FF
IMM 4D 5A 90
ind 41 4F 4C 49 44 58 AOL client preferences/settings file (MAIN.IND)
ISO 43 44 30 30 31 ISO-9660 CD Disc Image (This signature usually   occurs at byte 8001, 8801, or 9001.)
ivr 2E 52 45 43 RealPlayer video file (V11 and later)
JAR 4A 41 52 43 53 00 JARCS compressed archive
jar 5F 27 A8 89 JAR Archive File
jpg; jpeg FF D8 FF
jpg; jpe; jpeg FF D8 FF E0 00 JPG Graphic File
jpg; jpe; jpeg FF D8 FF FE 00 JPG Graphic File
KGB 4B 47 42 5F 61 72 63 68 20 2D KGB archive
KOZ 49 44 33 03 00 00 00 Sprint Music Store audio file (for mobile devices)
LDB 42 49 4C
lha 2D 6C 68 35 2D LHA Compressed
lha; lzh [2 byte offset] 2D 6C 68 Compressed archive file
LHP 3F 5F 03
lhp 3F 5F 03 00 Windows Help File
lib 21 3C 61 72 63 68 3E 0A Unix archiver (ar) files and Microsoft Program   Library Common Object File Format (COFF)
LIB 2A 24 20
LIT 49 54 4F 4C 49 54 4C 53 Microsoft Reader eBook file
LNK 4C 00 00
lnk 4C 00 00 00 Windows Shortcut (Link File)
lnk 4C 00 00 00 01 14 02 Windows Link File
LNK 4C 00 00 00 01 14 02 00 Windows shortcut file
log 2A 2A 2A 20 20 49 6E 73 74 61 6C 6C 61 74 69 6F 6E   20 53 74 61 72 74 65 64 20 Symantec Wise Installer log file
lzh lh Lz compression file
lwp 57 6F 72 64 50 72 6F Lotus WordPro v9
m3u 23 45 58
m4a 00 00 00 20 66 74 79 70 4D 34 41 20 00 00 00 00 Apple Lossless Audio Codec file
m4a; m4v 00 00 00 20 66 74 79 70 4D 34 41 20 00 00 00 00 QuickTime M4A/M4V file
manifest 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D Windows Visual Stylesheet XML file
MAR 4D 41 52 31 00 Mozilla archive
MAR 4D 41 52 43 Microsoft/MSN MARC archive
MAR 4D 41 72 30 00 MAr compressed archive
max D0 CF 11
mdb 00 01 00 00 53 74 61 6E 64 61 72 64 20 4A 65 74 20   44 42 Microsoft Access file
mdb; mda; mde; mdt 53 74 61 6E 64 61 72 64 20 4A MS Access
MDF 00 FF FF
mdf 00 FF FF FF FF FF FF FF FF FF FF 00 00 02 00 01 Alcohol 120% CD image
mdf 01 0F 00 00 Microsoft SQL Server 2000 database
MDI 45 50 Microsoft Document Imaging file
MDS 4D 45 44
MID; MIDI 4D 54 68 64 Musical Instrument Digital Interface (MIDI) sound   file
mkv 1A 45 DF A3 93 42 82 88 6D 61 74 72 6F 73 6B 61 Matroska stream file
MLS 4D 49 4C 45 53 Milestones v1.0 project management and scheduling   software (Also see “MV2C” and “MV214” signatures)
MLS 4D 4C 53 57 Skype localization data file
MLS 4D 56 32 31 34 Milestones v2.1b project management and scheduling   software (Also see “MILES” and “MV2C” signatures)
MLS 4D 56 32 43 Milestones v2.1a project management and scheduling   software (Also see “MILES” and “MV214” signatures)
MMF 4D 4D 4D 44 00 00 Yamaha Corp. Synthetic music Mobile Application   Format (SMAF) for multimedia files that can be played on hand-held devices.
mny 00 01 00 00 4D 53 49 53 41 4D 20 44 61 74 61 62 61   73 65 Microsoft Money file
MOV 00 00 0F
MOV 00 00 77
mov 6D 6F 6F 76 Quicktime
mov 6D 64 61 74 QuickTime Movie
mp 0C ED Monochrome Picture TIFF bitmap file (unconfirmed)
MP3 49 44 33 MPEG-1 Audio Layer 3 (MP3) audio file
MP3 FF FB 50
mp4 00 00 00 18 66 74 79 70 33 67 70 35 MPEG-4 video files
MPA 00 00 01
mpg; mpeg 00 00 01 B3 MPEG Movie
mpg 00 00 01 BA MPEG
MSC 3C 3F 78
msc 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E   30 22 3F 3E 0D 0A 3C 4D 4D 43 5F 43 6F 6E 73 6F 6C 65 46 69 6C 65 20 43 6F 6E   73 6F 6C 65 56 65 72 73 69 6F 6E 3D 22 Microsoft Management Console Snap-in Control file
msi 23 20 Cerius2 file
MSV 4D 53 5F 56 4F 49 43 45 Sony Memory Stick Compressed Voice file
NES 4E 45 53
NLS C2 20 20
nri 0E 4E 65 72 6F 49 53 4F Nero CD Compilation
ntf 1A 00 00 Lotus Notes database template
nsf; ntf 1A 00 00 03 00 00 Lotus Notes Database/Template
nsf 1A 00 00 03 00 00 11 00 Notes Database
nsf 1A 00 00 04 00 00 Lotus Notes database
ntf 30 31 4F 52 44 4E 41 4E 43 45 20 53 55 52 56 45 59   20 20 20 20 20 20 20 National Transfer Format Map File
obj 4C 01 Microsoft Common Object File Format (COFF)   relocatable object code file for an Intel 386 or later/compatible processors
OCX 4D 5A ActiveX or OLE Custom Control
OCX 4D 5A 90
OLB 4D 5A OLE object library
OLB 4D 5A 90
org; pfc 41 4F 4C 56 4D 31 30 30 AOL personal file cabinet (PFC) file
pak 1A 0B Compressed archive file
PAT 47 46 31 50 41 54 43 48 Advanced Gravis Ultrasound patch file
PAT 47 50 41 54 GIMP (GNU Image Manipulation Program) pattern file
PBK 5B 41 44
PCB 17 A1 50
PCS 0A 05 01
pcx 0A nn 01 01 ZSOFT Paintbrush file(where nn = 0x02, 0x03, or   0x05)
pcx 0A 05 01 08 PC Paintbrush(often associated with Quake Engine   games)
pdb [11 byte offset] 00 00 00 00 00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00 00 00 00 00 Palmpilot Database/Document File
PDF 25 50 44
pdf; fdf 25 50 44 46 Adobe Portable Document Format and Forms Document   file
pdf 25 50 44 46 2D 31 2E Adobe Acrobat
PDG 48 48 02
pf 11 00 00 00 53 43 43 41 Windows prefetch file
pic 01 00 00 00 01 Unknown type picture file
PJT 00 00 07
PLL 24 53 6F
PNG 89 50 4E
PNG 89 50 4E 47
png 89 50 4E 47 0D 0A PNG Image File
png 89 50 4E 47 0D 0A 1A 0A PNG Image File
PPC 52 65 63
PPT D0 CF 11
ppt [512 byte offset] 00 6E 1E F0 PowerPoint presentation subheader (MS Office)
ppt [512 byte offset] 0F 00 E8 03 PowerPoint presentation subheader (MS Office)
PPZ 4D 53 43 46 Powerpoint Packaged Presentation
prc 42 4F 4F 4B 4D 4F 42 49 Palmpilot resource file
PRG 23 44 45
ps 25 21 50 53 2D 41 64 6F 62 65 Postscript
PSD 38 42 50
psd 38 42 50 53 Adobe Photoshop image file
psp 7E 42 4B 00 PaintShop Pro Image File
pst 21 42 44 4E Microsoft Outlook Personal Folder file
pwl E3 82 85 96 Windows Password
qbb 45 86 00 00 06 00 Intuit QuickBooks Backup file
qdf AC 9E BD 8F Quicken
qph 03 00 00 00 Quicken price history file
qt 6D 64 61 74 Quicktime Movie File
qxd 00 00 4D 4D 58 50 52
ra 2E 52 4D 46 00 00 00 12 00 Real Audio file
ra; ram 2E 72 61 FD Real Audio File
ra 2E 72 61 FD 00 RealAudio streaming media file
RAR 52 61 72
rar 52 61 72 21 RAR Archive File
RAW 06 05 00
reg 52 45 47 45 44 49 54 34
rgb 01 DA 01 01 00 03 Silicon Graphics RGB Bitmap
RM 2E 52 4D
rm; rmvb 2E 52 4D 46 Real Media streaming media file
rpm ED AB EE DB RPM Archive File
RTD 43 23 2B 44 A4 43 4D A5 48 64 72 RagTime document file
RTF 7B 5C 72
rtf 7B 5C 72 74 66 Rich Text Format File
sav 24 46 4C 32 40 28 23 29 20 53 50 53 53 20 44 41 54   41 20 46 49 4C 45 SPSS Data file
SBV 46 45 44 46 (Unknown file type)
SCH 2A 76 65
scm 80 53 43
SH3 48 48 47 42 31 Harvard Graphics presentation file
SHD 4B 49 00 00 Windows 9x printer spool file
sit 53 49 54 21 Stuffit v1 Archive File
sit 53 74 75 66 66 49 74 Stuffit v5 Archive File
sle 3A 56 45 52 53 49 4F 4E Surfplan kite project file
sle 41 43 76 teganos Security Suite virtual secure drive
sly; srt; slt 53 52 01 00 Sage sly.or.srt.or.slt
SMD 00 FF FF
snm 00 1E 84 90 00 00 00 00 Netscape Communicator (v4) mail folder
SNP 4D 53 43 46 Microsoft Access Snapshot Viewer file
sol 00 BF Adobe Flash shared object file (e.g., Flash cookies)
spl 00 00 01 00 Windows NT/2000/XP printer spool file
SCR 4D 5A Screen saver
SUB FF FF FF
SWF 43 57 53 Shockwave Flash file (v5+)
SWF 46 57 53 Macromedia Shockwave Flash player file
sxc calc OpenOffice Calc
sxd draw OpenOffice Draw
sxi impress OpenOffice Impress
sxm math OpenOffice Math
sxw writer OpenOffice Writer
syw 41 4D 59 4F Harvard Graphics symbol graphic
TAG 00 00 02
tar; cpio 30 37 30 37 30 37 CPIO Archive File
tar.z 1F 9D 90 Compressed tape archive file
tga 00 00 10 00 00 RLE压缩的前5字节
TGA 00 00 02
tga 00 00 02 00 00 未压缩的前5字节
TIF; TIFF 49 20 49 Tagged Image File Format file
tif; tiff 49 49 2A TIFF (Intel)
tif; tiff 49 49 2A 00 Tagged Image File Format file (little endian, i.e.,   LSB first in the byte; Intel)
TIF; TIFF 4D 4D 00 2A Tagged Image File Format file (big endian, i.e., LSB   last in the byte; Motorola)
tif; tiff 4D 4D 2A TIFF (Motorola)
TIF; TIFF 4D 4D 00 2B BigTIFF files; Tagged Image File Format files >4   GB
TLB 4D 53 46 54 02 00 01 00 OLE, SPSS, or Visual C++ type library file
tr1 01 10 Novell LANalyzer capture file
TST 00 01 00
TTF 00 01 00
ufa 55 46 41 UFA Archive File
VBX 4D 5A VisualBASIC application
VCD 45 4E 54 52 59 56 43 44 02 00 00 01 02 00 18 58 VideoVCD (GNU VCDImager) file
vcf 42 45 47 49 4E 3A 56 43 41 52 44 0D 0A vCard file
vob 00 00 01 BA DVD Video Movie File (video/dvd, video/mpeg)
VXD, 386 4D 5A Windows virtual device drivers
WAV 52 49 46
wav 57 41 56 45 Wave
wav 57 41 56 45 66 6D 74 Wave Files
wb2 00 00 02 00 QuattroPro for Windows Spreadsheet file
wb3 [24 byte offset] 3E 00 03 00 FE FF 09 00 06 Quatro Pro for Windows 7.0 Notebook file
wk1; wks 20 00 60 40 60 Lotus 123 v1 Worksheet
wk1 00 00 02 00 06 04 06 00 08 00 00 00 00 00 Lotus 1-2-3 spreadsheet (v1) file
wk3 00 00 1A 00 00 10 04 00 Lotus 123 spreadsheet (v3) file
wk4; wk5 00 00 1A 00 02 10 04 00 Lotus 1-2-3 spreadsheet (v4, v5) file
wks 0E 57 4B 53 DeskMate Worksheet
WMA 30 26 B2
wmf 01 00 09 00 Graphics Metafile
wmf 01 00 09 00 00 03 Windows Metadata file (Win 3.x format)
wmf 02 00 09 00 Graphics Metafile
wmf D7 CD C6 9A Windows Meta File
WMV 30 26 B2
wp FF 57 50 43 WordPerfect v5 or v6
wpd FF 57 50 43 WordPerfect
wpg FF 57 50 47 WordPerfect Graphics
wri 31 BE Microsoft Write file
WRI 31 BE 00
wri 32 BE Microsoft Write file
ws 1D 7D WordStar Version 5.0/6.0 document
XBE 58 42 45
xdr 3C BizTalk XML-Data Reduced Schema file
xls 09 02 06 00 00 00 10 00 B9 04 5C 00 MS Excel v2
xls 09 04 06 00 00 00 10 00 F6 05 5C 00 MS Excel v4
XLS D0 CF 11
xls D0 CF 11 E0 MS Excel
xls [512 byte offset]  09 08 10 00 00 06 05 00 Excel spreadsheet subheader (MS Office)
XML 3C 3F 78
xml 3C 3F 78 6D 6C XML Document
xml FF FE 3C 00 52 00 4F 00 4F 00 54 00 53 00 54 00 55 00   42 00 XML Document (ROOTSTUB)
XMV 00 50 01
XSL FF FE 3C
xul 72 73 69 6F 6E 3D 22 31 3C 3F 78 6D 6C 20 76 65 2E   30 22 3F 3E XML User Interface Language file
z 1F 9D TAR Compressed Archive File
Z 1F 9D 8C
ZIP 50 4B 03
zip; jar; zipx 50 4B 03 04 ZIP Archive
zip 50 4B 30 30 ZIP Archive (outdated)
Zip 50 4B 30 30 50 4B 03 04 WINZIP Compressed
zoo 5A 4F 4F 20 ZOO Archive File

有了这个表格,我们就可以通过检查文件的十六进制头来判断文件实际的类型了。

方法

windows下

我们可以通过ultraedit之类的文本编辑器检查文件的十六进制表示,很方便。

Linux下

我们有hexdump命令来查看文件的十六进制形式,用这种方法查看png文件的结果大概是这样的:

1
2
3
4
5
6
0000000 5089 474e 0a0d 0a1a 0000 0d00 4849 5244
0000010 0000 dc00 0000 3200 0608 0000 3700 b514
0000020 0073 0000 7009 5948 0073 0b00 0013 0b00
0000030 0113 9a00 189c 0000 4d0a 4369 5043 6850
0000040 746f 736f 6f68 2070 4349 2043 7270 666f
.......

然而,这个只能查看不能修改。而且实际上,这里显示的字节的顺序是跟机器有关的,在常见的x86和一般的OS(如windows,FreeBSD,Linux)使用的是小端模式,因此他的高低位是反着的,实际上如果按照日常的阅读习惯,他应该表示为:

1
2
0000000 8950 4e47 0d0a 1a0a 0000 000d 4948 4452
.......

不过如果是在MacOS那样默认大端序的机器里用hexdump据说就不会反了。

这样才是标准的png文件的头。

当然,我们有更方便强大的vim,用这个可以查看和修改文件的十六进制表示形式。我们用vim打开刚才的png文件,会的到以下的ascii表示形式:

1
2
3
4
5
6
7
8
<89>PNG^M                                                                   
^Z
^@^@^@^MIHDR^@^@^@Ü^@^@^@2^H^F^@^@^@7^Tµs^@^@^@ pHYs^@^@^K^S^@^@^K^S^A^@<9a> <9c>^X^@^@
MiCCPPhotoshop ICC profile^@^@xÚ<9d>SwX<93>÷^V>ß÷e^OVBØð±<97>l<81>^@"#¬^HÈ^P Y¢^P<92>^@a<84>^P^R@Å<85><88>
V^T^U^Q<9c>HUÄ<82>Õ
H<9d><88>â (¸gA<8a><88>Z<8b>U\8î^_ܧµ}zïííû×û¼ç<9c>çüÎyÏ^O<80>^Q^R&<91>æ¢j^@ 9R<85><:Ø^_<8f>OHÄɽ<80>^B^UHà^D ^PæËÂg^EÅ^@^@ð^Cyx~t°?ü^A¯o^@^B^@pÕ.$^RÇáÿ< 83>ºP&W^@ <91>^@à"^Rç^K^A<90>R^@È.TÈ^T^@È^X^@°S³d
^@<94>^@^@ly|B"^@ª^M^@ìôI>^E^@Ø©<93>Ü^W^@Ø¢^\©^H^@<8d>^A^@<99>(G$^B@»^@`U<81 >R,^BÀÂ^@ ¬@".^DÀ®^A<80>Y¶2G^B<80>½^E^@v<8e>X<90>^O@`^@<80><99>B,Ì^@ 8^B^@C^ ^^SÍ^C L^C 0Ò¿à©_p<85>¸H^A^@ÀË<95>Í<97>KÒ3^T¸<95>Ð^Zwòðàâ!âÂl±Ba^W)^Pf ä"<9
..........

接下来我们需要在命令模式下输入  :%!xxd   这样会得到以下的样子:

1
2
3
4
5
6
7
8
9
0000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452  .PNG........IHDR   
0000010: 0000 00dc 0000 0032 0806 0000 0037 14b5 .......2.....7..
0000020: 7300 0000 0970 4859 7300 000b 1300 000b s....pHYs.......
0000030: 1301 009a 9c18 0000 0a4d 6943 4350 5068 .........MiCCPPh
0000040: 6f74 6f73 686f 7020 4943 4320 7072 6f66 otoshop ICC prof
0000050: 696c 6500 0078 da9d 5377 5893 f716 3edf ile..x..SwX...>.
0000060: f765 0f56 42d8 f0b1 976c 8100 2223 ac08 .e.VB....l.."#..
0000070: c810 59a2 1092 0061 8410 1240 c585 880a ..Y....a...@....
.......

这就对了,而且高低位的位置与一般的表示一致。

当我们需要调回ascii模式的时候,我们只要输入 :%!xxd -r 就可以回到之前的模式了。

这里要注意的是,在十六进制里我们可以通过编辑十六进制码来修改值,但是不要修改后面对应的ascii码和前面的地址,而且模式转换过去后一定要记得转换回来。

MD5算法应用

Posted on 2015-11-30 | In Cryptography

MD5(Message-Digest Algorithm 5)算法是一种非常常见的信息摘要hash算法,一般可以用来进行数字签名,或者理解成为一种压缩算法。他的本质是一种分组加密算法。百度上对MD5算法简要的叙述为:MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值。用十六进制表示的话,每四位变成一个十六进制数,这样也就是生成了总共为32位的十六进制数,即MD5码。

这里不介绍MD5的加密和解密算法的细节。从应用的角度讲,我们完全暂且不需要理解算法的过程。我们可以直接利用现有的工具进行加密——-还有解密!事实上,MD5算法从他1991年由MIT发明以来,一直都在经受着解密高手们的攻击,这样,终于在2004年,由我们中国山东大学的王小云教授成功研究出了高效的寻找碰撞的算法(本质上就是一种高效的暴力破解,而且目前这种破解算法已经随处可见了)。

作为应用而言,比如解决IDF实验室的第一题,我们只需要一个能够为我们调用的加密解密的接口来方便我们编程。找了半天我终于找到了一个比较不错的免费的API:http://tool.zzblo.com/,只需要简单的http请求就可以调用。

加密请求

1
2
3
4
5
6
7
8
9
10
11
import urllib,urllib2,cookielib
path='http://tool.zzblo.com/Api/Md5/encrypt'
plaintext='12345678'
cj=cookielib.CookieJar()
post_data=urllib.urlencode({'text':plaintext})
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders=[('User-agent','Opera/9.23')]
urllib2.install_opener(opener)
req=urllib2.Request(path,post_data)
response=urllib2.urlopen(req)
print response.read()

respose:

1
{"status":200,"text":"12345678","secret32":"25d55ad283aa400af464c76d713c07ad","secret16":"83aa400af464c76d"}

这里就看到了明文密文的对应关系。

解密请求

1
2
3
4
5
6
7
8
9
10
11
import urllib,urllib2,cookielib
path='http://tool.zzblo.com/Api/Md5/decrypt'
cyphertext='25f9e794323b453885f5181f1b624d0b'
cj=cookielib.CookieJar()
post_data=urllib.urlencode({'secret':cypthertext})
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders=[('User-agent','Opera/9.23')]
urllib2.install_opener(opener)
req=urllib2.Request(path,post_data)
response=urllib2.urlopen(req)
print response.read()

response:

1
{"status":200,"text":"123456789","secret32":"25f9e794323b453885f5181f1b624d0b","secret16":"323b453885f5181f"}

与上述结果一致。

当然这个API只能解决简单的MD5,对于一些复杂的还是要通过别的办法解决。

短信轰炸机原理分析及简要实现

Posted on 2015-11-27 | In Others

很多情况下,我们会遇到一些讨厌的人,特别是那种电信诈骗的骗子,行骗方法歹毒低劣,十分令人恶心。然而我们又不能拿他们怎么办。这时候我们就会想起用电话轰炸机这个东西来打击报复。网络上也有过类似的比如“短信轰炸机”这种东西,我也用他来黑过一些人。据他们后来说他们收到的都是些注册会员啊,登陆啊之类的短信提示,十分的烦人。当时听上去就觉得这个东西真的很厉害。最近一个偶然的机会,我发现其实短信轰炸机这种东西原理也是十分的简单的,只要拥有一些基础的网页登陆的知识,理论上就可以实现写出自己的“短信轰炸机”。

原理

在我们注册一家网站的会员的时候,很多情况下我们需要输入我们的手机号来进行识别和绑定。通常的步骤是:(1)我们输入手机号;(2)系统会向我们的手机发送一条验证码;(3)我们将手机上收到的验证码输入到注册页面上来完成识别。明眼人一看大概就能看出其中的端倪。没错,我们就可以利用网站发送的验证短信,来作为我们的”轰炸引擎“。

基本步骤

步骤就是模拟登陆的步骤了,首先我们手动实现一次验证,并且用抓包工具截获他的http封包,获得post的数据,然后利用现有的编程工具,模拟请求即可。

python简单实现

我模拟的是搜狐的注册申请,仅实现一次发送。

1
2
3
4
5
6
7
8
9
10
11
import urllib,urllib2,cookielib
path='https://passport.sohu.com/signup/send_mobile_captcha'
number="18282828282"
cj=cookielib.CookieJar()
post_data=urllib.urlencode({'mobile':number,'captchaType':'1'})
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders=[('User-agent','Opera/9.23')]
urllib2.install_opener(opener)
req=urllib2.Request(path,post_data)
response=urllib2.urlopen(req)
print response.read()

其中的post的数据是根据抓包抓来的数据来填写的,其他的就是简单的模拟登陆的东西。

note

  1. 实际应用中,需要使用多线程的技术申请多个网站来保证轰炸的密度。
  2. 申请的频率不能太快,因为服务器端会有记录,申请太快会被拒绝服务的。
  3. 通常会使用proxy代理http服务来隐藏自身信息。
  4. 为了防止被利用,绝大多数的网站都会在申请时设置验证码环节来抵御我们这种恶意的申请。所以通常还需要结合OCR技术来识别验证码。

正则表达式基础(2)

Posted on 2015-11-25 | In Others

上一节记录了主要的一些元字符集,算是刚刚入了门。这一节主要介绍一些稍微需要动脑筋的东西。

分组捕获与后向引用

分组实际上就是个对括号,用处就是将一个匹配串当成一个整体来看,用于改变匹配的优先级。比如我们要匹配“abcabcabcabc”,就可以这样匹配:(abc){4} 。

在分组的基础上,我们就可以进行后向引用。所谓后向引用,就是将之前匹配到的字符串记录下来,供后来继续用,提高表达式的效率。

具体用法是,系统会给表达式中所有的分组标上序号,从1开始。接下来每当希望用到之前分组的内容时,只需要调用”i” 其中i就是分组对应的序号。

比如(abc)1$12$13$14 就可以匹配“abc1abc2abc3abc4”了。

注:目测这样写的话,组数不能超过9,毕竟“”这个东西只能作用于一个字符。

零宽断言

这个是用来匹配一个位置而不是一个字符串的比如 ( $ 和 ^ ) ,所以叫零宽。他规定了当前的位置需要满足某些条件,或者不能够满足某些条件。也是非常有用处的一个东西。

1、(?=exp)        零宽度正预测先行断言

它断言自身出现的位置的后面能匹配表达式exp。比如bw+(?=ingb),匹配以ing结尾的单词的前面部分(除了ing以外的部分),如查找I’m singing while you’re dancing.时,它会匹配sing和danc。

2、(?<=exp)     零宽度正回顾后发断言

它断言自身出现的位置的前面能匹配表达式exp。比如(?<=bre)w+b 会匹配以re开头的单词的后半部分(除了re以外的部分),例如在查找reading a book时,它匹配ading。

3、(?!exp)     零宽度负预测先行断言

它断言此位置的后面不能匹配表达式exp。例如:d{3}(?!d) 匹配三位数字,而且这三位数字的后面不能是数字;b((?!abc)w)+b 匹配不包含连续字符串abc的单词。
4、(?<!exp)    零宽度负回顾后发断言

它断言此位置的前面不能匹配表达式exp:(?<![a-z])d{7} 匹配前面不是小写字母的七位数字。

注释

由于正则一向比较精炼,所以注释还是很有必要的。

我们可以用语法(?#comment)来包含注释。例如:2[0-4]d(?#200-249)|25[0-5](?#250-255)|[01]?dd?(?#0-199) 。

贪婪与懒惰

当正则表达式中包含能接受重复的限定符时,通常的行为是在满足匹配关系的情况下匹配尽可能多的字符。这被称为贪婪匹配,也是默认的匹配方式。

然而有时,我们更需要懒惰匹配,也就是匹配尽可能少的字符。前面给出的限定符都可以被转化为懒惰匹配模式,只要在它后面加上一个问号’?’。这样.*?就意味着匹配任意数量的重复,但是在能使整个匹配成功的前提下使用最少的重复。现在看看懒惰版的例子吧:

a.*?b匹配最短的,以a开始,以b结束的字符串。如果把它应用于aabab的话,它会匹配aab(第一到第三个字符)和ab(第四到第五个字符)。

懒惰限定符
代码/语法 说明
*? 重复任意次,但尽可能少重复
+? 重复1次或更多次,但尽可能少重复
?? 重复0次或1次,但尽可能少重复
{n,m}? 重复n到m次,但尽可能少重复
{n,}? 重复n次以上,但尽可能少重复

当然正则表达式在不同的平台上还有其他不同的功能,比如会有一些选项来优化匹配方式,或者是平衡组、递归匹配这些复杂的东西。这些遇到了再行了解。

正则表达式基础(1)

Posted on 2015-11-24 | In Others

正则表达式这东西真的特别常用,在搜索文本啊,写爬虫什么的都得用到。而且现如今各种语言,都会支持正则表达式。之前也零零碎碎的看过,但是很多细节都记不清,这里姑且把一些知识要点记录下来,供日后查找使用。

通用的正则表达式

由于正则表达式有很多种实现方式,所以各种方式之间有一些区别。但是以下的一些条件,则是在任何语言的正则中都适用的规律:

符号 匹配
^ 匹配输入字符串开始的位置。
$ 匹配输入字符串结尾的位置。
* 零次或多次匹配前面的字符或子表达式。例如,zo* 匹配“z”和“zoo”。* 等效于 {0,}。
+ 一次或多次匹配前面的字符或子表达式。例如,“zo+”与“zo”和“zoo”匹配,但与“z”不匹配。+ 等效于 {1,}。
? 零次或一次匹配前面的字符或子表达式。例如,“do(es)?”匹配“do”或“does”中的“do”。? 等效于 {0,1}。
{n} _n _是非负整数。正好匹配 n 次。例如,“o{2}”与“Bob”中的“o”不匹配,但与“food”中的两个“o”匹配。
{n,} _n _是非负整数。至少匹配 _n _次。例如,“o{2,}”不匹配“Bob”中的“o”,而匹配“foooood”中的所有 o。“o{1,}”等效于“o+”。“o{0,}”等效于“o*”。
{n,m} M 和 n 是非负整数,其中 n <= _m_。匹配至少 n 次,至多 m 次。例如,“o{1,3}”匹配“fooooood”中的头三个 o。’o{0,1}’ 等效于 ‘o?’。注意:您不能将空格插入逗号和数字之间。
? 当此字符紧随任何其他限定符(*、+、?、{n}、{n,}、{n,m})之后时,匹配模式是“非贪心的”。“非贪心的”模式匹配搜索到的、尽可能短的字符串,而默认的“贪心的”模式匹配搜索到的、尽可能长的字符串。例如,在字符串“oooo”中,“o+?”只匹配单个“o”,而“o+”匹配所有“o”。
. 匹配除“n”之外的任何单个字符。若要匹配包括“n”在内的任意字符,请使用诸如“[sS]”之类的模式。
x| y 匹配 x 或 _y_。例如,’z|food’ 匹配“z”或“food”。’(z| f)ood’ 匹配“zood”或“food”。
[xyz] 字符集。匹配包含的任一字符。例如,“[abc]”匹配“plain”中的“a”。
[^xyz] 反向字符集。匹配未包含的任何字符。例如,“[^abc]”匹配“plain”中的“p”。
[a-z] 字符范围。匹配指定范围内的任何字符。例如,“[a-z]”匹配“a”到“z”范围内的任何小写字母。
[^a-z] 反向范围字符。匹配不在指定的范围内的任何字符。例如,“[^a-z]”匹配任何不在“a”到“z”范围内的任何字符。

这里是正则最基础的用法了,熟记这张表,平时就够用了。

当然,我们平时也会看到其他一些符号,比如下面这些类似的符号。

其他元字符

符号 匹配
b 匹配一个字边界,即字与空格间的位置。例如,“erb”匹配“never”中的“er”,但不匹配“verb”中的“er”。
B 非字边界匹配。“erB”匹配“verb”中的“er”,但不匹配“never”中的“er”。
c_x_ 匹配 x 指示的控制字符。例如,cM 匹配 Control-M 或回车符。x 的值必须在 A-Z 或 a-z 之间。如果不是这样,则假定 c 就是“c”字符本身。
d 数字字符匹配。等效于 [0-9]。
D 非数字字符匹配。等效于 [^0-9]。
f 换页符匹配。等效于 x0c 和 cL。
n 换行符匹配。等效于 x0a 和 cJ。
r 匹配一个回车符。等效于 x0d 和 cM。
s 匹配任何空白字符,包括空格、制表符、换页符等。与 [ fnrtv] 等效。
S 匹配任何非空白字符。与 [^ fnrtv] 等效。
t 制表符匹配。与 x09 和 cI 等效。
v 垂直制表符匹配。与 x0b 和 cK 等效。
w 匹配任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。
W 与任何非单词字符匹配。与“[^A-Za-z0-9_]”等效。

这些元字符也非常普遍,我之所以将他跟上面的通用字符分开,是因为这种类似Perl风格的正则表达式并不能被所有的工具识别。比如Linux下的egrep就无法识别这种表示方法。这就引入了下面的POSIX字符类。

POSIX字符类

符号 匹配
[:alnum:] 字母字符和数字字符;在ASCII码中,与[A-Za-z0-9]等效
[:word:] 基本与[:alnum:]一样,只是多了一个下划线字符(_)
[:alpha:] 字母字符;在ASCII中,等效于[A-Za-z]
[:blank:] 包括空格和制表符
[:cntrl:] ASCII控制码;包括ASCII字符0~31以及127
[:digit:] 数字0~9
[:graph:] 可见字符;在ASCII中,包括字符33~126
[:lower:] 小写字母
[:punct:] 标点符号字符;在ASCII中,与[-!”#$%&’()*+,./:;<=>?@[]_`{|}~]等效
[:print:] 可打印字符;包括[:graph:]中的所有字符再加上空格字符
[:space:] 空白字符如空格符、制表符、回车符、换行符、垂直制表符以及换页符。在ASCII中,等效为[ trnvf]
[:upper:] 大写字母
[:xdigit:] 用于表示十六进制的字符;在ASCII中,与[0-9A-Fa-f]等效

在使用的时候记得还要在外面包一层[  ]。比如匹配四个字母,可以写成 [[:alpha:]]{4}

这里之所以提到POSIX字符类,是因为在shell中无法使用d w 这类的元字符,只能使用POSIX字符类。

以上就是正则最基础的用法了。掌握这些就可以解决绝大多数的问题了。

Beautiful Soup库的基本介绍

Posted on 2015-11-22 | In Python

beautiful soup库是python中用来解析html文件的一个工具,他能做到将html文件依据他的标签的特征来取出相应的标签块,比如取出网页的title啊,body啊,或者是某个id对应的东西啊,等等。从而为进一步的加工处理创造条件。从某种程度上讲是替代了正则的作用,但是比正则表达式使用的更加方便。

现在的beautiful soup库已经是第4.2版本了,所以我们通常叫他bs4。bs4作为一个库,其实是有很多的用法的。至于其具体用法,我是参考以下的文档的:

http://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

这里讲的比较详细,查阅起来也很方便。至于安装,我一般是用来pip进行的,这个比较方便快捷,也便于管理。

这里权且记录下一些基本的用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
from bs4 import BeautifulSoup #导入模块

soup = BeautifulSoup(html_doc,“lxml”) #生成对象soup(这里不加第二个参数有时候会报warning)

print soup.prettify() #prettify()方法将源码格式化并返回
# <html>
# <head>
# <title>
# The Dormouse's story
# </title>
# </head>
# <body>
# <p class="title">
# <b>
# The Dormouse's story
# </b>
# </p>
# <p class="story">
# Once upon a time there were three little sisters; and their names were
# <a class="sister" href="http://example.com/elsie" id="link1">
# Elsie
# </a>
# ,
# <a class="sister" href="http://example.com/lacie" id="link2">
# Lacie
# </a>
# and
# <a class="sister" href="http://example.com/tillie" id="link2">
# Tillie
# </a>
# ; and they lived at the bottom of a well.
# </p>
# <p class="story">
# ...
# </p>
# </body>
# </html>

#以下的方法都很浅显易懂
print soup.title
# <title>The Dormouse's story</title>

print soup.title.name
# u'title'

print soup.title.string
# u'The Dormouse's story'

print soup.title.parent.name
# u'head'

print soup.p
# <p class="title"><b>The Dormouse's story</b></p>

print soup.p['class']
# u'title'

print soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

print soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

print soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

for link in soup.find_all('a'):
print(link.get('href'))

# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie

print soup.get_text()
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
#
# ...

python模块下载工具pip和easy_install

Posted on 2015-11-22 | In Python

在写python的时候,经常会用到一些扩展包,作为python新手,经常又不知道去哪里找这些包。而且就算是找到了,下载下来之后还需要进行繁琐的安装、配置等操作。有时候为了进行这些安装还要去安装能够安装这些程序的程序,比如setuptools等。而安装这些东西有可能还会有很多奇奇怪怪的问题,这样严重影响了编程的体验。还好,python有几个类似ubuntu中apt-get一样的东西,相当于一个包管理器,能够十分便捷的帮我们安装到自己需要的模块,这就是pip和easy_install。

pip

安装

1
root@iZ28ntr2ej5Z:~# apt-get install python-pip

使用

比如想安装beautifulsoup包可以这样。

1
root@iZ28ntr2ej5Z:~# pip install beautifulsoup

这样基本就能将包安装好了。当然这里很多情况下是会报一些错的(比如包的名称不对),这些错通常都会输出到他的log文件中去,所有的问题都会在log中找到答案。

其实所有模块的安装方法都能在该模块的帮助文档中找到。比如beautifulsoup包也可以用apt-get来安装,但是作为一个更加专业的python软件,pip显然在python包的安装上更加有优势。而且,pip不仅可以下载,而且可以列举,更新和卸载包,使用的范围更加的广泛。

以下是一些常用参数:

更新

-U 或者 –upgrade

1
pip install -U SomePackage

可以用来更新其他模块,也可以更新pip自己。

查看更新

1
pip list --outdated

卸载

1
pip uninstall SomePackage

更新源

有时候经常会遇到他默认的下载点无法链接的情况,这就时候需要指定下载源了。

手动指定:

在pip后面跟-i 来指定源,比如用豆瓣的源来安装web.py框架:

pip install web.py -i http://pypi.douban.com/simple

自动指定:

修改~/.pip/pip.conf 文件的内容,类似与如下格式:

1
2
[global]
index-url = http://pypi.douban.com/simple/

最后再更新下pip就行了。

!!!最新发现,更可靠的源:http://pypi.zenlogic.net/simple/

easy_install

在实际中经常发现pip并不好用,因为牵涉到什么ssl的错误,经常会报一些奇奇怪怪的错。相比之下easy_install的表现就稳定许多了。

安装

1
root@iZ28ntr2ej5Z:~# apt-get install python-setuptools

没错,在setuptools里就有easy_install了。

使用

与pip的用法基本相同,只要把pip换成easy_install即可。包括指定源的方法,都一样。


2017年1月27日:

最近发现pip在使用自定义的源的时候需要添加–trusted-host参数才能从指定源下载,否则就会提示你需要安装的包找不到。其实这时候只要把在配置文件里写入的pypi源的url改成https即可。

Linux下的图片格式转换工具imagemagick

Posted on 2015-11-19 | In Linux

在玩Processing的时候,经常需要将大量的png文件作为帧,处理到gif图中。而他自带的插件似乎并没有用,所以就想在网上找个。网上当然有很多这种类型的工具啦,但是基本上都是像gimp这样的类Photoshop软件,虽然功能强大,但是为了这点小事还下一个这么大的软件,而且还不能用命令行。这显然不是Linux的精神。找了半天,终于找到了非常方便就能生成gif的小命令–imagemagick,用法简单而且还可以压缩,可以说是非常好用。

下载

myths@myths-X450LD:~$ sudo apt-get install imagemagick

简单易懂不罗嗦。

使用

1、生成
myths@myths-X450LD:~$ convert *.jpg out.gif
将当前文件夹下的*.jpg 压缩到out.gif,简单明了。

2、压缩
myths@myths-X450LD:~$ convert out.gif -fuzz 10% -layers Optimize optimized.gif
这里用了-fuzz 参数,用man查看帮助文档可以看到这一句:
-fuzz distance : colors within this distance are considered equal
就是模糊化处理的意思,一般距离参数选的是10%。

然后是-layers Optimize 表示优化的意思。

一般这样压缩gif效果非常明显通常能压缩到3%左右~~

基于stdarg.h的可变参数函数的用法

Posted on 2015-11-18 | In C/C++

.在开始学习C语言的函数的时候,我们就知道函数的参数个数应该是在函数声明的时候就指定的,这一点我们没有任何疑问。但是不知道大家有没有注意到我们的printf()函数,他的函数参数理论上并不是确定的,而是随着匹配字符串中的格式控制符的个数控制的。其实当时也没有注意到这一点,到是最近,偶然间看到了《嗨翻C语言》这本书,这里就详细讲解了这种可变参数函数的实现原理,今天考试间隙就顺带学习了一下,其实就是一种方法,知道了就晓得了,也是非常的简单。

头文件

这个用法需要引用一些宏,这些宏定义在C标准库“stdarg.h”中,(当然C++中就是“cstdarg”了)。

函数声明

1
2
3
int fun(int arg1,int arg2, ...){
//foo
}

这里可以用普通传参方法传入几个固定参数,一般情况下会有个表示可变参数的个数(否则函数也不晓得你到底传给了他啥),最后表示可变参数部分的是三个连续的点,注意中间不能有空格。我们传入的可变参数就是在这里的。

具体用法

这里主要涉及到va_list类型,va_start()、va_arg()、va_end()这几个函数。

va_list 可以理解为以链表的形式保存那些可变参数的一个数据类型(既然是链表,当然是可变的了)。

void va_start(va_list ap , int len); 接受两个参数,一个是上面的那个东西,还有一个表示数据的数目。目测可以理解为是自动收集传给本函数的可变参数,并连带他的数目,将信息赋给 va_list 。

type va_arg(va_list ap , type ); 接受两个参数,一个是va_list,另一个是保存的数据的类型。因为在调用参数的时候,编译器不会检查实际输入的是什么参数,所以需要适时的指定,并以那个类型返回。注意,这时候在va_list中的某个指针会指向下一个元素,所以下一次调用时输出的值就是下一个元素。在这一点上用法类似于iterator。

void va_end(va_list ap);最后清理这个va_list.

演示源码

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdarg.h>
#include<stdio.h>
void print(int arg,...){
va_list ap;
va_start(ap, arg);
for (int i = 0; i < arg; i++){
printf("%d ", va_arg(ap,int));
}
va_end(ap);
}
int main(){
print(4, 1, 2, 3, 4);
}

结果自然是输出 1 2 3 4。

说明

虽然看上去效果很酷,但是实际上据说这种用法的确很不安全,容易造成运行错误,所以还是尽量要少用。这种类似的问题如果能用重载或者类来解决的话还是少用可变参数函数吧。

1…515253…58

574 posts
69 categories
286 tags
© 2024 Companyd
Powered by Hexo
|
Theme — NexT.Muse v5.1.4