Exploiting Software by Gary McGraw was a famous book around 10 years ago, but do not iamgine that the concepts described are deprecated. This book describes vulnerabilities that affect all kinds of software, not just web application; why they exist; and the logic behind the bugs in detail.
The interesting thing about this book is that it does not matter if you are looking in a Perl script, such as the scripts you will find in this book, or in the newest popular web framework; the logic is so similar.