Created
October 31, 2016 20:02
-
-
Save penafieljlm/2af4a63a28ca20472a0dae2cf4f97682 to your computer and use it in GitHub Desktop.
Python source code attachment for the article at: https://penafieljlm.wordpress.com/2016/10/29/ekoparty-ctf-2016-write-ups/#web-150
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# creditcard.py | |
# | |
# Author: | |
# John Lawrence M. Penafiel (penafieljlm) | |
# | |
# Python source code attachment for the article at: | |
# https://penafieljlm.wordpress.com/2016/10/29/ekoparty-ctf-2016-write-ups/#web-150 | |
# | |
import math | |
def cardLuhnChecksumIsValid(card_number): | |
""" checks to make sure that the card passes a luhn mod-10 checksum """ | |
sum = 0 | |
num_digits = len(card_number) | |
oddeven = num_digits & 1 | |
for count in range(0, num_digits): | |
digit = int(card_number[count]) | |
if not (( count & 1 ) ^ oddeven ): | |
digit = digit * 2 | |
if digit > 9: | |
digit = digit - 9 | |
sum = sum + digit | |
return ( (sum % 10) == 0 ) | |
# list format: [missing_len, prefix, postfix] | |
jobs = [ | |
[5], [8], [7] | |
] | |
# read input formatted in the following manner: | |
# 4716 | |
# rest of your number | |
# 6108 | |
# Mastercard | |
# 5238 | |
# rest of your number | |
# 5763 | |
# American Express | |
# 3472 | |
# rest of your number | |
# 2959 | |
index = 0 | |
for index in range(0, 10 + 1): | |
line = raw_input() | |
if index == 0 or index == 2: | |
jobs[0].append(line.strip()) | |
if index == 4 or index == 6: | |
jobs[1].append(line.strip()) | |
if index == 8 or index == 10: | |
jobs[2].append(line.strip()) | |
index += 1 | |
for job in jobs: | |
length, prefix, postfix = job | |
for i in range(0, int(math.pow(10, length))): | |
iteration = str(i).zfill(length) | |
number = prefix + iteration + postfix | |
if cardLuhnChecksumIsValid(number): | |
print iteration | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment